C++常用标准模板库
vector(向量,即动态数组)
- 时间复杂度分析:数组后面插入元素和删除尾部元素O(1),头部或者中间插入删除依旧是O(n)
- 优点:可以动态的随着元素的增加和减少改变数组的长度,使用起来与普通数组区别不大
操作方法:
- 头文件 #include
- 定义与初始化操作
vector<int>v;//创建一个数组v,类型为int
vector<int>v(v1);//创建一个数组v,类型为int,并且初始化为v1
vector<int>v(v1.begin(),v1.end());//同上
vector<int>v = v1;//同上
vector<int>v(10);//定义一个大小为10的数组
vector<int>v(10,1);//定义一个长度为10,并且全为1的数组
- 使用方法:
v.push_back(x);//在vector后面插入一个元素x,时间复杂度O(1)
v.pop_back();//删除vector的尾元素,时间复杂度O(1)
v.size();//求vector中元素个数,时间复杂度O(1)
v.clear();//清空vector,时间复杂度O(n)
v.insert(a,b);//在vector中的任意迭代器a处插入一个元素b,时间复杂度O(n),注意是迭代器
v.earse(it);//删除迭代器it处的元素
v.earse(st,sd);//删除迭代器st至迭代器sd处的元素
v.back();//获得最后一位数字
- 遍历
//方法一:使用下标
int length = v.size();
for(int i=0;i<length;i++)
cout<<v[i];
cout<<endl;
//方法二:使用迭代器
vector<int>::iterator iterator = v.begin();
for(;iterator != v.end();iterator++)
{
cout<<*iterator;
}
set与multiset(集合)
-
时间复杂度分析:O(logn)
-
优点:可以插入,删除,查找等,时间复杂度低,并且set可以去重
-
set与multiset区别:
- set可以排序去重
- multiset保留所有元素
-
头文件:#include
操作方法:
- 初始化
vector<int> ivec;
for(vector<int>::size_type i = 0; i != 10; ++i) {
ivec.push_back(i);
ivec.push_back(i);
}
set<int> iset(ivec.begin(), ivec.end());
cout << ivec.size() << endl;//20个
cout << iset.size() << endl;// 10个
- 添加
set<string> set1;
set1.insert("the"); //第一种方法:直接添加,时间复杂度为O(logn)
set<int> iset2;
iset2.insert(ivec.begin(), ivec.end());//第二中方法:通过指针迭代器,时间复杂度为O(logn)
3.获取元素
set<int> iset;
for(int i = 0; i<10; i++)
iset.insert(i);
iset.find(1)// 返回指向元素内容为1的指针,时间复杂度为O(logn)
iset.find(11)// 返回指针iset.end()
iset.count(1)// 存在(count本质为查找set中x的数量),返回1,时间复杂度为O(logn)
iset.count(11)// 不存在,返回0
- 其余内容
size();//用来获得set内元素的个数,时间复杂度O(1)
clear();//用来清空set中所有元素,复杂度O(N)
map(映射,即键值对)
- 时间复杂度O(logn)
- 优点:可使用键作为下标来获取一个值。请注意:关联的本质在于元素的值与某个特定的键相关联,而并非通过元素在数组中的位置来获取。
- 头文件:#include
操作方法:
- 定义
map<int,string> map1; //默认为空
- 访问方法
map<string,int> mp ;
mp["hello"] = 8 ;
mp["hello"] = 100 ;
mp["aloha"] = 666 ;
mp["good"] = 777 ;
for(map<string,int>::iterator it=mp.begin();it!=mp.end();it++){
cout << it->first << ' ' << it->second << endl ;
}
输出结果:aloha 666
good 777
hello 100
-
常用函数
-
find(key),返回键为key的映射的迭代器
map<string,int> mp ; mp["hello"] = 8 ; mp["hello"] = 100 ; mp["aloha"] = 666 ; mp["good"] = 777 ; map<string,int>::iterator it = mp.find("good") ; cout << it->first << ' ' << it->second << endl ; 输出结果:good 777
-
erase(),删除单个元素和删除一个区间内的元素。
map<string,int> mp ; mp["hello"] = 8 ; mp["hello"] = 100 ; mp["aloha"] = 666 ; mp["good"] = 777 ; mp.erase(mp.find("good"));//也可以直接像这样写一个键mp.erase("good"); map<string,int>::iterator it = mp.begin(); for(;it!=mp.end();it++) cout << it.first << ' ' << it.second << endl ; //删除一个区间的元素,这里只能用迭代器删除,erase(st,ed),表示删除[st,ed)区间内的元素。 map<string,int> mp ; mp["hello"] = 8 ; mp["hello"] = 100 ; mp["aloha"] = 666 ; mp["good"] = 777 ; mp.erase(mp.find("good"),mp.find("hello")); for(auto ele : mp){//写成iterator迭代器就可以 cout << ele.first << ' ' << ele.second << endl ; } 输出结果:aloha 666 hello 100
-
size(),用来获得map中映射的对数,时间复杂度O(1)。
map<string,int> mp ; mp["hello"] = 8 ; mp["hello"] = 100 ; mp["aloha"] = 666 ; mp["good"] = 777 ; cout << mp.size() << endl ; 输出结果:3
-
clear(),用来清空map中所有元素,复杂度为O(N),其中N为map中元素的个数。
map<string,int> mp ; mp["hello"] = 8 ; mp["hello"] = 100 ; mp["aloha"] = 666 ; mp["good"] = 777 ; mp.clear() ; cout << mp.size() << endl ; 输出结果:0
-
queue(队列)
-
队列特点:先进先出
-
方法
bool empty()//判断是否为空 front()//队首元素的访问 back()//队尾元素的访问 push(x)//队尾插入x pop()//删除队首元素
-
头文件:#include
-
优先队列
-
priority_queue(队列顶端为最大元素或者最小元素)
priority_queue<int> X //大根堆,默认初始化 priority_queue<int,vector<int>,greater<int>> x //小根堆,运用了预定义函数greater<int>!
-
方法
void push(const value_type& x); //把元素x插入到优先级队列的尾部,队列的长度加1 void pop(); //删除优先级队列的第一个值,前提是队列非空,删除后队列长度减1 bool empty() const; //判断优先级队列是否为空,为空返回true,否则返回false
-
stack(栈)
- push
命令格式:void push(x);
作用:插入x到栈顶,即压栈 - pop
命令格式:void pop();
作用:弹出栈顶,即出栈 - empty
命令格式:bool empty();
作用:判断栈空,若空,返回true,否则返回false - top
命令格式:type top();
作用:返回栈顶
string(字符串)
-
头文件:#include
-
注意:#include与#include<string.h>两个头文件不一样!!!
-
操作
-
访问
//模仿数组 string str = "hello" ; for(int i=0;i<str.size();i++){ printf("%c",str[i]) ; } 输出结果:hello
-
常用函数
-
拼接
string str1 = "hello" ; string str2 = " world" ; cout << str1 + str2 << endl ; 结果:hello world
-
比较:两个string类型可以直接使用==,!=,<,<=,>,>=比较大小,比较规则是字典序。
string str1 = "aa" ; string str2 = "ab" ; string str3 = "abc" ; if(str1 != str2){ cout << "not same" << endl ; }else{ cout << "same" << endl ; } if(str3>str2){ cout << "str3>str2" << endl ; } 结果:not same str3>str2
-
获取长度
//size()||length() string str1 = "hello" ; cout << str1.size() << ' ' << str1.length() << endl ; 结果:5 5
-
插入:
/* insert()函数有很多种写法,这里列出几个常用的写法,时间复杂度度O(N)。 insert(pos,string),在pos号位置插入string。 insert(it,it1,it2),it为原字符串欲插入的位置,it2和it3为待插字符串的首尾迭代器,用来表示串[it1,it2)将被插在it的位置上。 */ string str1 = "hello world" ; string str2 = "Maric" ; str1.insert(str1.begin()+5,str2.begin(),str2.end()) ; cout << str1 << endl ; str2.insert(5,"hello") ; cout << str2 << endl ; 结果:helloMaric world Marichello
-
删除
/* erase()有两种用法,删除单个元素,上出一个区间内所有元素。时间复杂度O(N)。 a. erase(it)用于删除单个元素,it为需要删除的元素的迭代器。 b. 删除一个区间的元素有两种方法: 第一种是erase(st,ed),st,ed为string迭代器,表示删除区间[st,ed)之间的元素。 第二种是erase(pos,len),其中pos为需要删除的起始位置,len为删除的字符个数。 */ string str1 = "hello world" ; str1.erase(str1.begin()) ; cout << str1 << endl ; str1.erase(str1.begin()+5,str1.end()) ; cout << str1 << endl ; str1.erase(1,2) ; cout << str1 << endl ; 结果:ello world ello eo
-
清空
/* clear()函数用来清空string中的数据,时间复杂度O(1)。 */ string str1 = "hello world" ; cout << str1.length() << endl ; str1.clear() ; cout << str1.size() << endl ; 结果:11 0
-
截取子串
/* substr(pos,len)返回的是以pos位开始长度为len的子串,时间复杂度O(len)。 */ string str1 = "hello world" ; cout << str1.substr(6,5) << endl ; 结果:world
-
表示不存在(常在find中使用)
/* string::npos是一个常数,表示不存在的位置。取值一般为-1 */ if (a.find(b) != string::npos) { cout << "Yes!" << endl; } else { cout << "No!" << endl; }
-
查找
/* str.find(str1),当str1是str的子串时,返回其在str中第一次出现的位置。如果str1不是str的子串,那么返回string::npos str.find(str1,pos),从str的pos号位开始匹配str1,返回值与上面的相同,时间复杂度为O(nm),其中n和m分别为str和str1的长度。 */ string str1 = "hello world" ; string str2 = "world" ; if(str1.find(str2) != string::npos){ cout << str1.find(str2) << endl ; cout << str1.find(str2,3) << endl ; }else{ cout << "match failure" << endl ; } 结果:6
-
替换
/* str.replace(pos,len,str1),把str从pos号位开始,长度为len的子串替换为str1。 str.replace(it1,it2,str1)把str的迭代器[it1,it2)替换为str1 */ string str1 = "hello world" ; string str2 = "kangkang" ; cout << str1.replace(6,5,str2) << endl ; cout << str1.replace(str1.begin()+6,str1.end(),str2) << endl ; 结果:hello kangkang hello kangkang
-
-