customized_set :自定义set内类型、erase、遍历、关系函数

customized_multiset :自定义multiset内类型、erase、遍历、关系函数

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//============================================================================
// Name : multi_set_te.cpp
// Author :
// Version :
// Copyright :
// Description :
//============================================================================

#include <iostream>
#include <set>
#include <vector>
using namespace std;

struct Struct{
int a;
};

typedef bool(*s_compare_1)(const Struct &,const Struct &);

bool s_compare(const Struct & s1,const Struct &s2){
return s1.a < s2.a;
}

bool s_compare_2(const Struct & s1,const Struct &s2){
return s1.a <= s2.a;
}

std::ostream& operator <<(std::ostream& os,const Struct & s1){
return os<<s1.a;
}

void customized_multiset(){
//multiset<Struct,decltype(s_compare) *> ms(s_compare);
multiset<Struct,s_compare_1> ms(s_compare);
ms.insert({5});
ms.insert({10});
ms.insert({20});
ms.insert({5});
//multiset<Struct,decltype(s_compare) *>::const_iterator ms_cit = ms.begin();
multiset<Struct,s_compare_1>::const_iterator ms_cit = ms.begin();
for(;ms_cit!=ms.end();ms_cit++){
cout<<*ms_cit<<endl;
}
cout<<"ms delete erase after"<<endl;
ms.erase({5});
//multiset<Struct,decltype(s_compare) *>::const_iterator ms_cit = ms.begin();
ms_cit = ms.begin();
for(;ms_cit!=ms.end();ms_cit++){
cout<<*ms_cit<<endl;
}
}

void customized_set(){
//multiset<Struct,decltype(s_compare) *> ms(s_compare);
set<Struct,s_compare_1> ms(s_compare);
//set<Struct,s_compare_1> ms(s_compare_2);
ms.insert({5});
ms.insert({10});
ms.insert({20});
ms.insert({5});
//multiset<Struct,decltype(s_compare) *>::const_iterator ms_cit = ms.begin();
multiset<Struct,s_compare_1>::const_iterator ms_cit = ms.begin();
for(;ms_cit!=ms.end();ms_cit++){
cout<<*ms_cit<<endl;
}
}

void vector2set2vector(){
int nums[] = { 1, 2, 3, 4, 8, 9, 3, 2, 1, 0, 4, 8 };
vector<int> vec(nums,nums+12);
set<int> st(vec.begin(), vec.end());
vec.assign(st.begin(), st.end());

vector<int>::iterator it;
for (it = vec.begin(); it != vec.end(); it++)
cout << *it<<endl;
}


int main() {
//customized_set();
//customized_multiset();
vector2set2vector();

}