导航菜单
首页 >  ccf-csp考试内容  > CCF

CCF

获取字符串长度

s.size()和s.length() 返回string对象的字符个数,他们执行效果相同。

s.max_size()                 返回string对象最多包含的字符数,超出会抛出length_error异常

s.capacity()                  重新分配内存之前,string对象能包含的最大字符数

插入

s.push_back()                      在末尾插入

例:s.push_back('a')           末尾插入一个字符a

s.insert(pos,element)           在pos位置插入element

例:s.insert(s.begin(),'1')在第一个位置插入1字符

s.append(str)                      在s字符串结尾添加str字符串

例:s.append("abc")           在s字符串末尾添加字符串“abc”

删除

erase(iterator p)                         删除字符串中p所指的字符

erase(iterator first, iterator last)   删除字符串中迭代器区间[first,last)上所有字符

erase(pos, len)                           删除字符串中从索引位置pos开始的len个字符

clear()                                        删除字符串中所有字符

字符替换

s.replace(pos,n,str)       把当前字符串从索引pos开始的n个字符替换为str

s.replace(pos,n,n1,c)    把当前字符串从索引pos开始的n个字符替换为n1个字符c

s.replace(it1,it2,str)       把当前字符串[it1,it2)区间替换为str it1 ,it2为迭代器哦

大小写转换

法一:

tolower(s[i])   转换为小写

toupper(s[i])   转换为大写

法二:

string s;

transform(s.begin(),s.end(),s.begin(),::tolower);//转换小写

transform(s.begin(),s.end(),s.begin(),::toupper);//转换大写

分割

s.substr(pos,n)      截取从pos索引开始的n个字符

查找

s.find (str, pos)                           在当前字符串的pos索引位置(默认为0)开始,查找子串str,返回找到的位置索引,-1表示查找不到子串

s.find (c, pos)                             在当前字符串的pos索引位置(默认为0)开始,查找字符c,返回找到的位置索引,-1表示查找不到字符

s.rfind (str, pos)                          在当前字符串的pos索引位置开始,反向查找子串s,返回找到的位置索引,-1表示查找不到子串

s.rfind (c,pos)                             在当前字符串的pos索引位置开始,反向查找字符c,返回找到的位置索引,-1表示查找不到字符

s.find_first_of (str, pos)                在当前字符串的pos索引位置(默认为0)开始,查找子串s的字符,返回找到的位置索引,-1表示查找不到字符

s.find_first_not_of (str,pos)          在当前字符串的pos索引位置(默认为0)开始,查找第一个不位于子串s的字符,返回找到的位置索引,-1表示查找不到字符

s.find_last_of(str, pos)                 在当前字符串的pos索引位置开始,查找最后一个位于子串s的字符,返回找到的位置索引,-1表示查找不到字符

s.find_last_not_of ( str, pos)         在当前字符串的pos索引位置开始,查找最后一个不位于子串s的字符,返回找到的位置索引,-1表示查找不到子串

排序       sort(s.begin(),s.end());  //按ASCII码排序

相关推荐: