STL的学习

vector的学习

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
void Print(int val){
cout<<val<<endl;
}
//vector的学习使用
void test01(){
//创建一个vector容器
vector<int> v;
//容器添加数据
v.push_back(0);
v.push_back(1);
v.push_back(2);
v.push_back(3);
//迭代器遍历容器
vector<int>::iterator itBegin = v.begin();//指向第一个迭代器
vector<int>::iterator itEnd = v.end();//指向最后一个迭代器的后面一个


```C++
//while循环遍历
while(itBegin != itEnd){
cout<<*itBegin<<endl;//指针访问
itBegin++;
}

//for循环遍历
for(vector<int>::iterator it = v.begin(); it!=v.end();it++){
cout<<*it<<endl;
}

//for_each循环遍历
for_each(v.begin(),v.end(),Print);//初始位置,末尾位置,回调函数
```

}
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
//vector存放自定义类型数据
class Person{
public:
Person(string name, int age){
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};

void test02(){
//新建一个容器
vector<Person>v;
//新建person
Person p1("aaa",18);
Person p2("bbb",28);
// 容器中添加数据Person数据
v.push_back(p1);
v.push_back(p2);
//循环遍历
for(vector<Person>::iterator it = v.begin();it!=v.end();it++){
cout<<(*it).m_name<<" "<<(*it).m_age<<endl;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void test02(){
vector< vector<int> >v;
vector<int> v1;
vector<int> v2;
vector<int> v3;
for(int i = 0; i<4; i++){
v1.push_back(i);
v2.push_back(i+1);
v3.push_back(i+2);
}
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
for(vector< vector<int> >::iterator it = v.begin(); it != v.end(); it++){
for(vector<int>:: iterator st = (*it).begin(); st!=(*it).end();st++){
cout<<*st<<" ";
}
cout<<endl;
}
}
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
vector<int> v;
for(int i = 0; i < 4; i++){
v.push_back(i);
}
vector<int> v1(v.begin(),v.end());
vector<int> v2(10,5);
for(vector<int>::iterator it = v2.begin(); it != v2.end(); it++){
cout<<*it<<endl;
}


vector的empty(),size(),capacity(),resize()的使用
vector<int> v;
for(int i = 0; i<17 ;i++){
v.push_back(i);
}
v.resize(18,1000);
if(v.empty()){
cout<<"容器为空"<<endl;
}else{
cout<<"容器不为空"<<endl;
cout<<"容器的大小: "<<v.size()<<endl;
cout<<"容器的容量: "<<v.capacity()<<endl;
}
for(vector<int>::iterator it = v.begin(); it != v.end(); it++){
cout<<*it<<endl;
}
cout<<"---------------------------------------"<<endl;
vector<int> vm;
vm.push_back(2100);
vm.push_back(2900);
vm.swap(v);
for(vector<int>::iterator it = v.begin(); it != v.end(); it++){
cout<<*it<<endl;
}
vector<int> v;
//预留空间
v.reserve(100000);
int num = 0;
int* p = NULL;
for (int i = 0; i < 100000; i++) {
v.push_back(i);
if (p != &v[0]) {
p = &v[0];
num++;
}
}
cout << "num:" << num << endl;

string的学习

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
string str5;
str5.assign("hello c++",7);
cout << "str5 = " << str5 << endl;
string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl;
string str7;
str7.assign(5, 'x');
cout << "str7 = " << str7 << endl;
//添加字符串(两种方式)
string str = "abcdefg";
str += "hi";
str.append("jk");
cout<<str<<endl;
//从左往右查找
int pos1 = str.find("jk");
cout<<pos1<<endl;
//从右往左查找
int pos2 = str.rfind("jk");
cout<<pos2<<endl;
//替换函数
str.replace(0,2,"你好");
cout<<str<<endl;
//比较函数
/**
字符串比较是按字符的ASCII码进行对比
= 返回 0
> 返回 1
< 返回 -1
**/
string s1 = "abcdef";
string s2 = "hbcdef";
string s3 = "abcdef";
int result1 = s1.compare(s2);
int result2 = s1.compare(s3);
cout<< result1 << " "<<result2<<endl;
//string单个字符存取
cout<<s1[2]<<" : "<<s1.at(2)<<endl;
//string的插入删除
string st = "aceg";
st.insert(1,"b");
cout<<st<<endl;
st.insert(3,1,'d');
cout<<st<<endl;
st.erase(4,6);
cout<<st<<endl;
//string子串
string test = st.substr(0,2);
cout<<test<<endl;

deque的学习

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
	//定义一个双端数组 
deque<int> d1;
//添加元素
for(int i = 0; i < 10; i++){
d1.push_back(i);
}
//把d1复制到d2
deque<int> d2(d1.begin(),d1.end());
//迭代器遍历双端数组
// for(deque<int>::iterator it = d2.begin(); it!=d2.end();it++){
// cout<<*it<<endl;
// }
//构造函数将n个elem拷贝给本身
deque<int> d3(10,156);
// for(deque<int>::iterator it = d3.begin(); it!=d3.end(); it++){
// cout<<*it<<endl;
// }
//拷贝构造函数
deque<int> d4 = d3;
// for(deque<int>::iterator it = d4.begin(); it!=d4.end(); it++){
// cout<<*it<<endl;
// }

//deque的empty(),size(),resize()
cout<< d4.empty();
if(d4.empty()){
cout<<"deque为空"<<endl;
}
else{
cout<<"deque不为空"<<endl;
cout<<"deque的元素个数: "<<d4.size()<<endl;
}
//push_back(elem);
d1.push_back(10078);
//push_front(elem);
d1.push_front(78007);
cout<<"删除前:"<<endl ;
for(deque<int>::iterator it = d1.begin(); it!=d1.end();it++){
cout<<*it<<endl;
}
cout<<"删除后:"<<endl;
d1.pop_back(); //删除容器最后一个数据
d1.pop_front();//删除容器第一个数据
for(deque<int>::iterator it = d1.begin(); it!=d1.end();it++){
cout<<*it<<endl;
}
cout<<"insert执行后"<<endl;
//insert的用法
d1.insert(d1.begin(),10000);
d1.insert(d1.begin(),2,560);
for(deque<int>::iterator it = d1.begin(); it!=d1.end();it++){
cout<<*it<<" ";
}
//erase的用法
cout<<endl;
cout<<"erase执行后"<<endl;
d1.erase(d1.begin());
// d1.erase(d1.begin(),d1.end());
// d1.clear();
for(deque<int>::iterator it = d1.begin(); it!=d1.end();it++){
cout<<*it<<" ";
}

//数据存取
cout<<endl;
cout<<"at的使用: "<<d1.at(2)<<endl;
cout<<"front的使用:"<<d1.front()<<endl;
cout<<"back的使用:"<<d1.back()<<endl;

//排序
cout<<endl;
cout<<"排序后的结果: "<<endl;
sort(d1.begin(),d1.end());
for(deque<int>::iterator it = d1.begin(); it!=d1.end();it++){
cout<<*it<<" ";
}

list的学习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
	list<int> l1;
list<int> l2;
for(int i=0;i<5;i++){
l1.push_back(i);
}
for(int i=5;i<10;i++){
l2.push_back(i);
}
// l1.swap(l2);
for(list<int>::iterator it = l1.begin(); it!=l1.end();it++){
cout<<*it<<endl;
}
cout<<endl;
cout<<"删除后"<<endl;
list<int>::iterator it = l1.begin();
for(int i=0;i<2;i++)l1.insert(it,1000);
// l1.erase(++it);
// l1.remove(1000);
// l1.reverse();
l1.sort(compare);
for(list<int>::iterator it = l1.begin(); it!=l1.end();it++){
cout<<*it<<" ";
}

set的学习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
set<int> s;
for(int i = 1; i < 5 ; i++)s.insert(i);
for(set<int>::iterator it = s.begin(); it!=s.end(); it++){
cout<<*it<<" ";
}
cout<<endl;
s.insert(0);
for(set<int>::iterator it = s.begin(); it!=s.end(); it++){
cout<<*it<<" ";
}
cout<<endl;
//find函数
set<int>::iterator pos = s.find(3);
if(pos!=s.end()){
cout<<*pos<<endl;
} else{
cout<<"该数不存在"<<endl;
}
int sum = s.count(1);
cout<<"sum = "<<sum<<endl;

map的学习

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
map<int,int> m;
m.insert(pair<int,int>(1,10));
m.insert(pair<int,int>(2,20));
m.insert(pair<int,int>(3,30));
m.insert(pair<int,int>(4,40));
cout<<"没有插入删除前"<<endl;
for(map<int,int>::iterator mt = m.begin(); mt!=m.end(); mt++){
cout<<"map的first: "<<mt->first<<" map的second: "<<mt->second<<endl;
}
m.insert(make_pair(5,50));
m[6] = 60;
m.erase(m.begin());
cout<<"插入删除后"<<endl;
for(map<int,int>::iterator mt = m.begin(); mt!=m.end(); mt++){
cout<<"map的first: "<<mt->first<<" map的second: "<<mt->second<<endl;
}
//count()
cout<<"key的个数: "<<m.count(0)<<endl;
//find()
cout<<"查找key: "<<m.count(5)<<endl;