C++string类常用函数
C++string类成员函数c_str()的用法

C++string类成员函数c_str()的⽤法1.string类成员函数c_str()的原型:const char *c_str()const;//返回⼀个以null终⽌的c字符串2.c_str()函数返回⼀个指向正规c字符串的指针,内容和string类的本⾝对象是⼀样的,通过string类的c_str()函数能够把string对象转换成c中的字符串的样式;3.操作c_str()函数的返回值时,只能使⽤c字符串的操作函数,如:strcpy()等函数.因为,string对象可能在使⽤后被析构函数释放掉,那么你所指向的内容就具有不确定性.eg: char * name[20];string ptr = "tongnono";strcpy(name,ptr.c_str());//c_str()返回的是⼀个临时的指针变量,不能对其操作.语法:const char *c_str();c_str()函数返回⼀个指向正规C字符串的指针常量, 内容与本string串相同.这是为了与c语⾔兼容,在c语⾔中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。
注意:⼀定要使⽤strcpy()函数等来操作⽅法c_str()返回的指针⽐如:最好不要这样:char* c;string s="1234";c = s.c_str(); //c最后指向的内容是垃圾,因为s对象被析构,其内容被处理,同时,编译器也将报错——将⼀个const char *赋与⼀个char *。
应该这样⽤:char c[20];string s="1234";strcpy(c,s.c_str());这样才不会出错,c_str()返回的是⼀个临时指针,不能对其进⾏操作再举个例⼦c_str() 以 char* 形式传回 string 内含字符串如果⼀个函数要求char*参数,可以使⽤c_str()⽅法:string s = "Hello World!";printf("%s", s.c_str()); //输出 "Hello World!"⽐如定义⼀个函数func(const char *s){ //s+1这个指针是对的};func(string s){//s+1是错的};主函数中: string str;调⽤这个函数时可以为 func(s.c_str());。
c++的string的find函数

c++的string的find函数C++的string类是C++标准库中的一部分,它提供了许多字符串操作的函数。
其中一个常用的函数是find()函数,它用于查找字符串中某个子串的位置。
下面将详细介绍这个函数以及它的用法。
find()函数的原型如下:```size_t find(const string& str, size_t pos = 0) const noexcept;```它接受两个参数,第一个参数是要查找的子串,第二个参数是搜索的起始位置(默认为0)。
函数返回一个size_t类型的值,表示子串在字符串中的索引位置。
如果找到子串,则返回第一次出现的位置索引;如果找不到子串,则返回string::npos。
下面是一个示例代码,演示了find()函数的基本用法:```cpp#include <iostream>#include <string>int main() {std::string str("Hello, world!");std::size_t found = str.find("world");if (found != std::string::npos) {std::cout << "子串在位置" << found << "处找到了。
" << std::endl;} else {std::cout << "子串未找到。
" << std::endl;}return 0;}```输出结果为:```子串在位置7处找到了。
```在上面的示例中,我们通过find()函数查找了字符串"world"在变量str中第一次出现的位置。
由于子串存在于字符串中,所以返回了7,正是子串的起始位置。
CPPstring类常用函数

C++string类常用函数string类的构造函数:string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初始化此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2="hello";都是正确的写法。
当构造的string太长而无法表达时会抛出length_error异常string类的字符操作:const char &operator[](int n)const;const char &at(int n)const;char &operator[](int n);char &at(int n);operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
const char *data()const;//返回一个非null终止的c字符数组const char *c_str()const;//返回一个以null终止的c字符串int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目string的特性描述:int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)int max_size()const; //返回string对象中可存放的最大字符串的长度int size()const; //返回当前字符串的大小int length()const; //返回当前字符串的长度bool empty()const; //当前字符串是否为空void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分string类的输入输出操作:string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。
c语言string函数详解

