#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> a;
a={1,1,3,3,2,2};
for (auto i:a) { cout << i << ", "; }
cout << "\n";
if (a.find(3)!=a.end())
{ cout << "找到 \n"; }
else { cout << "未找到 \n"; }
}
1, 2, 3,
找到
练习
定义一个函数
-
对输入的vector元素去重
-
输出新的vector
#include <iostream>
#include <set>
#include <vector>
using namespace std;
auto noSame(vector<string> Vec)
{
set<string> temp;
for (auto i:Vec) { temp.insert(i); }
vector<string> newVec;
for (auto i:temp) { newVec.push_back(i); }
return newVec;
}
int main()
{
vector<string> V1 = {"001","003","002","001","003"};
auto V2 = noSame(V1);
for (auto i:V2) { cout << i << ", "; }
cout << endl << V2.size() << endl;
}
001, 002, 003,
3