cppSTL

C++ STL编程 学习笔记

STL是C++的标准模板库、可以快速实现代码逻辑。

大体上包括:容器、算法封装、迭代器、仿函数、适配器、分配器

vector

类似于数据结构的顺序表

定义vector

1
2
3
4
5
int main() {
vector<int> v = {1,2,3};
cout << v[0] << endl;
return 0;
}
1
2
输出:
3

操作方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() {
vector<int> v = {1,2,3};

cout<< v.capacity() << endl; // 查看vector容量

v.push_back(4); // 在尾部添加数据
cout<< v.capacity() << endl;

cout<< "begin -> " <<*v.begin() << endl; // 指向容器开始位置,需要解引用
cout<< "end -> " <<*(v.end()-1) << endl; // 末尾位置,左闭右开
cout<< "front -> " <<v.front() << endl; // 容器开头元素
cout<< "back -> " <<v.back() << endl; // 末尾元素
return 0;
}
1
2
3
4
5
6
7
输出:
3
6
begin -> 1
end -> 4
front -> 1
back -> 4

打印函数

1
2
3
4
5
6
7
8
9
10
11
12
13
void printVector (vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
cout << *it << " ";
}
}
......

int main() {
vector<int> v = {1,2,3};
printVector(v);
return 0;

}
1
2
输出:
1 2 3

迭代器

1
2
3
4
5
6
7
8
int main() {
vector<int> v1 = {1,2,3};

vector<int> v2(v1.begin(),v1.end());
printVector(v2);
return 0;

}
1
2
输出:
1 2 3

全零初始化

1
2
3
4
5
6
int main() {
vector<int> v(8);
printVector(v);
return 0;

}
1
2
输出:
0 0 0 0 0 0 0 0

初始化为所需值

1
2
3
4
5
6
int main() {
vector<int> v(8,6);
printVector(v);
return 0;

}
1
2
输出:
6 6 6 6 6 6 6 6

拷贝构造函数

1
2
3
4
5
6
7
8
int main() {
vector<int> v(8,6);
printVector(v);
vector<int> v2(v);
printVector(v2);
return 0;

}
1
2
3
输出:
6 6 6 6 6 6 6 6
6 6 6 6 6 6 6 6
1
2
3
4
5
6
7
8
int main() {
vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
printVector(v);
vector<int> v2(v.begin()+2,v.end()-2);
printVector(v2);
return 0;

}
1
2
3
输出:
1 2 3 4 5 6 7 8 9 10
3 4 5 6 7 8

赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() {
// "="赋值
vector<int> v1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
printVector(v1);
//“迭代器”赋值
vector<int> v2;
v2.assign(v1.begin(), v1.end());
printVector(v2);
//初始化列表
vector<int> v3;
v3.assign({1,2,3,4,5,6,7,8,9});
printVector(v3);
//赋值a个b
vector<int> v4;
v4.assign(8,6);
printVector(v4);
}
1
2
3
4
5
输出:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
6 6 6 6 6 6 6 6

插入数据

1
2
3
4
5
6
7
8
9
10
11
12
int main() {
vector<int> v;
// push_back方法
for (int i = 0; i < 10; i++) {
v.push_back(i);
}
printVector(v);
// insert方法
v.insert(v.begin()+8, 888);
printVector(v);
}

1
2
3
输出:
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 888 8 9

删除数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() {
vector<int> v = {1,2,3,4,5,6,7,8,9};

//pop_back方法
v.pop_back();
printVector(v);
//erase方法
v.erase(v.begin()+4);
printVector(v);
//clear方法
v.clear();
v.push_back(1);
printVector(v);
}
1
2
3
4
输出:
1 2 3 4 5 6 7 8
1 2 3 4 6 7 8
1

扩容机制

1
2
3
4
5
6
7
8
9
10
int main() {
vector<int> v1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << "v1.size() = " << v1.size() << endl;
cout << "v1.capacity() = " << v1.capacity() << endl;
v1.push_back(11);
cout << "v1.size() = " << v1.size() << endl;
cout << "v1.capacity() = " << v1.capacity() << endl;
// clion编译器的扩容机制为超过现有长度后直接扩大2倍

}
1
2
3
4
5
6
输出:
v1.size() = 10
v1.capacity() = 10
v1.size() = 11
v1.capacity() = 20

