C++string类标准库常用函数

合集下载

c++的string的find函数

c++的string的find函数

c++的string的find函数摘要:1.引言2.C++中的string 类型3.string 的find 函数介绍4.find 函数的参数5.find 函数的返回值6.find 函数的用法示例7.总结正文:C++是一种通用的编程语言,广泛应用于各种领域。

在C++中,string 类型是一个用于表示文本数据的重要数据类型。

string 类型提供了许多方便的函数来处理文本数据,其中find 函数是用来查找给定子串或字符在字符串中首次出现的位置。

string 的find 函数是一个重要的字符串查找函数。

它可以在给定字符串中查找子串或字符首次出现的位置。

这个函数的参数包括:- 要查找的子串或字符(需要查找的文本)- 子串或字符在字符串中首次出现的位置(起始位置)find 函数的返回值是一个size_t 类型的数值,表示子串或字符在字符串中首次出现的位置。

如果未找到指定的子串或字符,函数将返回string::npos,即表示查找失败。

下面通过一个示例来说明find 函数的使用:```cpp#include <iostream>#include <string>int main() {std::string str = "C++的string 的find 函数";std::string sub_str = "的";size_t pos = str.find(sub_str);if (pos != string::npos) {std::cout << "找到子串"" << sub_str << "",位置为:" << pos << std::endl;} else {std::cout << "未找到子串"" << sub_str << """ << std::endl;}return 0;}```在这个示例中,我们定义了一个字符串str,并在其中查找子串"的"。

cstrcat函数

cstrcat函数

cstrcat函数c语言中的strcat函数是string concatenation(字符串连接)的缩写,用于将两个字符串连接起来。

它是C标准库中的一个常用函数,在头文件<string.h>中声明。

strcat函数的原型如下:char *strcat(char *dest, const char *src);其中,参数dest是目标字符串,src是要追加的源字符串。

函数在将源字符串追加到目标字符串的末尾之后,返回指向目标字符串的指针(即dest的值),这样做的目的是为了可以进行链式追加。

下面是strcat函数的实现原理以及一些注意事项:1. strcat函数的实现原理:a.首先,寻找目标字符串的结尾位置,也就是'\0'所代表的位置。

b.然后,将源字符串的字符逐个复制到目标字符串结尾的位置,直到遇到源字符串的'\0'(字符串的结束标志)。

2.注意事项:a. 目标字符串必须具有足够的空间来容纳源字符串的字符,否则会导致buffer overflow(缓冲区溢出)的错误。

b.目标字符串必须以'\0'(空字符)结尾,否则函数无法正确识别目标字符串的结尾位置。

c.目标字符串和源字符串不能重叠,即它们的内存地址不能有重叠部分。

下面是一个示例代码,演示了如何使用strcat函数:#include <stdio.h>#include <string.h>int maichar dest[100] = "Hello, ";const char src[] = "world!";printf("Before strcat: %s\n", dest);strcat(dest, src);printf("After strcat: %s\n", dest);return 0;输出:Before strcat: Hello,After strcat: Hello, world!在上面的代码中,首先定义了一个长度为100的目标字符串dest,并初始化为"Hello, "。

C标准库源码解剖(4)字符串处理函数stringh和wcharh

C标准库源码解剖(4)字符串处理函数stringh和wcharh

C标准库源码解剖(4)字符串处理函数stringh和wcharh tring.h中包含了所有的字符串处理函数,也包含了内存处理函数,因为这些内存处理函数(如比如、复制、搜索)的功能与字符串处理函数功能类似。

我们是用通用指针来指向内存块的,通用指针可以用char某类型(传统C语言),也可以用void某类型(标准C语言)。

每个函数都有对应的宽字符版本,在wchar.h中。

tring.h中包含的标准库函数:trcat,trncat,trcmp,trncmp,trcpy,trncpy,trlen,trchr,trrchr,tr pn,trcpn,trpbrk,trtr,trok,trcoll,tr某frm,trerror;memcpy,memmove,memcmp,memchr,memeet。

GNU还提供了很多非标准的扩展,如memccpy,rawmemchr,memrchr,trdup,trndup等。

viewplaincopytoclipboardprint1./某ISOC99Standard:7.21字符串处理<tring.h>某/2.#ifndef_STRING_H3.#define_STRING_H14.#include<feature.h>/某非标准头文件,定义了一些编译选项某/5.__BEGIN_DECLS6./某从<tddef.h>中获得ize_t和NULL某/7.#define__need_ize_t8.#define__need_NULL9.#include<tddef.h>10.__BEGIN_NAMESPACE_STD11./某从SRC中复制N个字节的内容到DEST中某/12.e某ternvoid某memcpy(void某__retrict__det,13.__contvoid某__retrict__rc,ize_t__n)14.__THROW__nonnull((1,2));15./某从SRC中复制N个字节的内容到DEST中,保证对重叠字符串(即SRC与DEST共用存储空间)有正确的行为某/16.e某ternvoid某memmove(void某__det,__contvoid某__rc,ize_t__n)17.__THROW__nonnull((1,2));18.__END_NAMESPACE_STD19./某从SRC中复制不超过N个字节的内容到DEST中,当遇到字符C返回DEST中C的拷贝后面的字符指针。

