C++基本STL库

383 阅读4分钟

一 : 动态数组

1. 如何构建一个动态数组?

1.1:vector方法

1.语法为:vector<T> vec
2.需要使用#include<vector>头文件

C++中使用vector语句构建动态数组,vec为变量名,T为变量类型,初始时vec是空的.

1.2 :插入元素

C++中使用push_back()方法在数组尾部插入一个元素.

Example:

#include<iostream>
#include<vector>
using namespace std;
int main{
    vector<int> vec;       //[ ]
    vec.push_back(1);      //[1]
    vec.push_back(2);      //[1,2]
    vec.push_back(3);      //[1,2,3]
    return 0;
}

1.3 : 获取长度并且访问元素

C++中通过size()方法获取vector的长度,且通过[]操作直接访问数组中的元素.

Example:

#include<vector>
#include<iostream>
using namespace std;
int main(){
	vector<int> vec;
	vec.push_back(1);
	vec.push_back(2);
	vec.push_back(3);
	for(int i = 0;i < vec.size();i++){
		cout << vec[i] <<' ';
	}
	return 0;
}

1.4 : 删除元素

C++中使用pop_back()方法删除动态数组的最后一个元素.

Example:

#include<iostream>
#include<vector>
using namespace std;
int main{
    vector<int> vec;       //[ ]
    vec.push_back(1);      //[1]
    vec.push_back(2);      //[1,2]
    vec.push_back(3);      //[1,2,3]
    vec.pop_back();        //[1,2]
    vec.pop_back();        //[1]
    return 0;
}

1.4 : 清空

C++中可以使用clear()方法清空vector.

clear方法只是清空vector,并不会清空开的内存.

2 : 构造函数

如果我们需要n个1可以这样写:

int n = 10;
vector<int> vec
for(int i = 0;i < n;i++){
    vec.push_back(1);
}

我们也可以使用构造函数快速构建动态数组:

int n = 10;
vector<int> vec(n,1)

如果在定义一个vector的时候使用构造方法,第一个参数表示初始的动态数组的长度,第二个参数表示初始的数组里面每个元素的值,如果不传入第二个参数,那么初始的值都是0.

1.5 : 二维动态数组

vector<vector<int> > vec2

定义二维动态数组需要一维一维的赋值.

二 : 集合

2.1 : 插入元素

C++中使用insert()函数向集合中插入一个新的元素,*如果集合中已经存在了某种元素,再次插入没有效果.

Example

#include<set>
using namespace std;
int main(){
	set<string> country;
	country.insert("China");
	country.insert("American");
	return 0;
} 

2.2 :删除元素

C++中使用erase()函数删除集合中一个元素.

**Example : **

#include<set>
using namespace std;
int main(){
	set<string> country;
	country.insert("China");
	country.insert("American");
	country.erase("China");
	return 0;
} 

2.3 : 判断元素是否存在

C++中使用count函数来查找某个元素是否在集合中出现,如果存在返回1,否则返回0.

Example :

#include<set>
using namespace std;
int main(){
	set<string> country;
	country.insert("China");
	country.insert("American");
	country.erase("China");
	if(country.count("China")){
		cout << China belong to country << endl;
	}
	else{
		cout << "error";
	}
	return 0;
} 

2.4 : 迭代器

C++中使用迭代器可以访问集合中的每个元素,通过++操作让迭代器指向下一个元素.

写法:

set<T>::iterator it就定义了一个指向set<T>的迭代器it,::iterator是固定的写法.

用法:

#include<set>
#include<iostream>
using namespace std;
int main(){
	set<string> country;
	country.insert("China");
	country.insert("American");
	country.insert("Canada");
	for(set<string>::iterator it = country.begin();it != country.end();it++){
		cout << *it << endl;
	}
	return 0;
}

三 : 映射

3.1 : 构造一个映射

在C++中,我们使用map<T1,T2> m来构造一个映射,这样我们定义了一个名为m的T1到T2的映射.

比如map<string , int> m构建了一个字符串到整形的映射.

3.2 :插入一对映射

在C++中通过insert()函数插入一个新的映射,参数是一个pair.
pair<string,int>p;

pair是一个标准库类型,定义在头文件utility中,可以看成是由两个first和second的结构体,并且重载了<运算符.

make_pair(v1,v2)函数返回由v1和v2初始化的pair.

我们向映射中加入新映射对的时候就是通过插入pair来实现的,如果插入的key之前已经存在了,将不会用插入的新的value代替value,也就是说这次插入是无效的.

#include<map>
#include<string>
#include<utility>
using namespace std;
int main(){
	map<string,int> dict;
	dict.insert(make_pair("Tom",1));
	dict.insert(make_pair("Jone",2));
	dict.insert(make_pair("Alex",3));
	return 0;
} 

3.3 : 访问映射

在C++中访问映射和数组一样,直接用[]就能访问.

通常状态下,我们使用下标访问的方式来插入映射,而不是通过insert一个pair来实现.

#include<map>
#include<string>
#include<utility>
#include<iostream>
using namespace std;
int main(){
	map<string,int> dict;
	dict.insert(make_pair("Tom",1));
	dict.insert(make_pair("Jone",2));
	dict.insert(make_pair("Alex",3));
	cout << dict["Tom"];
	return 0;
} 

3.4 : 判断关键字是否存在

在C++中,可以使用count()函数判断某个关键字是否被映射过,如果关键字存在返回1,否则返回0.

#include<iostream>
#include<map>
#include<string>
using namespace std;
int main(){
	map<string,int> dict;
	dict["Tom"] = 1;
	dict["Alex"] = 2;
	dict["Ros"] = 3;
	if(dict.count("Tom")){
		cout << "Yes" << dict["Tom"] << endl;
	} 
	return 0;
} 

四 : 栈

stack与vector,map,set一样,在C++里的标准库里也有stack的实现.

stack除了支持push(),pop()等基本操作外,还支持top()获取栈顶元素,empty()判断元素是否为空,size()计算栈中元素的个数.

#include<stack>
#include<string>
#include<iostream>
using namespace std;
int main(){
	stack<string> s;
	s.push("123456");
	s.push("zwl123");
	s.push("whatheis");
	while(!s.empty()){
		cout << s.top() << endl;
		s.pop();
	}
	return 0;
}