随机访问

1
2
3
4
5
6
int main() {
vector<int> v1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << v1[2] << endl;
cout << v1.at(2) << endl;
}

1
2
3
输出:
3
3

内存交换

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
vector<int> v1 = {1, 2, 3, 4, 5};
vector<int> v2 = {6, 7, 8, 9, 10};
cout << "v1:" << endl;
printVector(v1);
cout << "v2:" << endl;
printVector(v2);
v1.swap(v2);
cout << "v1:" << endl;
printVector(v1);
cout << "v2:" << endl;
printVector(v2);
}
1
2
3
4
5
6
7
8
9
输出:
v1:
1 2 3 4 5
v2:
6 7 8 9 10
v1:
6 7 8 9 10
v2:
1 2 3 4 5
1
2
3
4
5
6
7
8
9
int main() {
vector<int> v1 = {1, 2, 3, 4, 5};
// 缩容
v1.resize(1000);
v1.resize(5);
cout << v1.capacity() << endl;
v1.swap(v1);
cout << v1.size() << endl;
}
1
2
3
输出:
1000
5

预留空间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main() {
vector<int> v;
for (int i = 0;i<100;i++) {
if (i%20==0)
cout<<"size: "<<v.size()<<","<<"capacity:"<<v.capacity()<<endl;
v.push_back(i);
}
v.clear();
v.reserve(150);
// 预留空间
printf("\n");
for (int i = 0;i<100;i++) {
if (i%20==0)
cout<<"size: "<<v.size()<<","<<"capacity:"<<v.capacity()<<endl;
v.push_back(i);
}
}

快速删除

只有在对顺序没有要求的时候才能使用

1
2
3
4
void remove1 (vector<int>& v,int index) { // 只有在对顺序没有要求的时候使用
swap(v[index],v.back());
v.pop_back();
}

string

定义字符串

1
2
3
4
5
int main() {
string s = "Hello World";
cout << s << endl;
return 0;
}
1
2
输出:
Hello World

操作方法

初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main() {
// 列表初始化
string s1({'h','e','l','l','o'});
cout << s1 << endl;
// 字符串初始化
string s2("world");
cout << s2 << endl;
// 字符串前n个字符
string s3("hello world",6);
cout << s3 << endl;
// 拷贝构造
string s4(s2);
cout << s4 << endl;
// a个字符b
string s5(8,'p');
cout << s5 << endl;

return 0;
}

赋值

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
int main() {
//常量赋值
string s1;
s1 = "hello";
cout << s1 << endl;
//字符串变量赋值
string s2;
s2 = s1;
cout << s2 << endl;
//接口调用赋值
string s3,s4,s5,s6;

s3.assign("world");
cout << s3 << endl;

s4.assign("helloworld",6);
cout << s4 << endl;

s5.assign(s4);
cout << s5 << endl;

s6.assign(4,'q');
cout << s6 << endl;
return 0;
}
1
2
3
4
5
6
7
输出:
hello
hello
world
hellow
hellow
qqqq

拼接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main() {
// '+'拼接
string s1 = "hello ";
string s2 = "world";
s1 += s2;
cout << s1 << endl;

//append 拼接
string s3 = "hello ";
string s4 = "world";
s3.append(s4);
s3.append("5201314",3,4);
cout << s3 << endl;

//push_back 拼接
string s5;
s5.push_back('5');
s5.push_back('6');
s5.push_back('7');
cout << s5 << endl;
return 0;
}
1
2
3
4
输出:
hello world
hello world1314
567

