1.vector:
1)unique:将相邻的重复元素放在最后面,返回不重复的最后一个元素的后面一个位置的迭代器。删除vec中的所有重复元素:
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end())
2)用数组给vector赋值
int a[5]={1,2,3,4,5};
vector vec(a,a+5);
3)返回指定元素出现的次数
int num=count(vec.begin(),vec.end(),goal);
4)查找指定元素的位置
vector::iterator it = find(vec.begin(), vec.end(), 6) //失败返回end()
5)erase删除一个或者多个元素
迭代器会失效
6)迭代器加减法
https://blog.csdn.net/weixin_47138280/article/details/110548737
7)数组反转
8.返回迭代器之间的元素个数
distance() 函数定义在头文件,并位于 std 命名空间中。
int val = distance(v.begin(), pos);此时表示【),即表示下标。
2.map
1.初始化
map<char,int> map1; map1[char]++;//元素值默认为0;
map1.insert(make_pair(‘A’,1));
2.返回指定元素出现的次数。
int num=map1.count(key);//内部自带,set也一样
3.找到指定元素
map<char,int>::iterator it=map1.find(key);//失败返回end()
3.string
1.string和其它类型的转换
std::to_string(othertype);
std::stoi(string);//char不能直接转换为string,否则会返回char的ASCII码值
2.字符串反转
std::reverse(s.begin(),s.end())
3.字符串中查找
查找子串,返回子串第一个字母下标,失败则返回-1: int a=s.find(string);
查找子串中任意一个元素在字符串中的第一次出现的位置。
4.字符串截取
string str=string.substr(pos,n) //截取下标为pos开始的共n个元素的子串
5.字符串替换
string str=string.replace(pos,n,str1) //用str1字符串替换下标为pos开始的共n个元素的子串
4.set
1.初始化
set set1(vec.begin(),vec1.end());
set1.insert(vec.begin(),vec1.end());
2.返回指定元素出现的次数。
set1.count(number);
其它笔记
1.查询变量类型:
#include
cout<<typeid(str).name()<<endl;
2.不能通过int(char类型)强制转换或者int a=char将其转为int。需要通过以下得到
char a=‘0’;
int aa=a-‘0’;