文章目录
- find()函数
- vector的find
- string的find
- map的find
- count()函数
- vector的count
- string的count
- map的count
- vector
- string
- map的各种排序方法
- 转换相关
待完善…
find()函数
vector的find
auto it = find(vec.begin(), vec.end(), 10);
if (it != vec.end())
cout << it - vec.begin();
}
string的find
string s1 = "abcdef";
string s2 = "de";
int index = s1.find(s2) ;
int index1 = s1.find_first_of(s2);
int index1 = s1.find_last_of(s2);
map的find
auro it = map.find(1);
if(iter != map.end())
cout<<iter->second<<endl;
count()函数
vector的count
一.count函数: 返回元素值为target的元素个数。
int num=count(vector1.begin(),vector2.begin(),target);
二.count_if函数:返回符合一定条件的元素个数。compare()函数是自定义的,返回值是true就是表示符合要求。
bool compare(student a){
return 90<a.score;
}
cout<<count_if(V.begin(),V.end(),compare)<<endl;
string的count
string s;
int num = count(s.begin(), s.end(), '1');
map的count
if(maps.count(1)==0)
{
cout<<"没有1这个元素"<<endl;
}
if(maps.find(1)==maps.end())
{
cout<<"没有1这个元素"<<endl;
}
vector
tip:
如果需要高效的随机存取,而不在乎插入和删除的效率,使用vector;
如果需要大量的插入和删除,而不关心随机存取,则应使用list。”
|| vector的构造函数(初始化)
vector<T> v;
vector(const vector &vec);
vector(v.begin(), v.end());
vector(n, elem);
|| vector的赋值
|“ = ”赋值:
vector& operator=(const vector& vec)
|assign赋值 --- 传入子集时使用
vector.assign(v'.begin(), v'.end())
vector.assign(10, 100)
|| vector的容量和大小
empty() --
size() --
|| vector的插入和删除
push_back(elem)
pop_back()
insert(const_iterator pos, int count = 1, elemtype elem)
erase(const_iterator pos)
erase(const_iterator pos_start, const_iterator pos_end)
clear()
|| vector的读取
[ ]读取
at读取
front()
back()
|| vector 转为数组
int arr[n];
for(int i=0; i<n; i++){
arr[i] = vec[i];
}
int arr[input.size()];
copy(vec.begin(), vec.end(), arr);
string
|| 容器string内部封装的是一个类,其中封装的成员函数操作对象可以是string类,也可以是char型字符串
|| string类的构造函数 :(初始化)
string()默认构造
string s2(const char * str)
string(int a, char b) 使用a个b初始化string
|| string的赋值操作
string & assign (const char * s) / (const char * s, int a)
string & assign(const string & s) / (const string & s, int a)
string & assign(int a, char ‘c’)
|| string的拼接 (拼接指的是拼到末尾)
string& operator+=(const string & s)
string& append (const char * s) / (const char * s, int n)
string& append(const string & s) / (const string & s, int start, int end)
|| string的替换
string& replace(int pos,int num,const string& s) / (int pos, int num, const char* s)
|| string的比较(比较原理:逐个比较字符的ASCll码)
int compare(const string& s) / (const char * s) --- 若 = 则返回0(若>返回1,若<返回-1)
|| string的读写
1,“ [ ] ” 读取:
2,at 读取:
|| string的插入和删除 (插个话:替换的本质就是删除加插入)
string& insert(int pos, const char* s) / (int pos, const string &s)
string& insert(int pos, int a , char c)
string& erase(int pos, int a) --- 删除从pos开始的a个字符
|| string的子串
string& substr(int pos = 0, int npos = s.size() - pos)
map的各种排序方法
1、key从大到小
map<string, int, greater<int>> hash;
2、特殊规则排序
bool compare(string a, string b){
return a.size() < b.size();
}
map<string, int, compare()> hash;
3、按value排序
map中key不允许重复,而multimap允许key重复。通过将map中的key和value对调存储到multimap中,就可以实现对map按值排序了。
map<char,int> cnt;
multimap<int,char>_cnt;
for(map<char,int>::iterator itr = cnt.begin();itr!=cnt.end();itr++){
_cnt.insert(pair<int,char>(itr->second,itr->first));
}
转换相关
string --> char*
char* --> string
int --> char
char --> int
int[] --> string
string --> int[]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)