-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathset_from_container.cpp
41 lines (34 loc) · 1 KB
/
set_from_container.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <vector>
#include <set>
#include <iostream>
//https://stackoverflow.com/questions/12850927/adding-elements-of-a-vector-to-an-unordered-set
int main()
{
std::vector<int> v = {1, 3, 3, 5, 7, 7, 7};
//initialize set with vector
std::set<int> myset(v.begin(), v.end());
for(std::set<int>::iterator it = myset.begin(); it!=myset.end(); it++){
std::cout << *it << " ";
}
std::cout << std::endl;
//initialize set with string
std::string mystr = "hello";
std::set<char> mysetc(mystr.begin(), mystr.end());
for(std::set<char>::iterator it = mysetc.begin(); it!=mysetc.end(); it++){
std::cout << *it << " ";
}
std::cout << std::endl;
//append set with vector
v = {8, 9, 10, 8};
std::copy(v.begin(),v.end(),std::inserter(myset, myset.end()));
for(std::set<int>::iterator it = myset.begin(); it!=myset.end(); it++){
std::cout << *it << " ";
}
std::cout << std::endl;
return 0;
}
/*
1 3 5 7
e h l o
1 3 5 7 8 9 10
*/