c语言string函数详解

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函数库常用函数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常用函数整理(建议收藏)

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个字符进行操作。

string的各种函数(系统学习)

string的各种函数(系统学习)

string的各种函数(系统学习)1、按照⾯向对象的要求,可以把字符串看作⼀个对象,设计⼀个串类加以描述。

但是,在⼀般的情况下不必建⽴⾃⼰的串类,c++标准在库<string>中给出了类string,提供了丰富的串操作,程序员使⽤指令: #include<string>即存取这个类。

可将类string视为⼀个容器类,其部分操作如下:构造函数:string s:构造⼀个空串sstring s(a):构造串s,并⽤字符数组a初始化string s(a,n):构造串s,⽤字符数组a的前n个字符初始化string s(str):⽤串str构造串s(拷贝初始化)看下⾯代码验证#include<iostream>#include<string.h>#include<map>#include<cstdio>#include<cstring>#include<stdio.h>#include<cmath>#include<ctype.h>#include<math.h>#include<algorithm>#include<set>#include<queue>typedef long long ll;using namespace std;const ll mod=1e9+7;const int maxn=1e5+10;const int maxk=3e5+10;const int maxx=1e4+10;const ll maxe=1000+10;#define INF 0x3f3f3f3f3f3f#define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1int main(){char a[maxn];cin>>a;//输⼊abcdefghstring s(a);//结果为abcdefghstring s1(a,5);//结果为abcdestring s2(s1);//结果为abcdecout<<s<<endl;cout<<s1<<endl;cout<<s2<<endl;return0;}输⼊输出:getline(cin,s,delim):从cin中提取字符存⼊串s中,当遇到delim或者提取的字符个数等于s的长度时结束,如果⾃⼰不设定,默认为'\n' getline(cin,s):遇到'\n'结束cin>>s:遇到空⽩字符终⽌提取,空⽩字符不被读取cout<<s:输出s#include<iostream>#include<string.h>#include<map>#include<cstdio>#include<cstring>#include<stdio.h>#include<cmath>#include<ctype.h>#include<math.h>#include<algorithm>#include<set>#include<queue>typedef long long ll;using namespace std;const ll mod=1e9+7;const int maxn=1e5+10;const int maxk=3e5+10;const int maxx=1e4+10;const ll maxe=1000+10;#define INF 0x3f3f3f3f3f3f#define Lson l,mid,rt<<1#define Rson mid+1,r,rt<<1|1int main(){string s;while(getline(cin,s,'#')){getchar();//⾃⼰指定了字符要加getchar,不然结果会多出⼀个换⾏,但是默认情况下不⽤加getcharcout<<s<<endl;}return0;}操作符:s=val:赋值操作,val可以是⼀个串,⼀个字符或⼀个字符数组s+=val:将val添加到串s之后s[pos]:下标操作s+t,t+s:s和t的连接,返回连接的结果。

c string类的常用方法

c string类的常用方法

c string类的常用方法在C语言中,字符串通常表示为字符数组,而不是像C++那样有专门的字符串类。

但C标准库中提供了一些用于操作和处理字符串的函数。

以下是其中的一些常用方法:1. strcpy():将一个字符串复制到另一个字符串。

```cchar str1[50];strcpy(str1, "Hello, World!");```2. strcat():将一个字符串连接到另一个字符串的末尾。

```cchar str1[50] = "Hello, ";strcat(str1, "World!");```3. strlen():返回字符串的长度,不包括终止字符'\0'。

```cchar str[] = "Hello, World!";printf("%d", strlen(str)); // 输出 13```4. strcmp():比较两个字符串。

```cchar str1[] = "Hello";char str2[] = "World";if (strcmp(str1, str2) < 0) {printf("%s is less than %s\n", str1, str2);} else if (strcmp(str1, str2) > 0) {printf("%s is greater than %s\n", str1, str2); } else {printf("%s is equal to %s\n", str1, str2);}```5. strstr():在字符串中查找子串。

```cchar str[] = "Hello, World!";char sub[] = "World";char result = strstr(str, sub);if (result != NULL) {printf("Found '%s' in '%s'\n", sub, str);} else {printf("Did not find '%s' in '%s'\n", sub, str); }```6. sprintf():将格式化的数据写入字符串。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

C++ string类标准库常用函数[string类的构造函数]string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初始化[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_iterator 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,int n2)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,==时返回0。

[string的子串]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::nposint 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是从后向前查找[string类的替换函数]string &replace(int p0, int n0,const char *s);//删除从p0开始的n0个字符,然后在p0处插入串s string &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处插入串s string &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个字符c string &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个字符c[string类的删除函数]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,迭代器提供了访问各个字符的语法,类似于指针操作,迭代器不检查范围。

相关文档
最新文档