vector
vector用于定义变长数组,基本操作如下:
#include<cstdio>
#include<vector> //声明头文件
using namespace std; //声明命名空间
int main(){
//init -------------------------------------------------------------------
vector<int> vi;
for(int i=1;i<=5;i++){
vi.push_back(i);
//下标访问*
printf("%d ",vi[i-1]);
}
//迭代器访问
vector<int>::iterator it; //定义迭代器
for(it=vi.begin();it!=vi.end();it++){
printf("%d",*it);
}
printf("\n");
//add-----------------------------------------------------------------------
vi.insert(vi.begin()+1,-1);
for(int i=0;i<vi.size();i++){ //size,计算长度
printf("%d",vi[i]);
}
printf("\n");
//delet---------------------------------------------------------------------
vi.pop_back(); //删除尾元素
for(int i=0;i<vi.size();i++){
printf("%d",vi[i]);
}
printf("\n");
vi.erase(vi.begin()+1); //删除某一个元素
for(int i=0;i<vi.size();i++){
printf("%d",vi[i]);
}
printf("\n");
vi.erase(vi.begin()+1,vi.begin()+3); //删除一段区间
for(int i=0;i<vi.size();i++){
printf("%d",vi[i]);
}
printf("\n");
vi.clear(); //清空
for(int i=0;i<vi.size();i++){
printf("%d",vi[i]);
}
}
输出
1 2 3 4 5 12345
1-12345
1-1234
1234
14
set
set 内部自动有序 ,不含重复元素
#include<cstdio>
#include<set>
using namespace std;
int main(){
//add--------------------------------------------------------------------------
set<int> st;
st.insert(1);
st.insert(3);
st.insert(3);
st.insert(5);
st.insert(4);
st.insert(9);
//迭代器访问
set<int>::iterator it; //定义迭代器
for(it=st.begin();it!=st.end();it++){
printf("%d",*it);
}
printf("\n");
printf("%d",st.size());//打印set中元素个数
printf("\n");
it=st.find(3);
//注意用find返回的是迭代器号,编号从1开始,包括因重复而未显示出的数
printf("%d",*it);
printf("\n");
//delet------------------------------------------------------------------------
st.erase(4); //删除某值
for(it=st.begin();it!=st.end();it++){
printf("%d",*it);
}
printf("\n");
st.erase(st.find(3),st.find(9)); //区间左闭右开,*(it+i)仅vector和string可用
for(it=st.begin();it!=st.end();it++){
printf("%d",*it);
}
printf("\n");
st.clear(); //清除
for(it=st.begin();it!=st.end();it++){
printf("%d",*it);
}
}
输出
13459
5
3
1359
19
string
#include<cstdio>
#include<iostream>
#include<string> //头文件
using namespace std;
int main(){
//init---------------------------------------------------------------------------
string str="abcd";
cin>>str;
cout<<str;
printf("\n");
//迭代器访问
string::iterator it;
for(it=str.begin();it!=str.end();it++)
printf("%c",*it);
printf("\n");
//下标访问
printf("%c",str[0]);
printf("\n");
}
output
abcd
abcd
abcd
a
//拼接,+=
string str1="hello world",str2="beauty";
str1+=str2;
printf("%s\n",str1.c_str());
//插入,insert---------------------------------------------------------------------------
str1.insert(11," ");
printf("%s\n",str1.c_str());
//擦除,erase----------------------------------------------------------------------------
str1.erase(str1.begin()+11); //擦除某个位置
printf("%s\n",str1.c_str());
str1.erase(str1.begin()+11,str1.end());//擦除区间
printf("%s\n",str1.c_str());
output
hello worldbeauty
hello world beauty
hello worldbeauty
hello world
hello worldbeauty
//find,查找指定字符串位置
str1="hello world",str2="beauty";
str1+=str2;
cout<<str1<<endl;
cout<<str1.find(str2)<<endl;//第一次出现位置
str1+=str2;
str1+=str2;
cout<<str1<<endl;
cout<<str1.find(str2,18)<<endl;//18好元素后,第一次出现str2的位置
//substr,返回指定位置之后指定长度字符串----------------------------------------------------
cout<<str1.substr(1,10)<<endl;
//replace,替换
str2="hello LiLi";
cout<<str1<<endl;
//replace,替换---------------------------------------------------------------------------
cout<<str1.replace(11,13,str2)<<endl;
//这里,11指开始替换位置,13指的是被替换字符串长度
cout<<str1.replace(str1.begin(),str1.begin()+5,str2)<<endl;
//替换下标0~5,左闭右开!!!
output
11
hello worldbeautybeautybeauty
23
ello world
hello worldbeautybeautybeauty
hello worldhello LiLieauty
hello LiLi worldhello LiLieauty
练习:A1060
map
map映射,把一种数据类型映射到另一种数据类型
#include<cstdio>
#include<map>
using namespace std;
int main(){
//define&visit-------------------------------------------------------------------
map<char,int> mp;//定义从char到int的映射
//注:定义时也可用STL容器,如:map<set<int>,int> mp
mp['c']=10;
mp['m']=20;
mp['a']=30;
mp['d']=40;
mp['f']=50;
//迭代器访问 ---------------------------------------------------------------------
map<char,int>::iterator it;
for(it=mp.begin();it!=mp.end();it++){
printf("%c %d ",it->first,it->second);
}
printf("\n");
//下标访问 -----------------------------------------------------------------------
printf("%d\n",mp['a']);
//find() ------------------------------------------------------------------------
printf("%c %d\n",mp.find('d')->first,mp.find('d')->second);
//erase()------------------------------------------------------------------------
mp.erase('c'); //擦除单个指,可以find配合迭代器代替此例中'c'
for(it=mp.begin();it!=mp.end();it++){
printf("%c %d ",it->first,it->second);
}
printf("\n");
============================================================
map<char,int>::iterator fir=mp.find('d'),last=mp.find('m');
mp.erase(fir,last);
//左闭右开区间,擦除[d,m)之间值
//即使p(p在f后,m前)未定义,经测试[d,p)一样可以擦除[d,p)区间的值,同理···
for(it=mp.begin();it!=mp.end();it++){
printf("%c %d ",it->first,it->second);
}
//size()
printf("\n%d\n",mp.size());
//clear()
mp.clear();
printf("%d",mp.size());
}
output
a 30 c 10 d 40 f 50 m 20
30
d 40
a 30 d 40 f 50 m 20
a 30 m 20
2
0
queue
队列
#include<cstdio>
#include<queue>
using namespace std;
int main(){
//define
queue<int> q;//定义int型队列
//empty()
if(q.empty())printf("empty");
//push()
for(int i=0;i<5;i++)q.push(i);
printf("\n");
printf("\n");
//front(),back()
printf("front %d back %d\n",q.front(),q.back());
//pop()
for(int i=0;i<3;i++)q.pop();
printf("After pop 3 times,front %d back %d\n",q.front(),q.back());
//size()
printf("%d",q.size());
}
output
empty
front 0 back 4
After pop 3 times,front 3 back 4
2
priority_queue
①基本操作
#include<cstdio>
#include<queue>
using namespace std;
int main(){
priority_queue<int> q;//int型优先队列,优先队列中,一般数值越大优先级越高
//push();top();pop()
q.push(3);
q.push(4);
q.push(1);
while(q.size()>0){
printf("%d ",q.top());
if(!q.empty())q.pop();
}
}
output
4 3 1
②优先级调整
#include<cstdio>
#include<queue>
using namespace std;
int main(){
priority_queue<int,vector<int>,greater<int> > q;
// 从小数到大数优先级递减,最后一个参数改成less<int>结果与之相反
q.push(3);
q.push(4);
q.push(1);
while(q.size()>0){
printf("%d ",q.top());
if(!q.empty())q.pop();
}
}
output
1 3 4
③结构体中的应用
#include<iostream>
#include<string>
#include<queue>
using namespace std;
struct fruit{
string name;
int price;
friend bool operator < (fruit f1,fruit f2){//定义友元
return f1.price>f2.price;//小数到大数优先级递减,注意!这里的表示方法与sort相反
}
}f1,f2,f3;
int main(){
priority_queue<fruit> 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);
while(q.size()>0){
cout<<q.top().name<<' '<<q.top().price<<endl;
if(!q.empty())q.pop();
}
}
output
桃子 1
苹果 3
鸭梨 4
另一种表示方法
#include<iostream>
#include<string>
#include<queue>
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;//小数到大数优先级递减
//注意,这里的表示方法与sort相反
}
};
int main(){
priority_queue<fruit,vector<fruit>,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);
while(q.size()>0){
cout<<q.top().name<<' '<<q.top().price<<endl;
if(!q.empty())q.pop();
}
}
output
桃子 1
苹果 3
鸭梨 4
注:数据量比较大时,使用引用可提高效率,形式如下
bool operator ()(const fruit &f1,const fruit &f2){····};
friend bool operator < (const fruit &f1,const fruit &f2){····};
stack
#include<cstdio>
#include<stack>
using namespace std;
int main(){
//define
stack<int> st;//定义int型栈
//empty()
if(st.empty())printf("empty\n");
//push()
for(int i=0;i<5;i++){
st.push(i);
printf("%d",i);
}
printf("\n");
//size();top();pop()
while(st.size()>0){
printf("%d",st.top());
st.pop();
}
}
output
empty
01234
43210
pair
pair容器用于联系两个不同的数据类型,类似于结构体,常用于给map赋值
#include<iostream>
#include<string>
#include<map>
using namespace std;
//实际上使用pair应用include<utility>,不过在使用map头时会自动装载该头,故可以偷懒
int main(){
//define
pair<string, int> p ;//从string到int的pair容器;
p.first="hh";
p.second=10;
//另一种初始化方法make_pair("hh",10);
cout<<p.first<<" "<<p.second<<endl;
//不同pair的大小关系
pair<int,int> p1,p2,p3 ;
p1=make_pair(5,55);
p2=make_pair(55,5);
p3=make_pair(5,555);
if(p1>=p2)cout<<"p1 >= p2 first:"<<p1.first<<" "<<p2.first<<endl;
else cout<<"p1 < p2 fiest:"<<p1.first<<" "<<p2.first<<endl;
if(p2>=p3)cout<<"p2 >= p3 first:"<<p2.first<<" "<<p3.first<<endl;
else cout<<"p2 < p3 first:"<<p2.first<<" "<<p3.first<<endl;
if(p1>=p3)cout<<"p1 >= p3 first:"<<p1.first<<" "<<p3.first<<endl;
else cout<<"p1 < p3 first:"<<p1.first<<" "<<p3.first<<endl;
//在map中的应用
map<string,int> mp;
mp.insert(make_pair("good",10));
cout<<mp.find("good")->first<<" "<<mp.find("good")->second;
}
output
hh 10
p1 < p2 fiest:5 55
p2 >= p3 first:55 5
p1 < p3 first:5 5
good 10