文章目录
待完善…
find()函数
vector的find
// 寻找vec中数值10所在下标,返回10的迭代器
auto it = find(vec.begin(), vec.end(), 10);
if (it != vec.end())
cout << it - vec.begin(); //tip: cout << *it = 10;
}
string的find
// 查找第一次出现的目标字符串substr,如果查找成功则输出查找到的第一个位置,否则返回-1
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
// 寻找键值为1的 返回1所对应的键值对
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);
//注意不是vector的类函数哟!!
二.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
//map中count(键值),只有1和0两种结果,用于判断其中是否有该键值对应的键值对
if(maps.count(1)==0)
{
cout<<"没有1这个元素"<<endl;
}
// tip:判断其中是否有该键值对应的键值对,还可以用find方法
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()); // 将v [begin(), end()) 区间中的元素拷贝给本身。
vector(n, elem); // 构造函数将n个elem拷贝给本身。
|| vector的赋值
|“ = ”赋值:
vector& operator=(const vector& vec)
|assign赋值 --- 传入子集时使用
vector.assign(v'.begin(), v'.end())// 将v [begin(), end()) 区间中的元素拷贝给本身
vector.assign(10, 100) // 重复次数和重复字符
|| vector的容量和大小
empty() --// 判空返回true或false
size() --//返回容器长度(元素个数)
|| vector的插入和删除
push_back(elem)
pop_back()
insert(const_iterator pos, int count = 1, elemtype elem)
//在迭代器指向的位置插入count个elem 注意:必须使用迭代器指明位置,插入个数不指明时为1
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];
}
//copy函数
int arr[input.size()];
copy(vec.begin(), vec.end(), arr);
string
|| 容器string内部封装的是一个类,其中封装的成员函数操作对象可以是string类,也可以是char型字符串
|| string类的构造函数 :(初始化)
string()默认构造
string s2(const char * str) // string s2 (s1) -- 拷贝构造(拷贝字符串或拷贝string)
string(int a, char b) 使用a个b初始化string
|| string的赋值操作
//1,“ = ”赋值
//2,assign赋值 (可以传入子串,和自定义重复串)
string & assign (const char * s) / (const char * s, int a)//---将s或s前a个字符赋值给string
string & assign(const string & s) / (const string & s, int a)// ---将s或s除前a个字符后的所有字符值给string
string & assign(int a, char ‘c’) // --- 将a个字符c 赋值给string
|| string的拼接 (拼接指的是拼到末尾)
//1,“ += ”拼接:
string& operator+=(const string & s)
//2,append赋值:
string& append (const char * s) / (const char * s, int n) //将char型字符串或其前n个字符拼接在s末尾
string& append(const string & s) / (const string & s, int start, int end) //将string或string的start end范围内的子串拼接在s末尾
|| string的替换
string& replace(int pos,int num,const string& s) / (int pos, int num, const char* s)//---替换从pos位置开始的num个字符为s
|| string的比较(比较原理:逐个比较字符的ASCll码)
int compare(const string& s) / (const char * s) --- 若 = 则返回0(若>返回1,若<返回-1)
|| string的读写
1,“ [ ] ” 读取:
2,at 读取:
|| string的插入和删除 (插个话:替换的本质就是删除加插入)
//1,插入:
string& insert(int pos, const char* s) / (int pos, const string &s)
string& insert(int pos, int a , char c)
//2, 删除:
string& erase(int pos, int a) --- 删除从pos开始的a个字符
|| string的子串
string& substr(int pos = 0, int npos = s.size() - pos)
//(pos的默认值是0,n的默认值是s.size() - pos)
//当有两个参数时:截取pos - npos的子串
//当不带参数时:默认拷贝整个s
//当只带一个参数时:此参数为头,默认第二个参数为字符串末尾
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[]
本文详细介绍了C++标准库中针对vector、string和map的find、count函数的用法,以及vector的插入、删除、读取等操作。同时,讲解了string的构造、赋值、拼接、替换等功能,并探讨了map的排序方法。此外,还涵盖了数据类型的转换以及在信息技术领域的应用。

1655

被折叠的 条评论
为什么被折叠?