比较

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() {
// compare 方法
string s1 = "aab";
string s2 = "aab";
string s3 = "aaa";
string s4 = "aac";
cout << "s1 compare s2 ->" << s1.compare(s2) << endl;
cout << "s1 compare s3 -> " << s1.compare(s3) << endl;
cout << "s1 compare s4 ->" << s1.compare(s4) << endl;
// 符号方法
cout << "s1 == s2 ->" << (s1==s2) << endl;
cout << "s1 != s2 ->" << (s1!=s2) << endl;
return 0;
}
1
2
3
4
5
6
输出:
s1 compare s2 ->0
s1 compare s3 -> 1
s1 compare s4 ->-1
s1 == s2 ->1
s1 != s2 ->0

随机访问

1
2
3
4
5
6
7
8
9
10
11
12
int main() {
string s = "Hello World";
for (int i = 0; i < s.length(); i++) {
cout << s[i]<<" ";
}
cout << endl;
for (int i = 0; i < s.length(); i++) {
cout << s.at(i)<<" ";
}

return 0;
}
1
2
3
输出:
H e l l o W o r l d
H e l l o W o r l d

插入

1
2
3
4
5
6
7
8
9
10
11
int main() {
string s = "Heworld";
s.insert(2,2,'l');
cout << s << endl;
s.insert(4,"o ");
cout << s << endl;


return 0;
}

1
2
3
插入:
Hellworld
Hello world

删除

1
2
3
4
5
6
7
8
9
10
11
12
int main(){
string s = "Hello Wooooorld";
s.erase(7);
cout << s << endl;
s = "Hello Wooooorld";
s.erase(7,4);
cout << s << endl;
s = "Hello Wooooorld";
s.erase(s.begin()+7,s.end()-4);
cout << s << endl;
return 0;
}
1
2
3
4
输出:
Hello W
Hello World
Hello World

查找

1
2
3
4
5
6
7
8
int main(){
string s = "Hello Wooooorld";
cout << s.find("oooo") << endl;
cout << s.rfind("oo") << endl;
cout << (int)s.find("oooo",9) << endl;
cout << s.find('o',s.find('o')+1) << endl;
return 0;
}
1
2
3
4
5
输出:
7
10
-1
7

替换

1
2
3
4
5
6
7
8
9
int main(){
string s = "Hello Wooooorld";
s.replace(7,6,"or");
cout << s << endl;
s = "Hello Wooooorld";
s.replace(s.begin()+7,s.end()-2,"or");
cout << s << endl;
return 0;
}
1
2
3
输出:
Hello World
Hello World

字串获取

1
2
3
4
5
6
7
8
9
10
11
12
13
int main(){
string s = "Hello Wooooorld";
string subStr = s.substr(7,5);
cout<<subStr<<endl;
string s2 = "Hello&&world";
int pos = s2.find("&&");
string s3 = s2.substr(0,pos);
cout<<s3<<endl;
string s4 = s2.substr(pos+2,s.size()-pos-2);
cout<<s4<<endl;
return 0;
}

1
2
3
4
输出:
ooooo
Hello
world

deque

定义双端队列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main() {
deque <int> d1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
printDeque(d1);
deque <int> d2({1,2,3,4,5});
printDeque(d2);
deque <int> d3(d1.begin()+2, d1.end()-2);
printDeque(d3);
deque <int> d4(d1);
printDeque(d4);
deque <int> d5(10);
printDeque(d5);
deque <int> d6(6,8);
printDeque(d6);

return 0;
}
1
2
3
4
5
6
7
输出:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5
3 4 5 6 7 8
1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0
8 8 8 8 8 8

操作方法

赋值

与vector相同

长度

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
deque <int> d1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << "d1.size() = " << d1.size() << endl;
cout << "d1.empty() = " << d1.empty() << endl;
d1.resize(15);
cout << "d1.size() = " << d1.size() << endl;
printDeque(d1);
d1.resize(20,6);
printDeque(d1);

return 0;
}

1
2
3
4
5
6
输出:
d1.size() = 10
d1.empty() = 0
d1.size() = 15
1 2 3 4 5 6 7 8 9 10 0 0 0 0 0
1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 6 6 6 6 6

