c++ stl
作者:
turboDeng
,
2022-03-05 01:19:08
,
所有人可见
,
阅读 225
#include "iostream"
#include "vector"
#include "list"
#include "unordered_map"
#include "map"
#include "unordered_set" //哈希表
#include "string"
#include "queue"
#include "algorithm"
using namespace std;
#define x first
#define y second
typedef pair<int,int> PII;
int main()
{
cout<<"=========vector========="<<endl;
//=============================vector=============================
vector<int> vec;
vec.push_back(1); //尾部插入数字
vec.push_back(2);
vec.push_back(3);
cout<<vec.front()<<endl; //返回第一个数据
vec.insert(vec.begin()+2,66); //第2个元素后面插入
vec.erase(vec.begin()+2); //删除第三个元素 1 2 3
vec.pop_back(); //删除最后一个元素
//vec.clear(); //清空vec
reverse(vec.begin(),vec.end()); //逆序 3 2 1
sort(vec.begin(),vec.end()); //排序 从小到大 1 2 3
cout<<"vector大小"<<vec.size()<<endl;
vector<int>::iterator it;
for(it=vec.begin();it!=vec.end();it++)
cout<<*it<<endl;
cout<<"=======string==========="<<endl;
//==========================string================================
string str="hausthaust";
int cnt=count(str.begin(),str.end(),'h');
cout<<"h的数量:"<<cnt<<endl;
sort(str.begin(),str.end());
cout<<str<<endl;
//s.substr(pos, n) 从pos开始的n个字符的拷贝。
cout<<"=========pair========="<<endl;
//==================================pair=============================
//pair一般用来存坐标
PII p[10];
for(int i=0;i<5;i++) p[i].x=p[i].y=100-i;
for(int i=0;i<5;i++) cout<<p[i].x<<" "<<p[i].y<<endl;
cout<<"=========map========="<<endl;
//==================================map=============================
map<int,string> m;
m[1]="hello";
m[2]="world";
m.insert({3,"dxh"});
cout<<m.size()<<endl;
cout<<m[1]<<endl;
cout<<m[2]<<endl;
cout<<m[3]<<endl;
for(int i=1;i<=m.size();i++) cout<<m[i]<<" ";
return 0;
}