{"rsdb":{"rid":"244436","subhead":"","postdate":"0","aid":"168211","fid":"49","uid":"1","topic":"1","content":"
\n

vector::erase<\/p> \n

C++<\/a> vector\u7684\u5143\u7d20\u5220\u9664\uff0c\u6e90\u7801<\/a>\u662f\u8fd9\u6837\u7684\uff1a<\/p> \n

\r\ntemplate <class _Tp, class _Allocator>\r\ninline _LIBCPP_INLINE_VISIBILITY\r\ntypename vector<_Tp, _Allocator>::iterator\r\nvector<_Tp, _Allocator>::erase(const_iterator __position)\r\n{\r\n#if _LIBCPP_DEBUG_LEVEL >= 2\r\n    _LIBCPP_ASSERT(__get_const_db()->__find_c_from_i(&__position) == this,\r\n        "vector::erase(iterator) called with an iterator not"\r\n        " referring to this vector");\r\n#endif\r\n    _LIBCPP_ASSERT(__position != end(),\r\n        "vector::erase(iterator) called with a non-dereferenceable iterator");\r\n    difference_type __ps = __position - cbegin();\r\n    pointer __p = this->__begin_ + __ps;\r\n    this->__destruct_at_end(_VSTD::move(__p + 1, this->__end_, __p));\r\n    this->__invalidate_iterators_past(__p-1);\r\n    iterator __r = __make_iter(__p);\r\n    return __r;\r\n}<\/pre> \n 

\u53ef\u77e5\uff0cC++<\/a>\u5c06\u88ab\u5220\u9664\u90e8\u5206\u7684\u540e\u9762\u6240\u6709\u5143\u7d20\u4f5c\u4e3a\u4e00\u4e2a\u96c6\u5408\u5411\u524d\u79fb\u52a8\u4e86\u3002<\/p> \n

\u6240\u4ee5\uff0c\u5047\u8bbe\u6211\u4eec\u9700\u8981\u6e05\u7a7a\u5143\u7d20\uff0c\u4e0d\u8981\u8fd9\u6837\u5199\uff1a<\/p> \n

\r\nint main()\r\n{\r\n    vector<int> v_int;\r\n    for(int i=0;i<10;i++){\r\n        v_int.push_back(i+1);\r\n    }\r\n    int size = v_int.size();\r\n    vector<int>::iterator it = v_int.begin();\r\n    while(size--) {\r\n        cout<<*it<<" ";\r\n        v_int.erase(it++);   \/\/ attention !\r\n        cout<<"size: "<<v_int.size()<<endl;\r\n    }\r\n    return 0;\r\n}<\/pre> \n 

\u5b83\u5f97\u5230\u7ed3\u679c\u662f\u8fd9\u6837\u7684\uff1a<\/p> \n

\r\n1 size: 9\r\n3 size: 8\r\n5 size: 7\r\n7 size: 6\r\n9 size: 5<\/pre> \n 

\u5c06\u4f8b\u5b50\u4e2d\u7684it++\u6539\u6210it\u5373\u53ef\uff0c\u90a3\u6837\u624d\u80fd\u6e05\u7a7a\u6240\u6709\u7684\u5143\u7d20\u3002<\/p> \n

\u65e0\u53c2\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528<\/p> \n

\u5f62\u5982Base instance()\u4f1a\u8c03\u7528\u7c7b\u7684\u65e0\u53c2\u6784\u9020\u51fd\u6570\u5417?<\/p> \n

\u7b54\u6848\u662f\u5426\u5b9a\u7684\uff0cC++\u4f1a\u5c06\u5176\u89e3\u91ca\u6210\u8fd4\u56deBase\u5bf9\u8c61\u7684\u51fd\u6570\u3002<\/p> \n

\r\n#include <iostream>\r\n#include <vector>\r\n#include <string>\r\nusing namespace std;\r\n\r\nclass Base{\r\npublic:\r\n    Base(){ cout<<"Base()..\\n"; }\r\n    Base(string str){ cout<<str<<endl; }\r\n    ~Base(){ cout<<"~Base().. \\n"; }\r\n};\r\n\r\nint main()\r\n{\r\n    \/\/Base ins(); \/\/empty parentheses interpreted as a function declaration\r\n    Base ins;     \/\/ok\r\n    Base ins2("hello world");  \/\/ok\r\n    return 0;\r\n}<\/pre> \n 

\u4e0d\u8fc7\u6709\u610f\u601d\u7684\u662f\uff0c\u6709\u53c2\u6784\u9020\u51fd\u6570\u5374\u662f\u53ef\u4ee5\u88ab\u8fd9\u6837\u8c03\u7528\u7684\u3002<\/p> \n

\u6709\u65f6\uff0c\u6211\u4eec\u4f1a\u9047\u5230\u8fd9\u6837\u7684\u51fd\u6570\uff1a<\/p> \n

\r\nvoid show(const Base &b){\r\n    \/\/...\r\n}<\/pre> \n 

\u9700\u8981\u7ed9\u8be5\u51fd\u6570\u4f20\u9012\u4e00\u4e2a\u5b58\u50a8\u5728\u6808\u4e2d\u7684\u53d8\u91cf\uff0c\u8981\u6784\u9020\u8fd9\u6837\u7684\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u5730\u8fd9\u6837\u5199Base(). \u5b83\u7b49\u4ef7\u4e8eBase b.<\/p> \n

\u5373\uff1a<\/p> \n

\r\n    \/\/show(Base("hello"));  \/\/ok. output: hello     it's equal to 'Base b("hello"); show(b);'\r\n    \/\/show(Base());         \/\/ok. output: Base()..  it's equal to 'Base b; show(b);'<\/pre> \n<\/div>","orderid":"0","title":"C++ vector::erase\u548c\u65e0\u53c2\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\uff08\u4ee3\u7801\u6559\u7a0b\uff09","smalltitle":"","mid":"0","fname":"c++\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"191","pages":"1","comments":"0","posttime":"2018-02-13 12:56:08","list":"1518497768","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"vector::erase<\/A> \u6784\u9020<\/A> \u51fd\u6570<\/A> \u8c03\u7528<\/A> \u4ee3\u7801<\/A> \u6559\u7a0b<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"113.87.122.142","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"C++ vector::erase\u548c\u65e0\u53c2\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\uff08\u4ee3\u7801\u6559\u7a0b\uff09","lastview":"1713369121","digg_num":"6","digg_time":"1711652852","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}