插入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main() {
deque <int> d1;
d1.push_front(1);
d1.push_front(2);
d1.push_front(3);

d1.push_back(-1);
d1.push_back(-2);
d1.push_back(-3);

d1.insert(d1.begin()+3,0);
printDeque(d1);

d1.insert(d1.begin()+3,5,0);
printDeque(d1);

d1.insert(d1.begin()+3,d1.begin()+4,d1.begin()+6);
printDeque(d1);

return 0;
}
1
2
3
4
输出:
3 2 1 0 -1 -2 -3
3 2 1 0 0 0 0 0 0 -1 -2 -3
3 2 1 0 0 0 0 0 0 0 0 -1 -2 -3

删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() {
deque <int> d1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
d1.pop_front();
d1.pop_back();
printDeque(d1);

d1.erase(d1.begin()+3);
printDeque(d1);
d1.erase(d1.begin()+2, d1.end()-2);
printDeque(d1);

return 0;
}

1
2
3
4
输出:
2 3 4 5 6 7 8 9
2 3 4 6 7 8 9
2 3 8 9

其他内容与vector大致相同

stack&&queue

stack 操作方法

创建

赋值

入栈&&出栈

获取栈顶

长度

容器替换

queue操作方法

创建

赋值

入队&&出队

获取队首&&队尾

长度

list

定义链表

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
#include <bits/stdc++.h>

using namespace std;

void printList(list<int> & v) {
for (list<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
int main() {
list<int> v;

list<int> v1 = {1,2,3};
list<int> v2({4,5,6});
list<int> v3(v2.begin(),v2.end());
list<int> v4(8);
list<int> v5(8,6);
list<int> v6(v1);

printList(v1);
printList(v2);
printList(v3);
printList(v4);
printList(v5);
printList(v6);
return 0;
}
1
2
3
4
5
6
7
输出:
1 2 3
4 5 6
4 5 6
0 0 0 0 0 0 0 0
6 6 6 6 6 6 6 6
1 2 3

操作方法

赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main() {
list<int> v = {1,2,3,4,5};

list<int> v1;
v1 = v;
printList(v1);

list<int> v2;
v2.assign(v1.begin(), v1.end());
printList(v2);

list<int> v3;
v3.assign({4,5,6});
printList(v3);

list<int> v4;
v4.assign(8,6);
printList(v4);
return 0;
}
1
2
3
4
5
输出:
1 2 3 4 5
1 2 3 4 5
4 5 6
6 6 6 6 6 6 6 6

长度

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
list<int> v = {1,2,3,4,5};

cout << "v.size() = " << v.size() << endl;
cout << "v.empty() = " << v.empty() << endl;

v.assign({2,4,6,8});
v.resize(10);
printList(v);
v.resize(15,6);
printList(v);
return 0;
}
1
2
3
4
5
输出:
v.size() = 5
v.empty() = 0
2 4 6 8 0 0 0 0 0 0
2 4 6 8 0 0 0 0 0 0 6 6 6 6 6

插入

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
int main() {
list<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);

v.push_front(-1);
v.push_front(-2);
v.push_front(-3);
printList(v);

list<int>::iterator it = v.begin();
++it;
++it;
++it;
v.insert(it, 0);
printList(v);

v.insert(it,3,0);
printList(v);

v.insert(it,v.begin(),v.end());
printList(v);

return 0;
}
1
2
3
4
5
输出:
-3 -2 -1 1 2 3
-3 -2 -1 0 1 2 3
-3 -2 -1 0 0 0 0 1 2 3
-3 -2 -1 0 0 0 0 -3 -2 -1 0 0 0 0 1 2 3 1 2 3

删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main() {
list<int> v({1,2,3,4,5,6,7,8,9,10});
v.pop_back();
printList(v);
v.pop_front();
printList(v);

list<int>::iterator it = v.begin();
it++;
it++;
v.erase(it);
printList(v);
it = v.begin();
it++;
v.erase(it,v.end());
printList(v);
return 0;
}
1
2
3
4
5
输出:
1 2 3 4 5 6 7 8 9
2 3 4 5 6 7 8 9
2 3 5 6 7 8 9
2

访问

