容器

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
#include
#include
#include
using namespace std;
class Person{
friend void test();
private:
int age;
string name;
public:
Person(int age,string name){
this->age=age;
this->name=name;
}
};
void test(){
Person p1(12,"java");
Person p2(13,"c++");
Person p3(1,"c");
vector v;
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3);
for(vector::iterator it=v.begin();it!=v.end();it++){
cout<<(*it)->age<<(*it)->name<<endl;
}
}
int main(){
test();
}

string容器

image-20191219202604459

string赋值

image-20191219203111427

string拼接

image-20191219203050386

容器嵌套容器

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
#include
#include
using namespace std;

void test(){
vector<vector<int> >v; //这里右边>>必须分开写
vector<int> v1;
vector<int> v2;
for(int i=0;i<4;i++){
v1.push_back(i);
v2.push_back(i+1);
}
v.push_back(v1);
v.push_back(v2);
for(vector<vector<int> >::iterator it = v.begin();it!=v.end();it++){
// *it 指的是vector
//再次遍历
for(vector<int>::iterator vit=(*it).begin();vit!=(*it).end();vit++){
cout<<*(vit)<<" ";
}
cout<<endl;
}
}

int main(){
test();
}

string字符串拼接

image-20200321163725406

string 查找和替换

image-20200321163810944

注意替换replace(),三个参数是指从第几个开始,替换几个,会将新的字符串全部加进去

1
2
3
string str="cd";
str.replace(1,3,"dasdddas")
// cdasdddas
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  
using namespace std;

void test(){
string str="hello";
str.append("world"); //添加
str+="i";
string str1="cdwewewf";
str1.replace(1,3,"das"); //字符替换
cout<
string str3="hello";
string str2="xello" ;
if(str3.compare(str2)==0){ //字符串比较
cout<<"相等"<<endl;
}
else{
cout<<"不相等"<<endl;
}

//string字符存取
string str4="hello";
//访问字符
for(int i=0;isize();i++){

cout<" ";
}
cout<<endl;
for(int i=0;isize();i++){
cout<" ";
}
cout<<endl;
// string 插入删除
// insert()
// erase(int pos,int n=npos);
str2.erase(1,2);
cout<endl;
// 子串
// substr(int pos,int n=npos)
string str5=str3.substr(2,3);
cout<endl;

string email = "zhangsan@sina.com"; //截取邮箱
int pos = email.find("@");
cout<0,pos)<<endl;
}
int main(){
test();

}

vector 容器 动态扩展

image-20200321214940482

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
#include

#include

using namespace std;

void printVector(vector<int> &v){
for(vector<int>::iterator it=v.begin();it!=v.end();it++){
cout<<*it<<" ";
}
}
void test(){
vector<int> v1;
for(int i=0;i<4;i++){
v1.push_back(i);
}
printVector(v1);
vector<int> v2;
v2=v1; //拷贝构造
printVector(v2);
vector<int> v3;
v3.assign(v1.begin(),v1.end());
printVector(v3);
vector<int> v4;
v4.assign(10,100); //赋值
printVector(v4);
}

int main(){
test();
}

vector容量和大小

image-20200321220704981

插入和删除

image-20200321211635313

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

#include

using namespace std;


