{"id":322,"date":"2023-09-16T10:32:10","date_gmt":"2023-09-16T02:32:10","guid":{"rendered":""},"modified":"2023-09-18T11:49:45","modified_gmt":"2023-09-18T03:49:45","slug":"c-c%e6%95%b0%e7%bb%84%e5%88%9d%e5%a7%8b%e5%8c%96%e7%9a%84%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/mushiming.com\/322.html","title":{"rendered":"C\/C++\u6570\u7ec4\u521d\u59cb\u5316\u7684\u603b\u7ed3"},"content":{"rendered":"
\u76ee\u5f55<\/strong><\/p>\n \u6570\u7ec4\u5b9a\u4e49\u4e0d\u521d\u59cb\u5316\u4f1a\u88ab\u968f\u673a\u8d4b\u503c<\/p>\n \u6570\u7ec4\u521d\u59cb\u5316\u7684\u51e0\u79cd\u5f62\u5f0f<\/p>\n \u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u67d0\u4e00\u56fa\u5b9a\u503c<\/p>\n memset(\u6570\u7ec4\u540d\uff0c\u503c\uff0csizeof(\u6570\u7ec4\u540d))<\/p>\n fill(begin(),end(),value)<\/p>\n \u4ee5\u524d\u521a\u5f00\u59cb\u5b66\u6570\u7ec4\u521d\u59cb\u5316\uff0c\u4e00\u76f4\u90fd\u6ca1\u641e\u592a\u6e05\u695a\uff0c\u5305\u62ec\u53c2\u52a0PAT\u8003\u8bd5\uff0c\u76f4\u5230\u770b\u4e86\u80e1\u51e1\u7684\u300a\u7b97\u6cd5\u7b14\u8bb0\u300b\uff0c\u9010\u6e10\u6e05\u6670\uff0c\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n \u56e0\u6b64\u5982\u679c\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u5728\u4e0b\u9762\u6ca1\u6709\u9010\u4e00\u8d4b\u503c\uff0c\u4f46\u662f\u53c8\u4f1a\u4f7f\u7528\u5230\u7684\u8bdd\uff0c\u6700\u540e\u4e0d\u8981\u53ea\u5b9a\u4e49\u800c\u4e0d\u521d\u59cb\u5316\u3002\u4f1a\u5e26\u6765\u95ee\u9898\u3002<\/p>\n \u53ef\u4ee5\u5c06\u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u67d0\u4e00\u56fa\u5b9a\u503c\u3002\u6709\u4e24\u79cd\u65b9\u6cd5\uff0c\u4e00\u79cd\u662f\u4f7f\u7528<string>(C++)\u6216<string.h>(C)\u4e2d\u7684memset\uff08\uff09\u51fd\u6570\uff0c\u8fd8\u6709\u4e00\u79cd\u662f\u4f7f\u7528<algorithm>\u4e2d\u7684fill()\u51fd\u6570\uff0cfill\u51fd\u6570\u53ef\u4ee5\u521d\u59cb\u5316\u5bb9\u5668\u548c\u6570\u7ec4\u3002<\/p>\n \u53ea\u5efa\u8bae\u4f7f\u7528memset()\u7684\u65f6\u5019\u521d\u59cb\u5316\u4e3a0\u6216\u8005-1\u3002menset()\u51fd\u6570\u7684\u529f\u80fd\u6027\u4e0d\u5982fill\uff08\uff09\u51fd\u6570\uff0c\u5176\u4f18\u70b9\u5728\u4e8e\u6267\u884c\u901f\u5ea6\u66f4\u5feb\u3002<\/p>\n \u7a0b\u5e8f\u5b9e\u4f8b\uff1a<\/p>\n fill()\u53ef\u4ee5\u628a\u6570\u7ec4\u548c\u5bb9\u5668\u4e2d\u7684\u67d0\u4e00\u6bb5\u533a\u95f4\u8d4b\u503c\u4e3a\u67d0\u4e2a\u76f8\u540c\u7684\u503c\u3002\u548cmemset()\u4e0d\u540c\u7684\u662f\u8fd9\u91cc\u7684\u8d4b\u503c\u53ef\u4ee5\u662f\u6570\u7ec4\u7c7b\u578b\u5bf9\u5e94\u8303\u56f4\u4e2d\u7684\u4efb\u610f\u503c\u3002<\/p>\n \u7a0b\u5e8f\u5b9e\u4f8b\uff1a<\/p>\n \u4ece\u8fd9\u91cc\u4e5f\u53ef\u4ee5\u770b\u51fa\uff0ca[]\u540e\u9762\u7684\u5143\u7d20\u662f\u968f\u673a\u8d4b\u503c\u7684\u3002<\/p>\n \u5efa\u8bae\u91c7\u7528fill\uff08\uff09\u800c\u4e0d\u662f\u4f7f\u7528memset()\u3002<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"C\/C++\u6570\u7ec4\u521d\u59cb\u5316\u7684\u603b\u7ed3\u76ee\u5f55\u6570\u7ec4\u5b9a\u4e49\u4e0d\u521d\u59cb\u5316\u4f1a\u88ab\u968f\u673a\u8d4b\u503c\u6570\u7ec4\u521d\u59cb\u5316\u7684\u51e0\u79cd\u5f62\u5f0f\u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u67d0\u4e00\u56fa\u5b9a\u503cmemset(\u6570\u7ec4\u540d\uff0c\u503c\uff0csiz...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/322"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=322"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/322\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\n\n
\u6570\u7ec4\u5b9a\u4e49\u4e0d\u521d\u59cb\u5316\u4f1a\u88ab\u968f\u673a\u8d4b\u503c<\/h1>\n<\/li>\n<\/ul>\n
\n
\u6570\u7ec4\u521d\u59cb\u5316\u7684\u51e0\u79cd\u5f62\u5f0f<\/h1>\n<\/li>\n<\/ul>\n
\n
\n
\u6570\u7ec4\u521d\u59cb\u5316\u4e3a\u67d0\u4e00\u56fa\u5b9a\u503c<\/h1>\n<\/li>\n<\/ul>\n
\n
memset(\u6570\u7ec4\u540d\uff0c\u503c\uff0csizeof(\u6570\u7ec4\u540d))<\/strong><\/h2>\n<\/li>\n<\/ul>\n
#include <stdio.h>\nint main(){\n int a[10];\n memset(a,5,sizeof(a));\n for(int i=0;i<10;i++)\n printf(\"%d\",a[i]);\n return 0;\n}\n\n\/\/output:5555555555<\/code><\/pre>\n
\n
fill(begin(),end(),value)<\/h2>\n<\/li>\n<\/ul>\n
#include <iostream>\n#include <algorithm>\nusing namespace std;\nint main(){\n int a[10];\n fill(a,a+5,233);\n for(int i=0;i<10;i++)\n cout<<a[i]<<\" \";\n return 0;\n}\n\n\/*\n233 233 233 233 233 0 31 0 0 0\n--------------------------------\nProcess exited after 0.4914 seconds with return value 0\n\u8bf7\u6309\u4efb\u610f\u952e\u7ee7\u7eed. . .\n*\/<\/code><\/pre>\n