{"id":1248,"date":"2018-09-09T10:35:23","date_gmt":"2018-09-09T02:35:23","guid":{"rendered":"http:\/\/www.yusian.com\/blog\/?p=1248"},"modified":"2018-09-09T10:35:23","modified_gmt":"2018-09-09T02:35:23","slug":"cstl%e4%b8%adlist%e4%b8%8evector%e5%9c%a8%e6%95%88%e7%8e%87%e6%96%b9%e9%9d%a2%e7%9a%84%e6%af%94%e8%be%83","status":"publish","type":"post","link":"https:\/\/www.yusian.com\/blog\/cpp\/2018\/09\/09\/1035231248.html","title":{"rendered":"C++STL\u4e2dlist\u4e0evector\u5728\u6548\u7387\u65b9\u9762\u7684\u6bd4\u8f83"},"content":{"rendered":"<p>1\u3001vector\u7684\u6570\u636e\u7ed3\u6784\u7c7b\u4f3c\u6570\u7ec4\uff0c\u5728\u5185\u5b58\u4e2d\u4e3a\u4e00\u7247\u8fde\u7eed\u7684\u5b58\u50a8\u7a7a\u95f4\uff1b<br \/>\n2\u3001list\u7684\u6570\u636e\u7ed3\u6784\u4e3a\u94fe\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u4e2d\u90fd\u4fdd\u5b58\u4e86\u4e0b\u4e00\u4e2a\u5143\u7d20\u7684\u5730\u5740\uff0c\u7a7a\u95f4\u53ef\u4ee5\u4e0d\u8fde\u7eed\uff1b<br \/>\n3\u3001\u57fa\u4e8e\u4e24\u8005\u6570\u636e\u7ed3\u6784\u7684\u7279\u70b9\uff0cvector\u7684\u968f\u673a\u8bbf\u95ee\u901f\u5ea6\u5feb\uff0clist\u7684\u589e\u5220\u64cd\u4f5c\u5feb\uff1b<br \/>\n4\u3001\u4ee51\u4ebf\u4e2a\u5143\u7d20\u7684\u5206\u522b\u4ee5list\u4e0evector\u7684\u65b9\u5f0f\u5b58\u50a8\u6765\u6bd4\u8f83\u4e24\u8005\u7684\u901f\u5ea6\uff1b<\/p>\n<pre lang=\"cpp\" line=\"1\">#include <iostream>\r\n#include <list>\r\n#include <vector>\r\nusing namespace std;\r\n\r\nint main(){\r\n    struct Node{\r\n        int m_a;\r\n        int m_b;\r\n    };\r\n    \r\n    clock_t time1 = clock(); \/\/ list\u521b\u5efa\u5f00\u59cb\r\n    list<Node> list1(100000000, {1, 2});\r\n    \r\n    clock_t time2 = clock(); \/\/ list\u521b\u5efa\u7ed3\u675f\uff0c\u5f00\u59cb\u904d\u5386\r\n    for_each(list1.begin(), list1.end(), [](Node &n)->void{\r\n\r\n    });\r\n    \r\n    clock_t time3 = clock(); \/\/ list\u904d\u5386\u7ed3\u675f\uff0cvector\u521b\u5efa\u5f00\u59cb\r\n    vector<Node> vect(100000000, {3, 4});\r\n    \r\n    clock_t time4 = clock(); \/\/ vector\u521b\u5efa\u7ed3\u675f\uff0c\u5f00\u59cb\u904d\u5386\r\n    for_each(vect.begin(), vect.end(), [](Node &n)->void{\r\n\r\n    });\r\n    \r\n    clock_t time5 = clock(); \/\/ vector\u904d\u5386\u7ed3\u675f\uff0clist\u5f00\u59cb\u63d2\u5165\u9996\u503c\r\n    list1.insert(list1.begin(), (Node){0, 1});\r\n    \r\n    clock_t time6 = clock(); \/\/ list\u63d2\u5165\u7ed3\u675f\uff0cvector\u5f00\u59cb\u63d2\u5165\r\n    vect.insert(vect.begin(), (Node){0, 1});\r\n    clock_t time7 = clock(); \/\/ vector\u63d2\u5165\u7ed3\u675f\r\n    \r\n    cout << fixed;\r\n    cout << \"list\u521b\u5efa1\u4ebf\u4e2a\u5143\u7d20\u6240\u82b1\u65f6\u95f4\uff1a\" << (time2 - time1) \/ 1000000.0 << \"\u79d2\" << endl;\r\n    cout << \"list\u904d\u53861\u4ebf\u4e2a\u5143\u7d20\u6240\u82b1\u65f6\u95f4\uff1a\" << (time3 - time2) \/ 1000000.0 << \"\u79d2\" << endl;\r\n    cout << \"vect\u521b\u5efa1\u4ebf\u4e2a\u5143\u7d20\u6240\u82b1\u65f6\u95f4\uff1a\" << (time4 - time3) \/ 1000000.0 << \"\u79d2\" << endl;\r\n    cout << \"vect\u904d\u53861\u4ebf\u4e2a\u5143\u7d20\u6240\u82b1\u65f6\u95f4\uff1a\" << (time5 - time4) \/ 1000000.0 << \"\u79d2\" << endl;\r\n    cout << \"list\u63d2\u5165\u9996\u503c\u6240\u82b1\u65f6\u95f4\uff1a\" << (time6 - time5) \/ 1000000.0 << \"\u79d2\" << endl;\r\n    cout << \"vect\u63d2\u5165\u4ea7\u503c\u6240\u82b1\u65f6\u95f4\uff1a\" << (time7 - time6) \/ 1000000.0 << \"\u79d2\" << endl;\r\n    return 0;\r\n}<\/pre>\n<p>5\u3001\u8fd0\u884c\u7ed3\u679c\uff1a<br \/>\n<code>list\u521b\u5efa1\u4ebf\u4e2a\u5143\u7d20\u6240\u82b1\u65f6\u95f4\uff1a12.982064\u79d2<br \/>\nlist\u904d\u53861\u4ebf\u4e2a\u5143\u7d20\u6240\u82b1\u65f6\u95f4\uff1a0.901292\u79d2<br \/>\nvect\u521b\u5efa1\u4ebf\u4e2a\u5143\u7d20\u6240\u82b1\u65f6\u95f4\uff1a1.543856\u79d2<br \/>\nvect\u904d\u53861\u4ebf\u4e2a\u5143\u7d20\u6240\u82b1\u65f6\u95f4\uff1a0.649677\u79d2<br \/>\nlist\u63d2\u5165\u9996\u503c\u6240\u82b1\u65f6\u95f4\uff1a0.000005\u79d2<br \/>\nvect\u63d2\u5165\u4ea7\u503c\u6240\u82b1\u65f6\u95f4\uff1a2.371839\u79d2<br \/>\nProgram ended with exit code: 0<\/code><br \/>\n6\u3001\u6bd4\u8f83\u660e\u663e\u7684\u533a\u522b\u5728\u4e8e\uff0clist\u521b\u5efa\u5bf9\u8c61\u65f6\u6240\u82b1\u7684\u65f6\u95f4\u8fdc\u8fdc\u5927\u4e8evector\uff0c\u7136\u540evector\u63d2\u5165\u5143\u7d20\u6240\u9700\u8981\u7684\u65f6\u95f4\u8fdc\u8fdc\u5927\u4e8elist\u3002<br \/>\n7\u3001\u901a\u8fc7\u7ed3\u679c\u53ef\u63a8\u65advector\u5982\u679c\u9700\u8981\u8fdb\u884c\u591a\u4e2a\u8282\u70b9\u7684\u63d2\u5165\u6216\u5220\u9664\uff0c\u6240\u9700\u7684\u65f6\u95f4\u4f1a\u66f4\u591a\uff1b<br \/>\n8\u3001\u540c\u6837\uff0clist\u5982\u679c\u9700\u8981\u5927\u91cf\u7684\u67e5\u627e\u64cd\u4f5c\uff0c\u6240\u9700\u8981\u7684\u65f6\u5019\u4e5f\u4f1a\u5927\u4e8evector\uff0c\u56e0\u4e3a\u968f\u673a\u8bbf\u95ee\u6548\u7387\u8f83\u4f4e\uff1b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001vector\u7684\u6570\u636e\u7ed3\u6784\u7c7b\u4f3c\u6570\u7ec4\uff0c\u5728\u5185\u5b58\u4e2d\u4e3a\u4e00\u7247\u8fde\u7eed\u7684\u5b58\u50a8\u7a7a\u95f4\uff1b 2\u3001list\u7684\u6570\u636e\u7ed3\u6784\u4e3a\u94fe\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u4e2d\u90fd\u4fdd\u5b58\u4e86\u4e0b\u4e00\u4e2a\u5143\u7d20\u7684\u5730\u5740\uff0c\u7a7a\u95f4\u53ef\u4ee5\u4e0d\u8fde\u7eed\uff1b 3\u3001\u57fa\u4e8e\u4e24\u8005\u6570\u636e\u7ed3\u6784\u7684\u7279\u70b9\uff0cvector\u7684\u968f\u673a\u8bbf\u95ee\u901f\u5ea6\u5feb\uff0clist\u7684\u589e\u5220\u64cd\u4f5c\u5feb\uff1b 4\u3001\u4ee51\u4ebf\u4e2a\u5143\u7d20\u7684\u5206\u522b\u4ee5list\u4e0evector\u7684\u65b9\u5f0f\u5b58\u50a8\u6765\u6bd4\u8f83\u4e24\u8005\u7684\u901f\u5ea6\uff1b #include #include #include using namespace std; int main(){ struct Node{ int m_a; int m_b; }; clock_t time1 = clock(); \/\/ list\u521b\u5efa\u5f00\u59cb list list1(100000000, {1, 2}); clock_t time2 = clock(); \/\/ list\u521b\u5efa\u7ed3\u675f\uff0c\u5f00\u59cb\u904d\u5386 for_each(list1.begin(), list1.end(), [](Node &#038;n)->void{ }); clock_t time3 = clock(); \/\/ list\u904d\u5386\u7ed3\u675f\uff0cvector\u521b\u5efa\u5f00\u59cb vector vect(100000000, {3, 4}); clock_t time4 = clock(); \/\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[88],"tags":[89,105,103,106,108,107],"class_list":["post-1248","post","type-post","status-publish","format-standard","hentry","category-cpp","tag-c","tag-list","tag-stl","tag-vector","tag-108","tag-107"],"_links":{"self":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/comments?post=1248"}],"version-history":[{"count":0,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/posts\/1248\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/media?parent=1248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/categories?post=1248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yusian.com\/blog\/wp-json\/wp\/v2\/tags?post=1248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}