CPP STL Contain

c语言
Article Directory
  1. 1. SET
  2. 2. map
  3. 3. Copy

SET

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
set<int> iset;

struct P{
int x;
int y;

// 重载操作符 <
bool operator< (const P p) const{
if (this->x < p.x && this-> y < p.y){
return true;
}else if (this->y < p.y){
return true;
}
return false;
}

bool operator == (const P p) const{
return this->x == p.x && this->y == p.y;
}
}

iset.insert(P);
iset.insert(P);
iset.insert(P);

// 这个时候迭代获得的是值
for (auto i : iset){
cout << iset <<endl;
}

// 返回一个迭代器,要访问迭代器指向的元素元素,使用 * 操作符
// 普通迭代器,只可以 ++ 操作
// 反向迭代器的 ++ 操作为递减 (rbegin(), rend())
// 随机 和 双向迭代器 ,,不会使,据说 C++ 11 标准中只有 5 中迭代器
// ---------------
// distance() 返回两个迭代器之间的距离,不过,这个特性,我目前没研究明白
// ----------------
// 在 for 循环中,ite 的值不会到达 iset.end(),迭代器,也不能使用 加减操作
// 所以,end() 迭代器实际上不止向任何元素,只是 作为结束标记,和 c_str 中的 00 结尾是一样的
// 所以,不能写类似 if (ite == iset.end() )... 这样的代码
for (auto ite = iset.begin() ; ite != iset.end() ; ite++){
cout << "x: " << ite->x << " y: " <<ite.y <<endl;
}

// set 不能通过下标访问,但是,可以通过 find() 进行查找,查找,将返回一个迭代器
// 需要比较的类有实现 == 操作
// 如果,没有找到元素,那么返回 end() 迭代器
set<P>::iterator res = iset.find(P(1, 2));
cout << res->x << " " << res->y <<endl;

map

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <map>
#include <iostream>
#include <string>


using namespace std;


class Node{
public:
int id;
string name;

Node (int id){
this->id = id;
this->name = "Undifined";
}

Node (int id, string name){
this->id = id;
this->name = name;
}

string toString(){
return "{ id: " + to_string(this->id) + ", name: " + this->name + "}";
}


};


int main(){

map<int, Node*> nodes;

Node a(1, "node 1"), b(2, "node 2"), c(3, "node 3");

cout << a.toString() <<endl;;
cout << b.toString() <<endl;;

// *************** insert
// insert a pair: first is the key ; second is the value

// ------ NOTE -------
// use insert function is different with use index to add items
// eg.
// nodes[1] = &a;
// nodes[1] = &b;
// then ,you find the nodes[1] the value will be item b;
//
// ----- BUT.... ----
// if you write the code like below
// the value won't be changed
// the value of nodes[1] will still be 'a
// so, it was conclude that insert() of map will ignore the dulpicate key's value
//
// nodes.insert({
// a.id,
// &a
// });
// nodes.insert({
// 1,
// &b
// });
cout << "---- Test Insert ---" <<endl;
nodes.insert({
a.id,
&a
});

// this value will be ignore because the key 1 is exisits in nodes
nodes.insert({
1,
&b
});


// ---- the map container is ordered -----------
// So, you cloud insert a key-value pair at the position you want to insert
// such as :
// nodes.insert(nodes.end(), {1, a});
// this code will insert {1, a} before nodes.end();
// when you ignore the position param, it has the same effect as the code above
// i think it is a shorthand of insert(position, value) ;


cout << "size: " << nodes.size() <<endl;

// ************** find a itm from map with a key
// find return a iterator if find the item else return the end() iterator of the container
//
cout << "----- Test find ----" <<endl;
auto res = nodes.find(1);
if (res == nodes.end()){
cout << "has not found itm " <<endl;
}else{
Node* p = (*res).second;
cout << p->toString()<<endl;
}
// if can not find the key in container, it will return end iterator
res = nodes.find(2);
if (res == nodes.end()){
cout << "has not found itm " <<endl;
}else{
Node* p = (*res).second;
cout << p->toString()<<endl;
}

// of course , you could get the value like this
cout << "node1: at():" << nodes.at(1)->toString() <<endl;;
cout << "node1: [] :" << nodes[1]->toString() <<endl;
// they all return a pointer of the value





nodes.insert({
b.id,
&b
});
nodes.insert({
c.id,
&c
});
// *********** Delete a key-value in map
// use erase() or clear()
// clear() will remove all item in map
//
// you cloud erase a item by a iterator
// erase(iterator it);
// Or remove in a range
// erase(iterator first, iterator last)
// even by a key
// erase(const Key &key)
cout << "---- Test delete ---" <<endl;
res = nodes.find(2);
if (res == nodes.end()){
cout << "has not found itm " <<endl;
}else{
Node* p = (*res).second;
cout << p->toString()<<endl;
}
// erase id 2
nodes.erase(2);

res = nodes.find(2);
if (res == nodes.end()){
cout << "has not found itm " <<endl;
}else{
Node* p = (*res).second;
cout << p->toString()<<endl;
}


nodes.erase(nodes.begin(), nodes.end());
res = nodes.find(1);
if (res == nodes.end()){
cout << "has not found itm " <<endl;
}else{
Node* p = (*res).second;
cout << p->toString()<<endl;
}

// -------- Swap -----
// Swap function swap two map contaoners items
// it was not to switch two items in a same map but use to sitch tow maps' value

// code ignore ...


return 0;
}

Copy

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

int main()
{
//vector复制到vector
{
vector<int> src = { 0, 1, 2, 3, 4 };
vector<int> dst(8, -1);
std::copy(src.begin(), src.end(), dst.begin());
for (int i = 0; i < dst.size(); i++)
{
cout << dst[i] << '\t';
}
cout << endl;
}

//vector插入到vector末尾
{
vector<int> src = { 0, 1, 2, 3, 4 };
vector<int> dst = { -10, -9 };
std::copy(src.begin(), src.end(), std::back_inserter(dst));
for (int i = 0; i < dst.size(); i++)
{
cout << dst[i] << '\t';
}
cout << endl;
}

//set插入到vector
{
set<int> src = { 4, 3, 2, 1, 0 };
vector<int> dst;
std::copy(src.begin(), src.end(), std::back_inserter(dst));
for (int i = 0; i < dst.size(); i++)
{
cout << dst[i] << '\t';
}
cout << endl;
}

//数组插入到vector
{
int src[5] = { 0, 1, 2, 3, 4 };
vector<int> dst;
std::copy(src, src+5, std::back_inserter(dst));
for (int i = 0; i < dst.size(); i++)
{
cout << dst[i] << '\t';
}
cout << endl;
}

//vector插入到数组
{
vector<int> src = { 0, 1, 2, 3, 4 };
int dst[8] = { -1 };
std::copy(src.begin(), src.end(), dst);
for (int i = 0; i < 8; i++)
{
cout << dst[i] << '\t';
}
cout << endl;
}

//数组插入到数组
{
int src[5] = { 0, 1, 2, 3, 4 };
int dst[8] = { -1 };
std::copy(src, src + 5, dst);
for (int i = 0; i < 8; i++)
{
cout << dst[i] << '\t';
}
cout << endl;
}
}

Author: 哒琳

Permalink: http://blog.jieis.cn/2022/4b21b6dc-3ec6-4d18-a73a-dc38ceb84111.html

Comments