1
2
3
4
5
6
7
8
9
int getlistIndex(list<int> & v,int n) {
list<int>::iterator it = v.begin();
while (it != v.end()) {
if (n <= 0) return *it;
it++;
n--;
}
return -191717;
}

链表反转

1
2
3
4
5
6
int main() {
list<int> v({1,2,3,5});
v.reverse();
printList(v);
return 0;
}
1
2
输出:
5 3 2 1

链表排序

1
2
3
4
5
6
int main() {
list<int> v({2,7,4,1,8,0});
v.sort();
printList(v);
return 0;
}
1
2
输出:
0 1 2 4 7 8

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
#include <bits/stdc++.h>

using namespace std;

void printSet(set<int>& s) {
for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
cout << *it << " ";
}
cout << endl;
}

int main() {
set<int> s = {9,3,1,7,1,4};
printSet(s);

set<int> s1({8,25,7,2,1,5});
printSet(s1);

set<int> s2(s1.begin(), s1.end());
printSet(s2);

set<int> s3(s);
printSet(s3);

return 0;
}
1
2
3
4
5
输出:
1 3 4 7 9
1 2 5 7 8 25
1 2 5 7 8 25
1 3 4 7 9

操作方法

赋值

1
2
3
4
5
6
7
8
9
10
int main() {
set<int> s = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

set<int> s1 = s;
printSet(s1);
set<int> s2;
s2 = {4,5,6,7,8,9,10};

return 0;
}
1
2
3
4
5
输出:
1 3 4 7 9
1 2 5 7 8 25
1 2 5 7 8 25
1 3 4 7 9

长度

1
2
3
4
5
6
7
8
int main() {
set<int> s = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

cout <<"s.size():"<< s.size() << endl;
cout <<"s.empty():"<< s.empty() << endl;

return 0;
}
1
2
3
输出:
10
0

插入

1
2
3
4
5
6
7
8
9
int main() {
set<int> s = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
s.insert(5);
s.insert(8);
s.insert(90);
printSet(s);

return 0;
}
1
2
输出:
1 2 3 4 5 6 7 8 9 10 90

查找

1
2
3
4
5
6
7
8
9
10
int main() {
set<int> s = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
set<int>::iterator it;
it = s.find(2);
cout << *it << endl;

int pos = s.count(2);
cout << pos << endl;
return 0;
}
1
2
3
输出:
2
1

删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main() {
set<int> s = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
set<int>::iterator it;
it = s.find(2);
s.erase(it);
printSet(s);

s.erase(4);
printSet(s);

s.erase(s.begin(), s.end());
printSet(s);

return 0;
}
1
2
3
4
输出:
1 3 4 5 6 7 8 9 10
1 3 5 6 7 8 9 10

排序规则

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
#include <bits/stdc++.h>

using namespace std;

typedef struct stu {
string name;
int age;
bool operator<(const stu& s) const {
if (age < s.age) {
return true;
}
return false;
}
}stu;

void printSet(set<stu>& s) {
for (set<stu>::iterator it = s.begin(); it != s.end(); it++) {
cout << it->name << " "<<it->age<<endl;
}

}

int main() {
set<stu> s;
s.insert({"lqf",19});
s.insert({"hsy",29});
s.insert({"opo",12});
printSet(s);
return 0;
}
1
2
3
4
输出:
opo 12
lqf 19
hsy 29

map

定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>

using namespace std;

void printMap(map<int, int> m) {
for (map<int,int>::iterator it = m.begin(); it != m.end(); it++) {
cout << it->first << " " << it->second << endl;
}
cout <<"----------------------"<<endl;
}


int main() {
map<int,int> m = {{1,10},{2,20},{3,30}};
printMap(m);

map<int,int> m1({pair<int,int>(6,60) ,pair<int,int>(5,50)});
printMap(m1);

return 0;
}
1
2
3
4
5
6
7
8
输出:
1 10
2 20
3 30
----------------------
5 50
6 60
----------------------

操作方法