c语言string函数详解linux kernel code : kernel/lib/string.cstrsep,strtok,strchr ,strstr ......PS:本文包含了大部分strings函数的说明,并附带举例说明。
本来想自己整理一下的,发现已经有前辈整理过了,就转了过来。
@函数原型: char *strdup(const char *s)函数功能: 字符串拷贝,目的空间由该函数分配函数返回: 指向拷贝后的字符串指针参数说明: src-待拷贝的源字符串所属文件: <string.h>[cpp] view plain copy1.#include <stdio.h>2.#include <string.h>3.#include <alloc.h>4.int main()5.{6.char *dup_str, *string="abcde";7.dup_str=strdup(string);8.printf("%s", dup_str);9.free(dup_str);10.return 0;11.}@函数名称: strcpy函数原型: char* strcpy(char* str1,char* str2);函数功能: 把str2指向的字符串拷贝到str1中去函数返回: 返回str1,即指向str1的指针参数说明:所属文件: <string.h>[cpp] view plain copy1.#include <stdio.h>2.#include <string.h>3.int main()4.{5.char string[10];6.char *str1="abcdefghi";7.strcpy(string,str1);8.printf("the string is:%s\n",string);9.return 0;10.}@函数名称: strncpy函数原型: char *strncpy(char *dest, const char *src,intcount)函数功能: 将字符串src中的count个字符拷贝到字符串dest中去函数返回: 指向dest的指针参数说明: dest-目的字符串,src-源字符串,count-拷贝的字符个数所属文件: <string.h>[cpp] view plain copy1.#include<stdio.h>2.#include<string.h>3.int main()4.{5.char*src = "bbbbbbbbbbbbbbbbbbbb";//20 'b's6.char dest[50] ="aaaaaaaaaaaaaaaaaaaa";//20 'a's7.8.puts(dest);9.strncpy(dest, src, 10);10.11.puts(dest);12.return0;13.}输出:[cpp] view plain copy1./*******************************************2.aaaaaaaaaaaaaaaaaaaa3.bbbbbbbbbbaaaaaaaaaa4.*******************************************/注意:strncpy只复制指定长度的字符,不会自动在末尾加'\0'。
c语言string函数

c语言string函数
标准 C 语言的string.h头文件中定义了一组有用的函数,被称为字符串处理函数。
字符串就是由字母,数字和符号组成的一行字符序列,通常以字符串结尾显示。
它是一种数据抽象,是用来存储,分析,表示和处理应用程序中的文本数据的一种常见方式。
1.strlen()函数:该函数用来计算字符串的长度,不包括字符串结尾的null字符
'\0'。
该函数所返回的字符数不包括null字符,而只代表字符串中实际的字符数。
2.strcat()函数:该函数的作用是将字符串s2拼接到字符串s1的末尾。
这意味着s1的末尾会出现字符串s2的第一个字符,直到结束符'\0'。
4.strcmp()函数:该函数用于比较两个字符串s1和s2,如果s1>s2,函数会返回正值;如果s1=s2,函数会返回0;如果s1
5.strchr()函数:该函数用于在字符串s1中查找给定字符c。
如果成功,函数会返回指向找到字符的指针;否则,函数会返回NULL。
7.strtok()函数:该函数用于将字符串分割成一系列子字符串,每个子字符串以一个或多个指定的分隔符分隔。
以上就是标准C语言的string.h头文件中的一些常用的字符串处理函数,这些函数主要用于对字符串的操作,如切割,拼接,比较等。
由于 C 语言的数据结构比较简单,因此这个头文件函数的功能也是非常强大的,可以满足各种字符串处理的需求。
c中string函数库常用函数

c中string函数库常用函数C语言中的string函数库是一个非常重要的函数库,它包含了许多常用的字符串处理函数。
这些函数可以帮助我们更加方便地处理字符串,提高我们的编程效率。
在本文中,我们将介绍一些常用的string函数库函数。
1. strlen函数strlen函数用于计算字符串的长度,它的原型如下:size_t strlen(const char *s);其中,s是要计算长度的字符串。
该函数返回字符串s的长度,不包括字符串末尾的空字符。
2. strcpy函数strcpy函数用于将一个字符串复制到另一个字符串中,它的原型如下:char *strcpy(char *dest, const char *src);其中,dest是目标字符串,src是源字符串。
该函数将源字符串src 复制到目标字符串dest中,并返回目标字符串dest的指针。
3. strcat函数strcat函数用于将一个字符串连接到另一个字符串的末尾,它的原型如下:char *strcat(char *dest, const char *src);其中,dest是目标字符串,src是要连接的字符串。
该函数将源字符串src连接到目标字符串dest的末尾,并返回目标字符串dest的指针。
4. strcmp函数strcmp函数用于比较两个字符串是否相等,它的原型如下:int strcmp(const char *s1, const char *s2);其中,s1和s2是要比较的两个字符串。
该函数返回一个整数值,如果s1和s2相等,则返回0;如果s1小于s2,则返回一个负数;如果s1大于s2,则返回一个正数。
5. strchr函数strchr函数用于在一个字符串中查找一个字符,它的原型如下:char *strchr(const char *s, int c);其中,s是要查找的字符串,c是要查找的字符。
该函数返回一个指向第一个匹配字符的指针,如果没有找到匹配字符,则返回NULL。
c 中string的用法

