vector:长度根据需要而自动改变的数组。
定义:
vector name;
vector Arrayname[arraySize];
如果typename也是一个STL容器,定义时需要记得在>>符号之间加上空格。
如:vector
vector vi;
直接通过vi[index]访问。
实例:
#include
#include
using namespace std;
int main(){
vector vi;
for (int i = 1; i <= 5;++i)
vi.push_back(i);
//vi.begin()为取vi的首元素地址,而it指向这个地址
//vi.end()是尾元素地址的下一个地址
vector::iterator it = vi.begin();
//方式一:vi[i]与*(vi.begin()+i)等价
for (int i = 0; i < 5;i++)
cout << *(it + i);
cout << endl;
//方式二
for (vector::iterator it = vi.begin(); it != vi.end();it++)
cout << *it;
return 0;
}
输出结果:12345
push_back(x)就是在vector后面添加一个元素x,时间复杂度O(n)。
#include
#include
using namespace std;
int main(){
vector vi;
for (int i = 1; i <= 3;++i)
vi.push_back(i);//将1、2、3依次插入vi末尾
for (int i = 0; i < vi.size();++i)//size()函数给出vi元素个数
cout << vi[i];
return 0;
}
输出结果:123
删除vector的尾元素,时间复杂度为O(1)。
#include
#include
using namespace std;
int main(){
vector vi;
for (int i = 1; i <= 3;++i)
vi.push_back(i); //将1、2、3依次插入vi末尾
vi.pop_back(); //删除vi的尾元素3
for (int i = 0; i < vi.size();++i)//size()函数给出vi元素个数
cout << vi[i];
return 0;
}
输出结果:12
用来获取vector中元素个数,时间复杂度O(1)。
#include
#include
using namespace std;
int main(){
vector vi;
for (int i = 1; i <= 3;++i)
vi.push_back(i); //将1、2、3依次插入vi末尾
cout << vi.size();
return 0;
}
输出结果:3
清空vector中所有元素,时间复杂度O(N),其中N为vector中元素的个数。
#include
#include
using namespace std;
int main(){
vector vi;
for (int i = 1; i <= 3;++i)
vi.push_back(i); //将1、2、3依次插入vi末尾
vi.clear();
cout << vi.size();
return 0;
}
输出结果:0
insert(it,x)用来向vector的任意迭代器it处插入一个元素x,时间复杂度O(N)。
#include
#include
using namespace std;
int main(){
vector vi;
for (int i = 1; i <= 5;++i)
vi.push_back(i); //将1、2、3、4、5依次插入vi末尾
for (int i = 0; i < vi.size();++i)
cout << vi[i];
cout << endl;
vi.insert(vi.begin() + 2, 0);//将0插入vi[2]的位置
for (int i = 0; i < vi.size();++i)
cout << vi[i];
return 0;
}
输出结果:
12345
120345
erase(it):删除迭代器为it处的元素。
#include
#include
using namespace std;
int main(){
vector vi;
for (int i = 1; i <= 5;++i)
vi.push_back(i); //将1、2、3、4、5依次插入vi末尾
for (int i = 0; i < vi.size();++i)
cout << vi[i];
cout << endl;
vi.erase(vi.begin() + 3);//删除vi[3]即删除4
for (int i = 0; i < vi.size();++i)
cout << vi[i];
return 0;
}
输出结果:
12345
1235
erase(first,last):删除[first,last)内所有元素。
#include
#include
using namespace std;
int main(){
vector vi;
for (int i = 1; i <= 5;++i)
vi.push_back(i); //将1、2、3、4、5依次插入vi末尾
for (int i = 0; i < vi.size();++i)
cout << vi[i];
cout << endl;
vi.erase(vi.begin()+1,vi.begin()+4);//删除vi[1]、vi[2]、vi[3]
for (int i = 0; i < vi.size();++i)
cout << vi[i];
return 0;
}
输出结果:
12345
15
set:集合,是一个内部自动有序且不含重复元素的容器。
定义:
set name;
set Arrayname[arraySize];
set只能通过迭代器iterator访问。
set::iterator it;
只有vector和string支持(it+i)的访问方式。*
实例:
#include
#include
using namespace std;
int main(){
set st;
st.insert(5);
st.insert(2);
st.insert(4);
st.insert(1);
st.insert(3);
//set不支持it::iterator it = st.begin(); it != st.end();it++)
cout << *it;
return 0;
}
输出结果:12345
insert(x)将x插入set容器中,并自动递增排序和去重,时间复杂度O(logN),其中N为set内的元素个数。实例如上“set容器内元素的访问”。
find(value)返回set中对应值为value的迭代器,时间复杂度O(logN),N为set中元素个数。
#include
#include
using namespace std;
int main(){
set st;
for (int i = 1; i <= 3;++i)
st.insert(i);
set::iterator it = st.find(2);//在set中查找2,返回其迭代器
cout << *it;
//也可以写成cout<<*(st.find(2));
return 0;
}
size()用来获得set内元素的个数,时间复杂度为O(1)。
#include
#include
using namespace std;
int main(){
set st;
st.insert(2);//插入2 5 4
st.insert(5);
st.insert(4);
cout << st.size();
return 0;
}
输出结果:3
clear()清空set中的所有元素,时间复杂度O(N),N为set内元素个数。
#include
#include
using namespace std;
int main(){
set st;
st.insert(2);//插入2 5 4
st.insert(5);
st.insert(4);
st.clear();
cout << st.size();
return 0;
}
输出结果:0
st.erase(it),it为所需要删除元素的迭代器。时间复杂度O(1),可以结合find()函数使用。
#include
#include
using namespace std;
int main(){
set st;
for (int i = 1; i <=5;++i)
st.insert(i);
st.erase(st.find(3));//利用find()函数找到3,然后用erase删除它
for (set::iterator it = st.begin(); it != st.end();it++)
cout << *it;
return 0;
}
输出结果:1245
st.erase(first,last):删除一个区间里所有元素,其中first为所需要删除区间的其实迭代器,last为所需要删除区间的末尾迭代器的下一个地址,即删除[first,last)。时间复杂度O(last-first)。
#include
#include
using namespace std;
int main(){
set st;
for (int i = 1; i <=5;++i)
st.insert(i);
st.erase(st.find(3), st.end());//删除元素3到末尾之间的元素,即删除3~5。
for (set::iterator it = st.begin(); it != st.end();it++)
cout << *it;
return 0;
}
输出结果:12
定义方式与基本数据类型相同,只需要在string后面跟上变量名。
string str;
string str="abcd";//给变量复制初始化
#include
#include
using namespace std;
int main(){
string str = "abcd";
for (int i = 0; i < str.length();i++)
cout << str[i];
return 0;
}
输出结果:abcd
string不像其他STL容器那样需要参数,可以直接定义:
string::iterator it;
实例:
#include
#include
using namespace std;
int main(){
string str = "abcd";
for (string::iterator it = str.begin(); it != str.end();it++)
cout << *it;
return 0;
}
输出结果:abcd
string加法:可以将两个string直接拼接起来。
#include
#include
using namespace std;
int main(){
string str1 = "abc", str2 = "xyz", str3;
str3 = str1 + str2;//str1与str2拼接,赋值给str3
str1 += str2;//将str2直接拼接到str1上
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
return 0;
}
输出结果:
abcxyz
xyz
abcxyz
两个string类型可以直接使用==,!=,<, <=, >, >=比较大小,比较规则是字典序。
#include
#include
using namespace std;
int main(){
string str1 = "aa", str2 = "aaa", str3="abc",str4="xyz";
if(str1=str3)
cout << "str4>=str3" << endl;
return 0;
}
输出结果:
str1=str3
leng()返回string的长度,即存放的字符数,时间复杂度O(1)。
string str="abc";
cout<
输出结果:3 3
insert(pos,string):在pos号位置插入字符串string。
insert(it,it2,it3),it为原字符串欲插入位置,it2和it3为待插字符串的首位迭代器,用来表示串[it2,it3)将被插在it的位置上。
#include
#include
using namespace std;
int main(){
string str = "abcxyz", str2 = "opq";//str是原字符串,str2是待插字符串
//方式一:往str[3]处插入opq
// str.insert(3, str2);
//方式二:
str.insert(str.begin() + 3, str2.begin(), str2.end());
cout << str << endl;
return 0;
}
输出结果:abcopqxyz
clear()清空string中的数据,时间复杂度O(1)。
#include
#include
using namespace std;
int main(){
string str = "abcd";
str.clear();
cout << str.length();
return 0;
}
输出结果:0
有删除单个元素、删除一个区间里所有元素两种方式。时间复杂度均为O(N)。
#include
#include
using namespace std;
int main(){
string str = "abcdefg";
str.erase(str.begin() + 4);//删除4号位(e)
cout << str;
return 0;
}
输出结果:abcdfg
#include
#include
using namespace std;
int main(){
string str = "abcdefg";
//删除在[str.begin()+2,str.end()]内的元素,即cdefg
str.erase(str.begin() + 2, str.end());
cout << str;
return 0;
}
输出结果:ab
#include
#include
using namespace std;
int main(){
string str = "abcdefg";
//删除从3号位开始的2个字符,即de
str.erase(3, 2);
cout << str;
return 0;
}
输出结果:abcfg
substr(pos,len):返回从pos号位开始、长度为len的子串。时间复杂度O(len)。
#include
#include
using namespace std;
int main(){
string str = "abcdefg";
cout << str.substr(0,3);
return 0;
}
输出结果:abc
string::npos是一个常数,本身值为-1,npos用来表示不存在的位置。
#include
#include
using namespace std;
int main(){
if(string::npos==-1)
cout << "-1 is true"<
输出结果:
-1 is true
18446744073709551615 is also true
str.find(str2),当str2是str的子串时,返回其在str中第一次出现的位置;如果str2不是str的子串,那么返回string::pos。
str.find(str2,pos):从str的pos号位开始匹配str2,返回值与上相同。
时间复杂度O(mn),n,m分别为str和str2的长度。
#include
#include
using namespace std;
int main(){
string str = "Thank you";
string str2 = "you";
string str3 = "like";
cout << str.find(str2) << endl;
cout << str.find(str3) << endl;
return 0;
}
输出结果:
6
18446744073709551615
str.replace(pos,len,str2):把str从pos号位开始、长度为len的子串替换为str2。
str.replace(it1,it2,str2):把str的迭代器[it1,it2)范围的子串替换为str2。
时间复杂度O(str.length())
#include
#include
using namespace std;
int main(){
string str = "Thank you for your love";
string str2 = "her";
string str3 = "enthusiasm";
cout << str.replace(14, 4, str2) << endl;
cout << str.replace(str.end()-4, str.end(), str3);
return 0;
}
输出结果:
Thank you for her love
Thank you for her enthusiasm
map:可以将任何基本类型(包括STL容器)映射到任何基本类型(包括STL容器)。
定义:
map mp;//第一个是健的类型,第二个是值的类型。
map mp; //字符串到整型的映射,必须使用string而不是char
map,string> mp;
map中的健是唯一的。
#include
#include
输出结果:30
定义:
map::iterator it;
map使用it->first来访问健,it->second来访问值。
#include
#include
输出结果:map会以健从小到大的顺序自动排序。
a 40
m 20
r 30
find(key):返回键为key的映射的迭代器,时间复杂度O(logN),N为map中映射的个数。
#include
#include
输出结果:b 2
mp.erase(it):it为需要删除的元素的迭代器。时间复杂度O(N)。
#include
#include
输出结果:
a 1
c 3
mp.erase(first,last):删除左闭右开的区间[first,last)。时间复杂度O(last-first)。
#include
#include
输出结果:a 1
用来获得map中映射的对数,时间复杂度O(1)。
#include
#include
输出结果:3
用来清空map中所有元素,时间复杂度O(N),N为map中元素的个数。
#include
#include
输出结果:0
定义:
queue name;
在STL中只能通过front()来访问队首元素,或是通过back()来访问队尾元素。
#include
#include
using namespace std;
int main(){
queue q;
for (int i = 1; i <= 5;i++)
q.push(i);//将i压入队列,依次入队1 2 3 4 5
cout << q.front() << " " << q.back() << endl;
return 0;
}
输出结果:1 5
push(x)将x进行入队,时间复杂度O(1)。
分别获得队首元素和队尾元素,时间复杂度O(1)。
令队首元素出队,时间复杂度O(1)。
#include
#include
using namespace std;
int main(){
queue q;
for (int i = 1; i <= 5;i++)
q.push(i);//将i压入队列,依次入队1 2 3 4 5
for (int i = 1; i <= 3;++i){
q.pop();//出队1 2 3
}
cout << q.front();
return 0;
}
输出结果:4
#include
#include
using namespace std;
int main(){
queue q;
if(q.empty()==true)//一开始队列中没有元素,所以是空
cout << "Empty" << endl;
else
cout << "Not Empty" << endl;
q.push(1);
if(q.empty()==true)//入队后,队列非空
cout << "Empty" << endl;
else
cout << "Not Empty" << endl;
return 0;
}
输出结果:
Empty
Not Empty
#include
#include
using namespace std;
int main(){
queue q;
for (int i = 1; i <= 5;++i)
q.push(i);
cout << q.size() << endl;
return 0;
}
输出结果:5
priority_queue称为优先队列,底层是用堆来实现的。在优先队列中,队首元素一定是当前队列中优先级最高的那一个。
定义:
priority_queue name;
#include
#include
using namespace std;
int main(){
priority_queue q;
q.push(3);
q.push(4);
q.push(1);
cout << q.top();
return 0;
}
输出结果:4
push(x)令x入队,时间复杂度O(logN),其中N为当前优先队列中的元素个数。
top()可以获得队首元素,时间复杂度O(1)。
pop()令队首元素出队,时间复杂度O(logN),其中N为当前优先队列中的元素个数。
#include
#include
using namespace std;
int main(){
priority_queue q;
q.push(3);
q.push(4);
q.push(1);
cout << q.top()<
输出结果:
4
3
检测优先队列是否为空,返回true则空,返回false则非空。时间复杂度O(1)。
#include
#include
using namespace std;
int main(){
priority_queue q;
if(q.empty()==true)
cout << "Empty" << endl;
else
cout << "Not Empty" << endl;
q.push(1);
if(q.empty()==true)
cout << "Empty" << endl;
else
cout << "Not Empty" << endl;
return 0;
}
输出结果:
Empty
Not Empty
#include
#include
using namespace std;
int main(){
priority_queue q;
q.push(3);
q.push(4);
q.push(1);
cout << q.size();//优先队列中有3个元素
return 0;
}
输出结果:3
此处的基本数据类型就是int型、double 型、char型等可以直接使用的数据类型,一般是数字大的优先级越高。
priority_queue q;
priority_queue,less > q;
priority_queue,greater > q;
第一个参数如果是double或char型,则需要填写vector或vector;第二个参数填写的是来承载底层数据结构堆(heap)的容器**。第三个参数less则是对第一个参数的比较类,less表示数字大的优先级越大,而greater表示数字小的优先级越大。**
实例:
#include
#include
using namespace std;
int main(){
priority_queue,greater > q;
q.push(3);
q.push(4);
q.push(1);
cout << q.top();
return 0;
}
输出结果:1
实例:
#include
#include
using namespace std;
struct fruit{
string name;
int price;
friend bool operator<(fruit f1,fruit f2){
return f1.price > f2.price;
}
} f1, f2, f3;
int main(){
priority_queue q;
f1.name = "桃子";
f1.price = 3;
f2.name = "梨子";
f2.price = 4;
f3.name = "苹果";
f3.price = 1;
q.push(f1);
q.push(f2);
q.push(f3);
cout << q.top().name << " " << q.top().price << endl;
return 0;
}
输出结果:苹果 1
可以看到:优先队列的这个函数与sort中的cmp函数是相反的。
于是,可以将cmp函数写到结构体外面:
#include
#include
#include
using namespace std;
struct fruit{
string name;
int price;
} f1, f2, f3;
struct cmp{
bool operator() (fruit f1,fruit f2){
return f1.price > f2.price;
}
};
int main(){
priority_queue,cmp> q;
f1.name = "桃子";
f1.price = 3;
f2.name = "梨子";
f2.price = 4;
f3.name = "苹果";
f3.price = 1;
q.push(f1);
q.push(f2);
q.push(f3);
cout << q.top().name << " " << q.top().price << endl;
return 0;
}
输出结果:苹果 1
定义:
stack name;
在STL中的stack只能通过top()来访问宅顶元素。
#include
#include
using namespace std;
int main(){
stack st;
for (int i = 1; i <= 5;++i)
st.push(i);//依次入栈1、2、3、4、5
cout << st.top();//取栈顶元素
return 0;
}
输出结果:5
push(x)将x入栈,时间复杂度O(1)。
top()获得栈顶元素,时间复杂度O(1)。
pop()弹出栈顶元素,时间复杂度O(1)。
实例:
#include
#include
using namespace std;
int main(){
stack st;
for (int i = 1; i <= 5;++i)
st.push(i);//依次入栈1、2、3、4、5
for (int i = 1; i <= 3;++i)
st.pop();//将5 4 3依次出栈
cout << st.top(); //取栈顶元素
return 0;
}
输出结果:2
检测stack是否为空,为空则返回true,不为空返回false;
#include
#include
using namespace std;
int main(){
stack st;
if(st.empty()==true)
cout << "Empty" << endl;
else
cout << "Not Empty" << endl;
st.push(1);
if(st.empty()==true)
cout << "Empty" << endl;
else
cout << "Not Empty" << endl;
return 0;
}
输出结果:
Empty
Not Empty
返回stack内元素个数,时间复杂度O(1)。
#include
#include
using namespace std;
int main(){
stack st;
for (int i = 1; i <= 5;++i)
st.push(i);
cout << st.size() << endl;
return 0;
}
输出结果:5
pair可以看作一个内部有两个元素的结构体,其这两个元素的类型是可以指定的。
struct pair{
typeName1 first;
typeName2 second;
}
头文件:
#include
定义:
pair name;
pair p;
pair p("haha",3);
make_pair("haha",5);//使用自带的make_pair函数定义
pair中元素的访问
#include
#include
#include
using namespace std;
int main(){
pair p;
p.first = "haha";
p.second = 5;
cout << p.first << " " << p.second << endl;
p = make_pair("xixi", 55);
cout << p.first << " " << p.second << endl;
p = pair("heihei", 555);
cout << p.first << " " << p.second << endl;
return 0;
}
输出结果:
haha 5
xixi 55
heihei 555
比较操作符:先以first的大小作为标准,只有当first相等时才去判别second的大小。
#include
#include
#include
using namespace std;
int main(){
pair p1(5, 10);
pair p2(5, 15);
pair p3(10, 5);
if(p1
输出结果:
p1
#include
#include
输出结果:
hahai 10
heihei 5
#include
#include
using namespace std;
int main(){
int x = -1, y = -2;
cout << max(x, y) << " " << min(x, y) << endl;
cout << abs(x) << endl;
return 0;
}
输出结果:
-1 -2
1
#include
#include
using namespace std;
int main(){
int x = -1, y = -2;
swap(x, y);
cout << x << " " << y << endl;
return 0;
}
输出结果:-2 -1
reverse(it,it2)可以将数组指针在[it,it2)之间的元素或容器的迭代器在[it,it2)范围内的元素进行反转。
#include
#include
using namespace std;
int main(){
int a[10] = {10, 11, 12, 13, 14, 15};
reverse(a, a + 4);
for (int i = 0; i < 6;++i)
cout << a[i] << " ";
return 0;
}
输出结果:13 12 11 10 14 15
对容器中的元素进行反转:
#include
#include
#include
using namespace std;
int main(){
string str = "abcdefghi";
reverse(str.begin() + 2, str.begin() + 6);//对str[2]~str[5]反转
for (int i = 0; i < str.length();++i)
cout << str[i];
return 0;
}
输出结果:abfedcghi
给出一个序列在全排列中的下一个序列。
#include
#include
#include
using namespace std;
int main(){
int a[10] = {1, 2, 3};
do{
cout << a[0] << a[1] << a[2] << endl;
}while(next_permutation(a,a+3));
return 0;
}
输出结果:
123
132
213
231
312
321
fill()可以把数组或容器中的某一段区间赋为某个相同的值。这里的赋值可以是数组类型对应范围中的任意值。
#include
#include
#include
using namespace std;
int main(){
int a[5] = {1, 2, 3, 4, 5};
fill(a, a + 5, 233);//将a[0]~a[4]均赋值为233
for (int i = 0; i < 5;++i)
cout << a[i]<<" ";
return 0;
}
输出结果:233 233 233 233 233
sort(首元素地址(必填),尾元素地址的下一个地址(必填),比较函数(非必填))
不写比较函数,则默认递增排序。
#include
#include
#include
using namespace std;
bool cmp(int a,int b){
return a > b;//a>b时把a放在b前面
}
int main(){
int a[] = {3, 1, 4, 2};
sort(a, a + 4, cmp);
for (int i = 0; i < 4;++i)
cout << a[i] << " ";
return 0;
}
输出结果:4 3 2 1
#include
#include
using namespace std;
struct node{
int x, y;
} ssd[10];
bool cmp(node a,node b){
if(a.x!=b.x)
return a.x > b.x;//x不等时按照x从大到小排序
else
return a.y < b.y;//x相等时按照y从小到大排序
}
int main(){
ssd[0].x = 2;//{2,2}
ssd[0].y = 2;
ssd[1].x = 1;//{1,3}
ssd[1].y = 3;
ssd[2].x = 2;//{2,1}
ssd[2].y = 1;
sort(ssd, ssd + 3, cmp);
for (int i = 0; i < 3;++i)
cout << ssd[i].x << " " << ssd[i].y << endl;
return 0;
}
输出结果:
2 1
2 2
1 3
只有vector、string、deque是可以使用sort的。
#include
#include
using namespace std;
bool cmp(string str1,string str2){
return str1.length() < str2.length();
}
int main(){
string str[3] = {"aaaa", "bbb", "ccccc"};
sort(str, str + 3, cmp);
for (int i = 0; i < 3;++i)
cout << str[i] << " ";
return 0;
}
输出结果:bbb aaaa ccccc
lower_bound(first,last,val):用来寻找在数组或容器内[first,last)范围内第一个大于等于val的元素的位置。
lower_bound(first,last,val):用来寻找在数组或容器内[first,last)范围内第一个大于val的元素的位置。
时间复杂度均为O(log(last-first))。
#include
#include
using namespace std;
int main(){
int a[10] = {1, 2, 2, 3, 3, 3, 5, 5, 5, 5};
//寻找3
//直接令返回值减去数组首地址
cout << lower_bound(a, a + 10, 3) - a << " " << upper_bound(a, a + 10, 3) - a;
return 0;
}
输出结果:4 3 2 1
#include
#include
using namespace std;
struct node{
int x, y;
} ssd[10];
bool cmp(node a,node b){
if(a.x!=b.x)
return a.x > b.x;//x不等时按照x从大到小排序
else
return a.y < b.y;//x相等时按照y从小到大排序
}
int main(){
ssd[0].x = 2;//{2,2}
ssd[0].y = 2;
ssd[1].x = 1;//{1,3}
ssd[1].y = 3;
ssd[2].x = 2;//{2,1}
ssd[2].y = 1;
sort(ssd, ssd + 3, cmp);
for (int i = 0; i < 3;++i)
cout << ssd[i].x << " " << ssd[i].y << endl;
return 0;
}
输出结果:
2 1
2 2
1 3
只有vector、string、deque是可以使用sort的。
#include
#include
using namespace std;
bool cmp(string str1,string str2){
return str1.length() < str2.length();
}
int main(){
string str[3] = {"aaaa", "bbb", "ccccc"};
sort(str, str + 3, cmp);
for (int i = 0; i < 3;++i)
cout << str[i] << " ";
return 0;
}
输出结果:bbb aaaa ccccc
lower_bound(first,last,val):用来寻找在数组或容器内[first,last)范围内第一个大于等于val的元素的位置。
lower_bound(first,last,val):用来寻找在数组或容器内[first,last)范围内第一个大于val的元素的位置。
时间复杂度均为O(log(last-first))。
#include
#include
using namespace std;
int main(){
int a[10] = {1, 2, 2, 3, 3, 3, 5, 5, 5, 5};
//寻找3
//直接令返回值减去数组首地址
cout << lower_bound(a, a + 10, 3) - a << " " << upper_bound(a, a + 10, 3) - a;
return 0;
}
输出结果:3 6