C++STL容器
发表于:2024-02-04 | 分类: C++

C++STL容器

vector容器

  • 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
35
36
37
38
#include<iostream>
#include<vector>
using namespace std;
//vector容器的构造
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
cout << *it << " ";
}
void test01()
{
vector<int>v1;//默认构造(无参)构造
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
//通过区间方式进行构造
vector<int>v2(v1.begin(), v1.end());
cout << endl;
printVector(v2);
cout <<endl;
//通过n个elem方式来进行构造
vector<int>v3(3, 100);// 第一个参数是个数,第二个参数是数据
printVector(v3);
cout << endl;
//拷贝构造
vector<int>v4(v3);
printVector(v4);
}

int main()
{
test01();
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<vector>
using namespace std;
//vector容器-赋值操作
void printvector(vector<int>&v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";

}
cout << endl;
}
void test01()
{
vector<int> v;//无参构造
for (int i = 0; i < 10; i++)
{
v.push_back(i);

}
printvector(v);
//赋值 operator=
vector<int>v2;
v2 = v;
printvector(v2);
//赋值 assign
vector<int>v3;
v3 .assign(v.begin(), v.end());//begin是闭区间,end是开区间
printvector(v3);
//赋值 assign n个elem方式赋值
vector<int>v4;
v4.assign(5, 100);
printvector(v4);


}
int main()
{
test01();
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<vector>
using namespace std;
//vector容器的容量和大小操作
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
printVector(v1);
if (v1.empty())//为真 代表容器为空
{
cout << "v1为空 " << endl;

}
else {
cout << "v1不为空 " << endl;
cout << "v1的容量为:" << v1.capacity() << endl;
cout << "v1的大小为: " << v1.size() << endl;
}
//重新指定大小
v1.resize(15, 4);//如果重新指定的size比原来大,则默认用0填充,若指定一个数,则会用指定数来填充
printVector(v1);
v1.resize(5);
printVector(v1);//如果重新定义的比原来短了,超出部分会删除掉
}
int main()
{
test01();
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
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
#include<iostream>
#include<vector>
using namespace std;
//vector插入和删除
/*·push_back(elem); //尾部插入元素elem
·pop_back(); //删除最后一个元素
·insert(const_iterator pos, elem); //迭代器指向位置pos插入元素elem
·insert(const_iterator pos, int count,elem); 迭代器指向位置pos插入count个元素elem
·erase(const_iterator pos); //删除迭代器指向元素
·erase(const_iterator start, const iterator end); //删除迭代器从start到end之间的元素
·clear(); //删除容器中所有元素
*/
void printVector(vector<int>&v)

{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int>v1;
//尾插法
v1.push_back(10);
v1.push_back(20);
v1.push_back(30);
v1.push_back(40);
v1.push_back(50);
v1.push_back(60);
//遍历
printVector(v1);
//尾删
v1.pop_back();
printVector(v1);
//插入 第一个参数为迭代器
v1.insert(v1.begin(), 100);
printVector(v1);
v1.insert(v1.begin(), 3, 500);
printVector(v1);
//删除 第一个参数也是迭代器
v1.erase(v1.begin());
printVector(v1);
v1.erase(v1.begin(), v1.begin() + 2);//删除begin到begin+2之间的元素
printVector(v1);
//清空
v1.clear();//删除容器中所以元素
printVector(v1);


}
int main()
{
test01();
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
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<vector>
using namespace std;
//vector容器 数据存取
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
//利用[]方式访问数组中元素
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << " ";
}
cout << endl;
//利用成员函数at来访问元素
for (int i = 0; i < v1.size(); i++)
{
cout << v1.at(i) << " ";
}
cout << endl;
//获取第一个元素
cout << "第一个元素为:" << v1.front() << endl;
//获取最后一个元素
cout << "最后一个元素为:" << v1.back() << endl;

}
int main()