void printVector(vector<int>&v){
for(vector<int>::iterator it=v.begin();it!=v.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}
void test(){
vector<int> v1;
cout<endl;// 为空的时候 容量为0
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
cout<endl;//
// 插入和删除
// 尾删
printVector(v1);
v1.pop_back();
printVector(v1);
// insert()
v1.insert(v1.begin(),100);
printVector(v1);
v1.insert(v1.begin(),2,1000);
printVector(v1);
v1.erase(v1.begin());
printVector(v1);
v1.erase(v1.begin(),v1.end()); //相等于清空
printVector(v1);
v1.clear(); //清空
}
int main(){
test();
}

image-20200321213530726

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
#include
#include
using namespace std;

void printVector(vector<int> &v1){
for(vector<int>::iterator it=v1.begin();it!=v1.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}
void test(){
vector<int> v1;
vector<int> v2;
for(int i=0;i<10;i++){
v1.push_back(i);
}
for(int i=10;i>0;i--){
v2.push_back(i);
}
//cout<<*v1<
// 数组方式遍历vector
for(int i=0;isize();i++){

cout<" ";
}
cout<<endl;
for(int i=0;isize();i++){
cout<" ";
}
cout<<endl;
// 获取第一个元素
cout<endl;
//互换容器
v1.swap(v2);
printVector(v1);
printVector(v2);
// 实际用途,收缩内存空间
vector<int> v4;

//使用reserve预留空间,可以减少内存开辟次数
v4.reserve(100000);
int num=0;
int *p=NULL;
for(int i=0;i<100000;i++){
v4.push_back(i);
if(p!=&v4[0]){
p=&v4[0];
num++;
}
}
cout<endl; //num 用来统计开辟了多少次内存 18 次
cout<endl;
v4.resize(3);
vector<int>(v4).swap(v4); //匿名对象 ,回收内存
cout<endl;
// 预留空间

}

int main(){
test();
}

deque 双端数组

与vector的区别

vector对于头部的插入删除效率低,数据量越大,效率越低

deque相对而言,对头部的插入删除速度比vector快

vector访问元素的速度会比deque快

deque内部工作原理,deque内部有中控器,维护每段缓冲区的内容,缓冲区中存放真实数据

中控器维护的是每一个缓冲区的地址,是的每个deque像一片连续的地址

image-20200321220121820

注意: deque没有容量的概念

判断为空—empty

返回元素个数—-size

重新指定个数—-resize

image-20200321211749654

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
#include
#include
#include
using namespace std;
// deque容器的插入和删除
void printDeque(const deque<int>&d){
for(deque<int>::const_iterator it=d.begin();it!=d.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}
void test(){
deque<int> d;
d.push_back(12);
d.push_back(100);
d.push_front(120);
printDeque(d);
// 尾删
d.pop_back();
d.pop_front();
printDeque(d);
// 指定位置插入删除
d.insert(d.begin(),10000);
printDeque(d);
// 在指定区间
deque<int> d2;
d2.push_back(1);
d2.push_back(2);
d2.push_back(3);
//
d.insert(d.begin(),d2.begin(),d2.end());

printDeque(d);

// 删除
d.erase(d.begin());
deque<int>::iterator it=d.begin();
it++;
d.erase(it);
printDeque(d);
sort(d.begin(),d.end()); // 排序算法
printDeque(d);
}
/*
120 12 100
12
10000 12
1 2 3 10000 12
2 10000 12
2 12 10000
*/

int main(){
test();
}

案例

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
#include
#include
#include
#include
#include
using namespace std;
class Person{
public:
string name;
int score;
Person(string name,int score){
this->name=name;
this->score=score;
}
};
void print(const vector&v){
for(vector::const_iterator it=v.begin();it!=v.end();it++){
cout<<(*it).name<<":"<<"分数"<<(*it).score<<endl;
}
cout<<endl;
}
void createPerson(vector &v){
string stringSeed="ABCDE";
int score=0;
for(int i=0;i<5;i++){
string name ="选手";
name+=stringSeed[i];
Person p(name,score);
v.push_back(p); //创建5名对象
}
}

void setScore(vector&v){
for(vector::iterator it=v.begin();it!=v.end();it++){
deque<int> d;
for(int i=0;i<10;i++){
int score=rand()%41+60; //60-100
d.push_back(score);
cout<<(*it).name<<":"<" ";
}
cout<<endl;
sort(d.begin(),d.end()); // 排序
d.pop_front();
d.pop_back(); //去掉分数
int sum=0;
for(deque<int>::iterator it=d.begin();it!=d.end();it++){
sum+=*it;
}
(*it).score=sum/d.size();
}
}

int main(){
//随机数种子
srand((unsigned int)time(NULL));
// 创建5名选手
vector v;
createPerson(v);
// 给5名选手打分
setScore(v);
//
print(v);
cout<endl;
}

stack容器

image-20200322014757939

queue容器

image-20200322015602691

list容器

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
29
#include
#include

using namespace std;

void printList(const list<int>&L){

for(list<int>::const_iterator it=L.begin();it!=L.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}

void test(){
list<int> L1;
list<int> L2;
L1.push_back(100);
L1.push_back(20);
L1.push_back(12);
L2.assign(10,100);
printList(L1);
printList(L2);
L1.swap(L2); //交换
printList(L1);
printList(L2);
}
int main(){
test();
}

image-20200324233345500

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include
#include

using namespace std;
// list 容器的排序案例
class Person{
public:
int age;
string name;
int height;
Person(string name,int age, int height){
this->name=name;
this->age=age;
this->height=height;
}


};
void printPerson(const list&L){
for(list::const_iterator it=L.begin();it!=L.end();it++){
cout<<(*it).name<<" "<<(*it).age<<" "<<(*it).height<<endl;
}
cout<<endl;
}
bool myCompare(Person &p1, Person &p2 ){ // 自定义数据类型的排序规则
if(p1.age==p2.age){
return p1.heightheight;
}
return p1.age
}
void test(){
list L;
L.push_back(Person("zhangsan",23,180));
L.push_back(Person("lisi",43,290));
L.push_back(Person("wangwu",43,90));

// 容器用年龄排序,如果年龄相同,用身高排序
printPerson(L);


L.sort(myCompare);

printPerson(L);
}

int main(){
test();
}

set集合(没有重复元素,并且自动排序)

image-20200325004617027

image-20200325011034860

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
#include

using namespace std;


void printSet(const set<int> &s){
for(set<int>::const_iterator it=s.begin();it!=s.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}
void test(){
set<int> s;
s.insert(23);
s.insert(43);
printSet(s);
// find 返回迭代器
if(s.find(43)!=s.end()){
cout<<*(s.find(43))<<endl; // 返回迭代器 *(s.find(23))
}
cout<23)<<endl;
}
int main(){
test();
}

pair对组(和元组很像)

paie的创建方式

1
2
pair<string,int>p("tom",23);
pair<string,int> p2 =make_pair("zhangsan",43);

利用仿函数重载排序规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class compare{
public:
bool operator()(int v1,int v2){
return v1>v2;
}


};
set<int,compare> s;
s.insert(23);
s.insert(43);
for(set<int,compare>::iterator it=s.begin();it!=s.end();it++){
cout<<*it<<" ";
}

map容器(高效率)(会按照key排序)

image-20200325015540130

1
2
3
4
5
6
7
8
9
10
map<string,int> m;
m.insert(pair<string,int>("zhangsan",23));
m.insert(pair<string,int>("lisi",34));
m.insert(pair<string,int>("wangwu",343));
m.insert(make_pair("wangmazi",89));
m.insert(map<string,int>::value_type("啦啦啦",43));
cout<"zhangsan"]<<endl; // 中括号适合访问,不适合插入
for(map<string,int>::iterator it=m.begin();it!=m.end();it++){
cout<first<second<<endl;
}

本文标题:容器

文章作者:philxling

发布时间:2020年04月15日 - 23:20

最后更新:2020年10月15日 - 12:26

原始链接:http://philxling.club/2020/04/15/c++/5-%E5%AE%B9%E5%99%A8/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

philxling wechat
ex. subscribe to my blog by scanning my public wechat account
-------------本文结束感谢您的阅读-------------
谢谢你给我糖吃!!!
0%