vector 中的元素去重
#include<iostream>
#include<vector>
#include<algorithm>
?
using namespace std;
?
int main()
{
??? cout << "********************一維向量*************************" << endl;
??? vector<int> a;
??? for (int i = 0; i < 6; i++)
??? {
??????? a.push_back(i);
??? }
??? cout << "a[4]:" << a[4] << endl;
?
??? cout << "*********************************************" << endl;
??? for (int i = 0; i < 6; i++)
??? {
??????? cout << i <<":" << a[i] << endl;
??? }
??? a.push_back(1);
??? a.push_back(2);
??? a.push_back(3);
??? cout << "去重前 " <<a.size() << endl;
??? cout <<? "*********************************************"? << endl;
?
??? sort(a.begin(), a.end());
??? a.erase(unique(a.begin(), a.end()), a.end());
??? cout << "去重前" << a.size() << endl;
?
??? //a.size()???????????????? //獲取向量中的元素個數(shù)
??? //a.empty()??????????????? //判斷向量是否為空
??? //a.clear()??????????????? //清空向量中的元素
??? //a = b;???????????????? //將b向量復制到a向量中
??? system("pause");
??? return 0;
}
?
?
總結(jié)
以上是生活随笔為你收集整理的vector 中的元素去重的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vector元素的读取
- 下一篇: Eigen(8)实例最小二乘法