{
test01();
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
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
#include<iostream>
#include<vector>
using namespace std;
//vector 互换容器
//1.基本使用
void printVector(vector<int>& v)
{
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
vector<int>v1;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
}
cout << "交换前的打印:" << endl;
printVector(v1);
vector<int>v2;
for (int i = 10; i > 0; i--)
{
v2.push_back(i);
}
printVector(v2);
cout << "交换后的打印:" << endl;
v1.swap(v2);
printVector(v1);
printVector(v2);


}
//2.实际用途
//巧用swap可以收缩内存空间
void test02()
{
vector<int>v;
for (int i = 0; i < 100000; i++)
{
v.push_back(i);
}
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
v.resize(3);//重新指定大小
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;
//巧用swap收缩内存
vector<int>(v).swap(v);
//vector<int>(v);//匿名对象
cout << "v的容量为:" << v.capacity() << endl;
cout << "v的大小为:" << v.size() << endl;


}
int main()
{
test01();
test02();
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
26
#include<iostream>
#include<vector>
using namespace std;
//vector容器-预留空间
void test01()
{
vector<int>v1;
//利用reserve预留空间
v1.reserve(100000);
int num=0;//统计开辟次数
int* p = NULL;
for (int i = 0; i < 100000; i++)
{
v1.push_back(i);
if (p != &v1[0]) {
p = &v1[0];//每开辟一次新的内存空间,地址发生变化
num++;
}
}
cout << "num = " << num << endl;
}
int main()
{
test01();
return 0;
}

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
#include<iostream>
#include<map>
using namespace std;
//map容器 构造和赋值

void printmap(map<int, int>& mp)
{
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
{
cout << "key = " << it->first << " " << "value = " << it->second << endl;
}
}
void test01()
{
//创建map容器
map<int, int> mp;
mp.insert(pair<int, int>(1, 10));
mp.insert(pair<int, int>(2, 20));
mp.insert(pair<int, int>(3, 30));
mp.insert(pair<int, int>(4, 40));
mp.insert(pair<int, int>(5, 50));
//打印输出
printmap(mp);
cout << endl;
//拷贝构造
map<int, int>mp2(mp);
printmap(mp2);
cout << endl;
//赋值
map<int, int>mp3;
mp3 = mp2;
printmap(mp3);
}
int main()
{

test01();
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
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
#include<iostream>
#include<map>
using namespace std;
//map容器大小和交换

void printmap(map<int, int>& mp)
{
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
{
cout << "key = " << it->first << " " << "value = " << it->second << endl;
}
}
void test01()
{
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));
m.insert(pair<int, int>(5, 50));
if (m.empty())
{
cout<<"m为空 !"<<endl;
}
else
{
cout << "m不为空!" << endl;
cout << "m的大小为:" << m.size() << endl;
}


}
//交换
void test02()
{
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));
m.insert(pair<int, int>(5, 50));

