要剔除vector中重複元素首先使用std::unique找到不同的元素,這樣並不能把重複的元素刪除,因爲unique只是實現了把不同的相鄰元素copy到了前面,返回的iterator是沒有相同相鄰元素的下一個迭代位置,因此若要刪除重複元素還需要調用vector的erase函數將重複元素剔除表達方式如下(這樣做的前提是已經對vector元素進行了排序否則還是行不通,排序可以用sort(intvect.begin,intvect.end))
<span style="font-size:18px;">std::vector<int> intvect(3, 10);
intvect.insert(intvect.end(), 2, 11);
intvect.erase(std::unique(intvect.begin(), intvect.end()), intvect.end());
</span>
intvect中的值在unique之前是10,10,10,11,11
之後是10,11,10,11,11。
執行erase後是10, 11
這樣即實現了刪除vector中的重複元素