赋值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main() {
map<int, int> m;
m = {pair<int, int>(1, 2),
pair<int, int>(2, 3),
pair<int, int>(3, 4),
pair<int, int>(4, 5)};
printMap(m);

map<int, int> m1;
m1= m;
printMap(m1);

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
输出:
1 2
2 3
3 4
4 5
----------------------
1 2
2 3
3 4
4 5
----------------------

长度

1
2
3
4
5
6
7
8
9
10
11
12
int main() {
map<int , int> mp = {
pair<int, int> { 1, 1 },
pair<int, int> { 2, 2 },
pair<int, int> { 3, 3 },
pair<int, int> { 4, 4 },
pair<int, int> { 5, 5 },
};
cout<<"mp.size() = "<<mp.size()<<endl;
cout<<"mp.empty() = "<<mp.empty();
return 0;
}
1
2
3
输出:
mp.size() = 5
mp.empty() = 0

插入

1
2
3
4
5
6
7
8
9
10
11
int main() {
map<int, int> m;
m.insert(pair<int, int>(1, 1));
m.insert(pair<int, int>(2, 2));
m.insert(pair<int, int>(3, 3));
m.insert(pair<int, int>(4, 4));
printMap(m);


return 0;
}
1
2
3
4
5
6
输出:
1 1
2 2
3 3
4 4
----------------------

查找

1
2
3
4
5
6
7
8
9
10
int main() {
map<int, int> m;
m[1] = 12;
m[2] = 13;
m[3] = 14;
map<int, int>::iterator it;
it = m.find(2);
cout << it->first << " " << it->second << endl;
return 0;
}
1
2
输出:
2 13

删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main() {
map<int , int> mp = {
pair<int, int> { 1, 1 },
pair<int, int> { 2, 2 },
pair<int, int> { 3, 3 },
pair<int, int> { 4, 4 },
pair<int, int> { 5, 5 },
};
mp.erase(1);
printMap(mp);
map<int,int>::iterator it = mp.begin();
it++;
it++;
mp.erase(it, mp.end());
printMap(mp);
return 0;
}

修改

1
2
3
4
5
6
7
8
9
10
11
12
13
int main() {
map<int , int> mp = {
pair<int, int> { 1, 1 },
pair<int, int> { 2, 2 },
pair<int, int> { 3, 3 },
pair<int, int> { 4, 4 },
pair<int, int> { 5, 5 },
};
mp[2] = 45;
printMap(mp);
return 0;
}

1
2
3
4
5
6
7
输出:
1 1
2 45
3 3
4 4
5 5
---------------------------------------------

统计

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
void findmapindex(map<int, int> mp) {
for (int i = -1; i < 4; i++) {
cout <<i<<" Appeared "<<mp.count(i)<<" times"<<endl;
}
cout<<"--------------------------------------------"<<endl;
}

void findmumapindex(multimap<int, int> mp) {
for (int i = -1; i < 4; i++) {
cout <<i<<" Appeared "<<mp.count(i)<<"times"<<endl;
}
cout<<"--------------------------------------------"<<endl;
}
int main() {
map<int , int> mp = {
pair<int, int> { 1, 1 },
pair<int, int> { 2, 2 },
pair<int, int> { 3, 3 },
pair<int, int> { 4, 4 },
pair<int, int> { 5, 5 },
};
findmapindex(mp);
multimap<int, int> mp2 = {
pair<int, int>{1, 1},
pair<int, int>{2, 2},
pair<int, int>{3, 3},
pair<int, int>{4, 4},
pair<int, int>{1, 1},
pair<int, int>{1, 1},
pair<int, int>{2, 2},
pair<int, int>{5, 5},
};
findmumapindex(mp2);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
输出:
-1 Appeared 0 times
0 Appeared 0 times
1 Appeared 1 times
2 Appeared 1 times
3 Appeared 1 times
--------------------------------------------
-1 Appeared 0times
0 Appeared 0times
1 Appeared 3times
2 Appeared 2times
3 Appeared 1times
--------------------------------------------

priority_queue

定义

操作方法

赋值

长度

插入

查找

删除

统计

unordered_set

定义

操作方法

赋值

长度

插入

查找

删除

统计

unordered_map

定义

操作方法

赋值

长度

插入

查找

删除

修改

统计