在 C 语言中,字符串(String)实际上是一个字符数组,以 null 字符('\0')结尾。
C 中没有专门的字符串类型,而是使用字符数组来表示字符串。
以下是一些常见的C 中字符串的用法:
字符数组声明和初始化:
字符串输入和输出:
字符串函数:
C 标准库提供了许多用于处理字符串的函数,这些函数定义在string.h头文件中。
以下是一些常见的字符串函数:
•strlen:计算字符串的长度。
•strcpy 和 strncpy:复制字符串。
•strcat 和 strncat:连接字符串。
这只是 C 语言中字符串的基础用法。
需要注意的是,C 中的字符串是以 null 字符结尾的字符数组,因此在操作字符串时需要确保数组足够大以容纳字符串及其 null 字符。
此外,使用字符串函数时要注意数组边界,以防止缓冲区溢出。
C++-string常用函数整理(建议收藏)

C++-string常用函数整理(建议收藏)作者:翟天保Steven版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处最近刷笔试题,涉及到许多字符串相关的题目,所以将一些常用的函数整理一下,便于后面查看。
本文后续持续更新。
构造函数:1.string s; // 声明一个s字符串,为空2.string s(ss); // 声明一个s字符串,将ss字符串复制过来3.string s(ss,2); // 声明一个s字符串,将ss字符串从2位置(也就是第三个字符)后的所有字符复制过来4.string s(ss,1,3); // 声明一个s字符串,将ss字符串从1位置(也就是第二个字符)起的3个字符复制过来5.string s(chars); // 声明一个s字符串,将C字符串复制过来6.string s(chars,5); // 声明一个s字符串,将C字符串的前5个字符复制过来7.string s(6,char); // 声明一个s字符串,初值为6个char类型的字符,比如char为'a',则s为aaaaaa析构函数:1.s.~string(); // 将s字符串析构赋值函数:1.string s=c; // 赋值函数,c既可以是字符串,也可以是C字符串,比如char a[]=“123”2.s.assign(ss); // 赋值函数,ss既可以是字符串,也可以是C字符串3.s.assign(ss,1,3); // 赋值函数,ss无论是string字符串还是C字符串,均从1位置开始的3个字符赋值4.s.assign(ss,1); // 赋值函数,ss若为string字符串,则从1位置开始的所有字符赋值;若ss为C字符串,则赋值前1个字符5.s.assign(6,char); //赋值函数,赋为6个char类型的字符6.s.assign(s1.begin(),s1.end()-1); //赋值函数,将s1字符串从开始到倒数第二个字符赋值给s,string有begin和end,C字符串没有哦操作函数:1.swap(s1,s2); // 交换两个字符串2.s.size(); // 返回字符串字符个数3.s.length(); // 返回字符串字符个数4.s.clear(); // 清除所有字符5.s.empty(); // 返回字符串是否为空,若空则true,非空则false6.s.begin(); // 字符串迭代器-开始位置7.s.rbegin(); // 字符串迭代器-逆向开始位置,逆向开始是跳过结束符的第一个字符8.s.end(); // 字符串迭代器-结束位置,注意结束一般是'\0'9.s.rend(); // 字符串迭代器-逆向结束位置10.s.capacity(); // 返回已分配存储的大小11.s.resize(n); // 把字符串的长度设置为n个字符,若s字符串原本为1234,n为2,则resize后的s字符为1212.s.max_size(); // 返回字符串的最大大小13.s.capacity(); // 保留一定量内存以容纳一定数量的字符14.s.at(5); // 输出s字符串5位置的字符(第六个字符)15.getline(cin,s); // 获取一行字符串给s16.s.copy(cs,3,2); // 将s字符串第二个字符后的三个字符,复制到C字符串cs的前三个字符的位置,原字符被替换掉,cs只能是C字符串17.s.c_str(); // 将内容以C_string返回18.s.data(); // 将内容以字符数组形式返回,和c_str()输出基本一致,区别在于data()结尾不补\0,毕竟是数组形式添加函数:1.s+=s1; // 添加函数,直接添加字符串至后方,s1可以是string字符串也可以是C字符串2.s.append(ss); // 添加函数,将ss添加到后方3.s.append(ss,1,3); // 添加函数,ss无论是string字符串还是C 字符串,均将从1位置开始的3个字符添加4.s.append(ss,1); // 添加函数,ss若为string字符串,则将从1位置开始的所有字符添加;若ss为C字符串,则添加前1个字符5.s.append(6,char); //添加函数,添加6个char类型的字符6.s.append(s1.begin(),s1.end()-1); //添加函数,将s1字符串从开始到倒数第二个字符添加到s后面,string有begin和end,C字符串没有哦插入函数:1.s.insert(2,ss); // 插入函数,在s字符串的第二个字符后插入ss2.s.insert(2,ss,4); // 插入函数,若ss为string字符串,则在s字符串的第二个字符后插入ss的前四个字符;若ss为C字符串,则在s 字符串的第二个字符后插入ss第四个字符后的所有字符3.s.insert(2,ss,4,2); // 插入函数,s字符串的第二个字符后插入ss 第四个字符后的2个字符4.s.insert(2,6,char); //插入函数,s字符串的第二个字符后6个char类型的字符5.s.insert(s.begin()+3,'A'); // 插入函数,通过迭代器在s字符串第三个字符后添加1个A字符6.s.insert(s.begin()+3,6,'A'); // 插入函数,通过迭代器在s字符串第三个字符后添加6个A字符7.s.insert(s.begin()+3,ss.begin(),ss.end()); // 插入函数,通过迭代器在s字符串第三个字符后添加ss字符串从开始到结尾的所有字符删除函数:1.s.erase(s.begin()+3); // 删除函数,删除字符串第3个字符后的第一个字符,也就是第四个字符2.s.erase(s.begin(),s.begin()+3); // 删除函数,删除字符串第1到第3的所有字符,虽然s.begin()+3指向从头起第四个字符,但是最后这个字符是不删除的3.s.erase(1,3); // 删除函数,删除字符串从1位置(第二个字符)起的3个字符提取函数:1.s=ss.substr(1,3); // 提取函数,提取ss字符串从1位置起的3个字符给s字符串2.s=ss.substr(); // 提取函数,提取ss字符串的所有内容给s字符串3.s=ss.substr(3); // 提取函数,提取ss字符串从3位置起的所有内容给s字符串查找函数:1.a=s.find('A',1); // 提取函数,a是string::size_type类型,从前面1位置开始查找,查找A字符最早出现的位置,若没找到,则返回string::npos2.a=s.find(ss,1); // 提取函数,从前面1位置开始查找,查找ss 字符串最早出现在s字符串的位置,若没找到,则返回string::npos3.a=s.find(cs,1); // 提取函数,从前面1位置开始查找,查找C字符串cs最早出现在s字符串的位置,若没找到,则返回string::npos4.a=s.find(cs,1,2); // 提取函数,从前面1位置开始查找,查找C 字符串cs中前两个字符最早出现在s字符串的位置,若没找到,则返回string::npos;注意string字符串没有该格式函数5.a=s.rfind('A'); // 提取函数,从后查找,查找A字符最先出现的位置,若没找到,则返回string::npos6.a=s.find_first_of(ss); // 提取函数,从前查找,ss字符串中任一字符出现在s字符串中的位置7.a=s.find_first_of(ss,3); // 提取函数,从第3个字符后查找(即第4个字符开始),ss字符串中任一字符最先出现在s字符串中的位置8.a=s.find_last_of(ss); // 提取函数,从前查找,ss字符串中任一字符最后一次出现在s字符串中的位置;换句话说,从后查找,最先出现的位置9.a=s.find_last_of(ss,3); // 提取函数,从后面第3个字符后查找(即第4个字符开始),ss字符串中任一字符最先出现在s字符串中的位置;换句话说,在s后面第3个字符位置前的字符串中,ss任一字符最后一次出现在此字符串中的位置10.a=s.find_first_not_of(ss); // 提取函数,从前查找,任一非ss 字符串中字符最早出现在s字符串中的位置11.a=s.find_first_not_of(ss,3); // 提取函数,从第3个字符后查找(即第4个字符开始),任一非ss字符串中字符最先出现在s字符串中的位置12.a=s.find_last_not_of(ss); // 提取函数,从前查找,任一非ss 字符串中字符最后一次出现在s字符串中的位置;换句话说,从后查找,最先出现的位置13.a=s.find_last_not_of(ss,3); // 提取函数,从后面第3个字符后查找(即第4个字符开始),任一非ss字符串中字符最先出现在s 字符串中的位置;换句话说,在s后面第3个字符位置前的字符串中,任一非ss字符串中字符最后一次出现在此字符串中的位置哈哈哈,后面这几个find函数是不是看晕了,沉下心来理解一下我说的逻辑,然后自己写个测试代码试试就懂了,编程一定不要背,而是去体会和理解,这样才能活学活用~比较函数:pare(ss); // 比较函数,直接比较s和ss的字符串大小,从第一个字符开始比,若一样则下一个;若s大于ss则返回1,等于返回0,小于返回-1pare(2,2,ss); // 比较函数,用s字符串第二个字符后的两个字符作为子字符串,来和ss比较pare(2,2,ss,1,3); // 比较函数,用s字符串第二个字符后的两个字符作为子字符串,来和ss第一个字符后三个字符组成的子字符串比较替换函数:1.s.replace(1,2,ss); // 替换函数,将s字符串第一个字符后的两个字符替换为ss字符串2.s.replace(s.begin(),s.begin()+3,ss); // 替换函数,将s字符串从头起的三个字符替换为ss字符串3.s.replace(1,2,ss,2,3); // 替换函数,将s字符串第一个字符后的两个字符替换为ss字符串第二个字符后的三个字符4.s.replace(1,2,ss,2); // 替换函数,将s字符串第一个字符后的两个字符被替换,若ss为string字符串,则进行替换的字符串为ss字符串第二个字符后的所有字符;若ss为C字符串,则进行替换的字符串为ss字符串的前两个字符5.s.replace(s.begin(),s.begin()+3,ss,2); // 替换函数,将s字符串从头起的三个字符替换为C字符串ss的前两个字符,注意ss字符串只能是C字符串而不是string字符串6.s.replace(0,3,5,'A’); //替换函数,将s字符串从头起的三个字符替换为5个A字符7.s.replace(s.begin(),s.begin()+3,5,'A’); //替换函数,将s字符串从头起的三个字符替换为5个A字符总结:细心的朋友应该发现了,在string函数的调用过程中,输入参数个数、类型不同可能会带来不同的结果,而string字符串和C字符串(也就是char*)大多数情况下的调用情况一致,比如输入2个数字的时候,第一个数字往往代表了字符操作的起点位置,第二个数字代表了操作的字符个数;但是,如果只输入1个数字X的时候,就有所不同,若字符串是string,则表示从该数字X对应位置往后的所有字符进行操作,若字符串是C字符串,则表示取前X个字符进行操作。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
string类的构造函数:string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初始化此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2="hello";都是正确的写法。
当构造的string太长而无法表达时会抛出length_error异常string类的字符操作:const char &operator[](int n)const;const char &at(int n)const;char &operator[](int n);char &at(int n);operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。
const char *data()const;//返回一个非null终止的c字符数组const char *c_str()const;//返回一个以null终止的c字符串int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目string的特性描述:int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)int max_size()const; //返回string对象中可存放的最大字符串的长度int size()const; //返回当前字符串的大小int length()const; //返回当前字符串的长度bool empty()const; //当前字符串是否为空void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分string类的输入输出操作:string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作。
函数getline(istream &in,string &s);用于从输入流in中读取字符串到s中,以换行符'\n'分开。
string的赋值:string &operator=(const string &s);//把字符串s赋给当前字符串string &assign(const char *s);//用c类型字符串s赋值string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值string &assign(const string &s);//把字符串s赋给当前字符串string &assign(int n,char c);//用n个字符c赋值给当前字符串string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串string &assign(const_iterator first,const_itertor last);//把first 和last迭代器之间的部分赋给字符串string的连接:string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾string &append(const char *s); //把c类型字符串s连接到当前字符串结尾string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾string &append(const string &s); //同operator+=()string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾string &append(int n,char c); //在当前字符串结尾添加n个字符cstring &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾string的比较:bool operator==(const string &s1,const string &s2)const;//比较两个字符串是否相等运算符">","<",">=","<=","!="均被重载用于字符串的比较;int compare(const string &s) const;//比较当前字符串和s的大小int compare(int pos, int n,const string &s)const;//比较当前字符串从pos开始的n个字符组成的字符串与s的大小int compare(int pos, int n,const string &s,int pos2,intn2)const;//比较当前字符串从pos开始的n个字符组成的字符串与s中pos2开始的n2个字符组成的字符串的大小int compare(const char *s) const;int compare(int pos, int n,const char *s) const;int compare(int pos, int n,const char *s, int pos2) const; compare函数在>时返回1,<时返回-1,==时返回0string的子串:string substr(int pos = 0,int n = npos) const;//返回pos开始的n 个字符组成的字符串string的交换:void swap(string &s2); //交换当前字符串与s2的值string类的查找函数:int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置//查找成功时返回所在位置,失败返回string::npos的值int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置int rfind(const char *s, int pos = npos) const;int rfind(const char *s, int pos, int n = npos) const;int rfind(const string &s,int pos = npos) const;//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值int find_first_of(char c, int pos = 0) const;//从pos开始查找字符c第一次出现的位置int find_first_of(const char *s, int pos = 0) const;int find_first_of(const char *s, int pos, int n) const;int find_first_of(const string &s,int pos = 0) const;//从pos开始查找当前串中第一个在s的前n个字符组成的数组里的字符的位置。
查找失败返回string::nposint find_first_not_of(char c, int pos = 0) const;int find_first_not_of(const char *s, int pos = 0) const;int find_first_not_of(const char *s, int pos,int n) const;int find_first_not_of(const string &s,int pos = 0) const;//从当前串中查找第一个不在串s中的字符出现的位置,失败返回string::nposint find_last_of(char c, int pos = npos) const;int find_last_of(const char *s, int pos = npos) const;int find_last_of(const char *s, int pos, int n = npos) const; int find_last_of(const string &s,int pos = npos) const;int find_last_not_of(char c, int pos = npos) const;int find_last_not_of(const char *s, int pos = npos) const;int find_last_not_of(const char *s, int pos, int n) const;int find_last_not_of(const string &s,int pos = npos) const;//find_last_of和find_last_not_of与find_first_of和find_first_not_of相似,只不过是从后向前查找string类的替换函数:string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串sstring &replace(int p0, int n0,const char *s, int n);//删除p0开始的n0个字符,然后在p0处插入字符串s的前n个字符string &replace(int p0, int n0,const string &s);//删除从p0开始的n0个字符,然后在p0处插入串sstring &replace(int p0, int n0,const string &s, int pos, int n);//删除p0开始的n0个字符,然后在p0处插入串s中从pos开始的n个字符string &replace(int p0, int n0,int n, char c);//删除p0开始的n0个字符,然后在p0处插入n个字符cstring &replace(iterator first0, iterator last0,const char*s);//把[first0,last0)之间的部分替换为字符串sstring &replace(iterator first0, iterator last0,const char *s, int n);//把[first0,last0)之间的部分替换为s的前n个字符string &replace(iterator first0, iterator last0,const string &s);//把[first0,last0)之间的部分替换为串sstring &replace(iterator first0, iterator last0,int n, char c);//把[first0,last0)之间的部分替换为n个字符cstring &replace(iterator first0, iterator last0,const_iterator first, const_iterator last);//把[first0,last0)之间的部分替换成[first,last)之间的字符串string类的插入函数:string &insert(int p0, const char *s);string &insert(int p0, const char *s, int n);string &insert(int p0,const string &s);string &insert(int p0,const string &s, int pos, int n);//前4个函数在p0位置插入字符串s中pos开始的前n个字符string &insert(int p0, int n, char c);//此函数在p0处插入n个字符citerator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符void insert(iterator it, int n, char c);//在it处插入n个字符cstring类的删除函数iterator erase(iterator first, iterator last);//删除[first,last)之间的所有字符,返回删除后迭代器的位置iterator erase(iterator it);//删除it指向的字符,返回删除后迭代器的位置string &erase(int pos = 0, int n = npos);//删除pos开始的n个字符,返回修改后的字符串string类的迭代器处理:string类提供了向前和向后遍历的迭代器iterator,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。