map<int, int>m2;
m2.insert(pair<int, int>(6, 10));
m2.insert(pair<int, int>(7, 20));
m2.insert(pair<int, int>(8, 30));
m2.insert(pair<int, int>(9, 40));
m2.insert(pair<int, int>(10, 50));
cout << "交换前:" << endl;
printmap(m);
cout << endl;
printmap(m2);
cout << "-----------------------" << endl;
cout << "交换后:" << endl;
m.swap(m2);
printmap(m);
cout << endl;
printmap(m2);

}
int main()
{
test02();
//test01();
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<map>
using namespace std;
//map容器 插入和删除
void printmap(map<int, int>& mp)
{
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
{
cout << "key = " << it->first << " " << "value = " << it->second << endl;
}
}
void test01()
{
map<int, int>m;
//插入
//第一种
m.insert(pair<int, int>(1, 10));
//第二种
m.insert(make_pair(2, 20));
//第三种
m.insert(map<int, int>::value_type(3, 30));
//第四种
//[]不建议插入,用途 可以利用key访问到value
m[4] = 40;
printmap(m);
cout << endl;
//删除
m.erase(m.begin());
printmap(m);
cout << endl;

m.erase(3);//按照key删除
printmap(m);
cout << endl;

m.erase(m.begin(), m.end());
printmap(m);
cout << endl;

m.clear();
}


int main()
{
test01();
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
26
27
28
29
30
31
32
#include<iostream>
#include<map>
using namespace std;
//map容器 查找和统计
void test01()
{
//查找
map<int, int>m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(3, 30));
map<int, int>::iterator pos = m.find(3);
if (pos != m.end())
{
cout << "找到了元素 key = " << (*pos).first << " " << "value = " << pos->second << endl;

}
else
{
cout << "未找到该元素!" << endl;
}
//统计
//map不允许插入重复的key元素, count统计而言,结果要么为1 要么为0
//multimap的count统计可能大于1
int num = m.count(3);
cout << " num = " << num << endl;
}
int main()
{
test01();
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
26
27
28
29
30
31
32
33
#include<iostream>
#include<map>
using namespace std;
class myCompare
{
public:
bool operator()(int v1,int v2)const
{
//降序
return v1 > v2;
}
};
//map容器 排序
void test01()
{
map<int, int,myCompare>m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(4, 40));
m.insert(make_pair(3, 30));
m.insert(make_pair(5, 50));
m.insert(make_pair(6, 60));
for (map<int, int,myCompare>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key = " << it->first << " " << " value = " << it->second << endl;
}

}
int main()
{
test01();
return 0;
}

stack容器

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
#include<iostream>
#include<stack>
using namespace std;
//栈容器
void test1()
{
//特点:符合先进后出数据结构
stack<int>s;
//入栈
s.push(10);
s.push(20);
s.push(30);
s.push(40);
//只要栈不为空,查看栈顶,并且执行出栈操作
while (!s.empty())
{
//查看栈顶元素
cout << "栈顶元素为:" << s.top() <<" ";
cout << "栈的大小为:" << s.size() << endl;
//出栈
s.pop();
}
cout << "栈的大小为:" << s.size() << endl;


}
int main()
{
test1();



return 0;
}

queue容器

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
#include<iostream>
#include<queue>
#include<string>
using namespace std;
// 队列 queue容器 front队头 back队尾
class person
{
public:
person(string name, int age)
{
this->m_age = age;
this->m_name = name;
}
string m_name;
int m_age;
};
void test01()
{
//创建队列
queue<person>q;
//准备数据
person p1("唐僧", 30);
person p2("孙悟空",100);
person p3("猪八戒", 200);
person p4("沙僧", 300);
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
//判断队列是否为空,查看队头,查看队尾,出队
while (!q.empty())
{
//查看队头
cout << "队头元素-姓名:" << q.front().m_name << "年龄:" << q.front().m_age << endl;
//查看队尾
cout << "队尾元素-姓名:" << q.back().m_name << "年龄:" << q.back().m_age << endl;
//查看队中元素个数
cout << "当前队列中元素个数为:" << q.size() << endl;
cout << "-------------------------------------------------------" << endl;
//出队
q.pop();
}


}
int main()
{
test01();

return 0;
}

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
#include<iostream>
#include<set>
using namespace std;
//set构造以及赋值
void print_set(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
set<int>s1;
//插入数据只有insert方式
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(50);
s1.insert(60);
s1.insert(40);
//打印输出
//set容器特点,所有元素插入时自动排序
//set容器不允许插入重复值
print_set(s1);
//拷贝构造
set<int>s2(s1);
print_set(s2);
//赋值
set<int>s3;
s3 = s2;
print_set(s3);
}
int main()
{
test01();
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
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
#include<iostream>
#include<set>
using namespace std;
//set容器大小和交换

void print(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
}
//大小
void test01()
{
set<int>s1;
//插入数据
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
s1.insert(70);
s1.insert(50);

//打印
print(s1);
if (s1.empty())
{
cout << "s1为空" << endl;
}
else cout << "s1不为空" << endl;
cout << s1.size() << endl;
}

//交换
void test02()
{
set<int>s1;
//插入数据
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
s1.insert(70);
s1.insert(50);
set<int>s2;
//插入数据
s2.insert(30);
s2.insert(40);
s2.insert(50);
s2.insert(60);
s2.insert(70);
s2.insert(90);
cout << "交换前:" << endl;
print(s1);
cout << endl;
print(s2);
cout << endl;
cout << endl;
cout << "交换后:" << endl;
s1.swap(s2);
print(s1);
cout << endl;
print(s2);
}
int main()
{
//test01();
test02();
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
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
#include<iostream>
#include<set>
using namespace std;
//set容器大小和交换

void print(set<int>& s)
{
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
}
//大小
void test01()
{
set<int>s1;
//插入数据
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
s1.insert(70);
s1.insert(50);

//打印
print(s1);
if (s1.empty())
{
cout << "s1为空" << endl;
}
else cout << "s1不为空" << endl;
cout << s1.size() << endl;
}
//交换
void test02()
{
set<int>s1;
//插入数据
s1.insert(10);
s1.insert(20);
s1.insert(30);
s1.insert(40);
s1.insert(70);
s1.insert(50);
set<int>s2;
//插入数据
s2.insert(30);
s2.insert(40);
s2.insert(50);
s2.insert(60);
s2.insert(70);
s2.insert(90);
cout << "交换前:" << endl;
print(s1);
cout << endl;
print(s2);
cout << endl;
cout << endl;
cout << "交换后:" << endl;
s1.swap(s2);
print(s1);
cout << endl;
print(s2);
}
int main()
{
//test01();
test02();
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
#include<set>
using namespace std;
//set查找和统计

void test01()
{
set<int>s1;
//插入
s1.insert(10);
s1.insert(30);
s1.insert(69);
s1.insert(40);
s1.insert(50);
//查找
set<int>::iterator pos = s1.find(30);
if (pos != s1.end())
{
cout << "找到元素:" << *pos << endl;

}
else
{
cout << "未找到元素" << endl;
}

}
//统计
void test02()
{
set<int>s1;
//插入
s1.insert(10);
s1.insert(30);
s1.insert(69);
s1.insert(40);
s1.insert(50);
int num = s1.count(30);
cout << "num=" << num;
//对于set而言 统计结果要么为1 要么为0
}
int main()
{


test01();
test02();
return 0;
}

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
#include<iostream>
#include<deque>
//deque构造函数
using namespace std;
void print(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
//容器里的数据不可以修改,只读
cout << *it << " ";
}
cout << endl;

}
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
print(d1);
deque<int>d2(d1.begin(), d1.end());
print(d2);
deque<int>d3(10, 100);
print(d3);
deque<int>d4(d3);
print(d4);
}
int main()
{
test01();
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
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<deque>
//deque容器赋值操作
using namespace std;
void print(const deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
print(d1);
//operator = 赋值
deque<int>d2;
d2 = d1;
print(d2);
//assign 赋值
deque<int>d3;
d3.assign(d2.begin(), d2.end());
print(d3);
deque<int>d4;
d4.assign(10, 100);
print(d4);
}
int main()
{
test01();
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<deque>
using namespace std;
//deque容器 大小操作
void print(const deque<int>&d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)d1.push_back(i);
print(d1);
if (d1.empty())
{
cout << "d1为空" << endl;
}
else
{
cout << "d1不为空" << endl;
cout << "d1的大小为:" << d1.size() << endl;
//deque容器没有容量概念
}
//重新指定容器大小
d1.resize(15,1);
print(d1);
d1.resize(5);
print(d1);


}
int main()
{

test01();
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
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<deque>
using namespace std;
//deque容器插入和删除
void print(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
//两端操作
void test01()
{
deque<int>d1;
//尾插
d1.push_back(10);
d1.push_back(20);
//头插
d1.push_front(5);
d1.push_front(4);
//
print(d1);
//尾删
d1.pop_back();
//头删
d1.pop_front();
print(d1);
}
//对指定位置操作
void test02()
{
deque<int>d2;
d2.push_back(10);
d2.push_back(20);
d2.push_front(5);
d2.push_front(4);
print(d2);
//insert插入
d2.insert(d2.begin(), 1000);
print(d2);
d2.insert(d2.begin() + 1, 2, 500);
print(d2);

//按照区间进行插入
deque<int>d3;
d3.push_back(1);
d3.push_back(2);
d3.push_back(3);
d3.push_back(4);
d3.push_back(5);
d3.insert(d3.begin(), d2.begin(), d2.end());
print(d3);
}
//删除
void test03()
{
deque<int>d1;
d1.push_back(10);
d1.push_back(20);
d1.push_front(5);
d1.push_front(4);
print(d1);
d1.erase(d1.begin());
print(d1);
deque<int>::iterator it = d1.begin();
it++;
d1.erase(it);
print(d1);
//按照区间的方式删除
d1.erase(d1.begin(), d1.end());
d1.clear();
print(d1);
}
int main()
{
test01();
test02();
test03();
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
26
27
28
29
30
#include<iostream>
#include<deque>
//deque容器数存取
using namespace std;
void test01()
{
deque<int>d1;
for (int i = 0; i < 10; i++)
{
d1.push_back(i);
}
//通过[]方式访问元素
for (int i = 0; i < d1.size(); i++)
{
cout << d1[i] << " ";
}
cout << endl;
//通过at方式访问元素
for (int i = 0; i < d1.size(); i++)
{
cout << d1.at(i) << " ";
}
cout << endl;
cout << d1.front() << " " << d1.back() << endl;
}
int main()
{
test01();
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
26
27
28
29
30
31
32
33
34
35
36
37
38
#include<iostream>
#include<deque>
#include<algorithm>//标准算法头文件
//deque排序
using namespace std;
void print(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
deque<int>d1;
d1.push_back(20);
d1.push_back(50);
d1.push_back(40);
d1.push_back(70);
d1.push_front(30);
d1.push_front(60);
d1.push_front(100);
cout << "排序前的容器:" << endl;
print(d1);
cout << "排序后的容器:" << endl;
//排序 默认排序规则是从小到大
//对于支持随机访问的迭代器的容器,都可以利用sort算法直接对其进行排序
//vector容器也可以利用sort进行排序
sort(d1.begin(), d1.end());
print(d1);
}
int main()
{

test01();
return 0;
}

priority_queue容器

优先队列,其底层是基于堆来进行实现的,在优先队列中,队首元素一定是当前队列中优先级最高的那一个

1.priority_queue的定义

1
priority_queue<typename> name;

2.priority_queue容器内元素的访问

只能通过top()函数来访问队首元素(堆顶元素),时间复杂度为O(1),也就是优先级最高的元素,通过push()函数入队,其时间复杂度为O(logN),通过pop()函数弹出队首元素出队,时间复杂度为O(logN)。empty()函数判断优先队列是否为空,返回true为空,返回false为非空。size()函数返回优先队列内元素的个数,时间复杂度为O(1);

3.priority_queue内元素优先级的设置

(1)基本数据类型的优先级设置

1
2
3
priority_queue<int>q;
priority_queue<int,vector<int>, less<int> >q;
priority_queue<int,vector<int>, greater<int> >q;

第二个参数vector参数填写的是承载底层数据结构堆(heap)的容器,第三个参数less则是对第一个参数的比较类,less表示数字大的优先级大,greater表示数字小的优先级大。

(2)结构体的优先级设置

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
#include<iostream>
#include<cmath>
#include<queue>
using namespace std;
struct fruit
{
string name;
int price;
}f1, f2, f3;
struct cmp
{

bool operator() (const fruit& a,const fruit& b)
{
return a.price < b.price;
}
};
int main()
{
priority_queue<fruit, vector<fruit>, cmp> q;
f1.name = "桃子";
f1.price = 15;
f2.name = "苹果";
f2.price = 20;
f3.name = "菠萝";
f3.price = 12;
q.push(f1);
q.push(f2);
q.push(f3);
cout << q.top().name << " " << q.top().price << endl;
return 0;
}

pair容器

pair实际上可以看作一个内部含有两个元素的结构体

1
2
3
4
struct pair{
typename first;
typename second;
};

1.pair的定义,要使用pair要添加头文件#include,由于map的内部涉及pair,故添加map头文件也能使用pair.

1
pair<typename1, typename2> name;

2.pair中元素的访问

pair中只有两个元素,分别是frist和second,只需要按照正常结构体方式去访问即可。

3.pair常用函数

比较操作数

两个pair类型数据进行比较可以直接使用==,!=,>,<比较大小,比较规则是先以first的大小作为标准,只有当first相等时才会去判别second的大小。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<utility>
using namespace std;
int main()
{
pair<int,int>p1(5,10);
pair<int,int>p2(5,22);
pair<int,int>p3(3,20);
cout<<(p1<p2?"p1<p2":"p1>=p2")<<endl;
cout<<(p1<p3?"p1<p3":"p1>=p3")<<endl;


return 0;
}

algorithm头文件下的常用函数

1.max(),min(),abs()

max(x,y)和min(x,y)分别返回x与y中的最大值与最小值,且参数必须是两个(可以是浮点数),abs(x)返回x的绝对值。且x必须是整数,浮点数的绝对值采用fabs()。

2.swap()

swap(x,y)用来交换x与y的值。

3.reverse(it,it2)可以将数组指针在[it,it2)之间的元素或者容器的迭代器在[it,it2)范围内的元素进行反转。

4.next_permutation()

next_permutation()给出一个序列在全序列中的下一个序列。

1
2
3
4
5
6
int arr[4]={1,2,3,4};
do
{
cout<<arr[0]<<arr[1]<<arr[2]<<arr[3]<<endl;
}while(next_permutation(arr,arr+4));

5.fill()

fill()可以把数组或者容器中的某一段区间赋值某个相同的值。和memset不同,这里的赋值可以是数组类型对应范围中的任意值。

6.sort()

sort(首元素地址,尾元素地址的下一个地址,比较函数)对数据进行排序

7.lower_bound()和upper_bound()

lower_bound和upper_bound需要用在一个有序数组或者容器中

lower_bound(first,last,val)用来寻找在数组或者容器的[first,last)范围内第一个值大于等于val的元素的位置

upper_bound(first,last,val)用来寻找在数组或者容器的[first,last)范围内第一个值大于val的元素的位置

1
2
3
4
5
int arr[10]={1,2,3,4,5,6,7,8,9,10};
int *lowerPos=lower_bound(arr,arr+10,2);
int *upperPos=upper_bound(arr,arr+10,2);
cout<<"第一个大于等于2的元素的位置为:"<<lowerPos<<endl;
cout<<"第一个大于2的元素的位置为:"<<upperPos<<endl;
上一篇:
Java图形可视化编程
下一篇:
动态规划