C语言和C++中的字符串(string)

知识内容:

1.CC++字符串简述

2.C字符串相关操作

3.C++ string类相关操作

一、CC++字符串简述

1.C语言字符串

C语言字符串是字符的数组。单字节字符串顺序存放各个字符串,并用''来表示字符串结束。在C语言库函数中,有一系列针对字符串的处理函数,比如说strcpy()、sprintf()、stoi()等,只能用于单字节字符串,当然也有一些函数用于处理Unicode字符串,比如wcscpy()、swprintf()等

 1 //C语言字符串示例 
 2 
 3 #include<stdio.h>
 4 
 5 int main()
 6 {
 7     char s1[20];
 8     scanf("%s", s1);
 9     printf("%s
", s1); 
10     
11     char s2[] = "Hello, World!";
12     printf("%s
", s2);
13     
14     return 0;
15 } 

一般遍历C语言字符串有两种方式,一种是根据字符串的大小遍历,另一种是使用指针来遍历字符串,个人推荐使用根据字符串大小来遍历字符串,这样更稳妥。

 1 //C语言字符串遍历示例 - 遍历输出字符串所有字符
 2 #include<stdio.h>
 3 #include<string.h>    //strlen()的头文件 
 4 
 5 int main()
 6 {
 7     char s[] = "Hello, World!"; 
 8     //根据字符串的大小遍历
 9     int i;
10     for(i=0;i<strlen(s);i++) 
11         printf("%c", s[i]);
12     printf("
");            
13     
14     return 0;
15 } 

2.C++的string类综述

STL的C++标准程序库中的string类,使用时不必担心内存是否充足、字符串长度等问题,并且C++中的string类作为一个类,其中集成的操作函数(方法)足以完成多数情况下的程序需求,比如说string对象可以用"="进行赋值,使用"=="进行等值比较,使用"+"进行串联。

如果要使用C++的string类必须包含头文件,并引入命名空间:

1 #include <string>
2 using namespace std;

string对象的输入方式: cingetline

 1 #include <iostream>
 2 #include <string>
 3 
 4 int main()
 5 {
 6     string s1, s2;
 7     cin >> s1;
 8     getline(cin, s2);
 9 
10     return 0;      
11 }

二、C字符串相关操作

对于C语言的字符串,有以下这些库函数:

atof()

将字符串转换成浮点数

atoi()

将字符串转换成整数

atol()

将字符串转换成长整型数

isalnum()

当字母或数字字符时, 返回真值

isalpha()

当字母字符时, 返回真值

iscntrl()

当控制字符时, 返回真值

isdigit()

当数字字符时, 返回真值

isgraph()

当非空格可打印字符时, 返回真值

islower()

当小写字母字符时, 返回真值

isprint()

当可打印字符时, 返回真值

ispunct()

当标点字符时, 返回真值

isspace()

当空格字符时, 返回真值

isupper()

当大写字母字符时, 返回真值

isxdigit()

当十六进制字符时, 返回真值

memchr()

在某一内存范围中查找一特定字符

memcmp()

比较内存内容

memcpy()

拷贝内存内容

memmove()

拷贝内存内容

memset()

将一段内存空间填入某值

strcat()

连接两个字符串

strchr()

查找某字符在字符串中首次出现的位置

strcmp()

比较两个字符串

strcoll()

采用目前区域的字符排列次序来比较字符串

strcpy()

拷贝字符串

strcspn()

在某字符串中匹配指定字符串

strerror()

返回错误码对应的文本信息

strlen()

返回指定字符串的长度

strncat()

连接某一长度的两个字符串

strncmp()

比较某一长度的两个字符串

strncpy()

复制某一长度的一个字符串到另一字符串中

strpbrk()

查找某字符串在另一字符串中首次出现的位置

strrchr()

查找某字符在字符串中末次出现的位置

strspn()

返回子串的长度,子串的字符都出现包含于另一字符串中

strstr()

在一字符串中查找指定的子串首次出现的位置

strtod()

将字符串转换成浮点数

strtok()

查找指定字符之前的子串

strtol()

将字符串转换成长整型数

strtoul()

将字符串转换成无符号长整型数

strxfrm()

转换子串, 可以用于字符串比较                             

tolower()

将字符转换成小写字符

toupper()

将字符转换成大写字符

以下是上面部分函数的详细解释:

(1)atof()

语法:

#include <stdlib.h>

double atof( const char *str );

功能:将字符串str转换成一个双精度数值并返回结果。 参数str 必须以有效数字开头,但是允许以“E”或“e”除外的任意非数字字符结尾

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     char *s = "42.03 is the answer";
 7     double x = atof(s);
 8     printf("%f", x);
 9     
10     return 0;
11 }
atof()用法

(2)atoi()

语法:

#include <stdlib.h>

int atoi(const char *str);

功能: 将字符串str转换成一个整数并返回结果。参数str以数字开头,当函数从str中读到非数字字符时则结束转换并将结果返回

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     char *s = "32 this is the answer!";
 7       int number = atoi(s);
 8      printf("%d", number);
 9 
10     return 0;
11 }
atoi()用法

(3)atol()

语法:

#include <stdlib.h>

long atol(const char *str);

功能:将字符串转换成长整型数并返回结果。函数会扫描参数str字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时才结束转换,并将结果返回

 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6    long l;
 7    char *str = "987654321";
 8 
 9    l = atol(str);
10    printf("string = %s integer = %ld
", str, l);
11    
12    return 0;
13 } 
atol()用法

(4)isalnum()

语法:

#include <ctype.h>

int isalnum(char ch);

功能:如果参数是数字或字母字符,函数返回非零值,否则返回零值

 1 #include <ctype.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     char c;
 7     scanf("%c", &c);
 8     if(isalnum(c))
 9         printf("You entered the alphanumeric character %c
", c);
10 
11    return 0;
12 } 
isalnum()用法

(5)isalpha()

语法:

#include <ctype.h>

int isalpha(char ch);

功能:如果参数是字母字符,函数返回非零值,否则返回零值

 1 #include <ctype.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     char c;
 7     scanf("%c", &c);
 8     if(isalpha(c))
 9         printf("You entered a letter of the alphabet
");
10     else 
11         printf("You entered not is a letter of the alphabet
");
12     
13    return 0;
14 } 
isalpha()用法

(6)isdigit()

语法:

#include <ctype.h>

int isdigit(char ch);

功能:如果参数是0到9之间的数字字符,函数返回非零值,否则返回零值

 1 #include <ctype.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     char c;
 7     scanf("%c", &c);
 8     if(isdigit(c))
 9           printf("You entered the digit %c
", c );
10     else
11         printf("You is not entered the digit
");
12     
13    return 0;
14 } 
isdigit()用法

(7)islower()和isupper()

语法:

#include <ctype.h>

int islower(char ch);

int isupper(char ch);

islower() -> 功能:如果参数是小写字母字符,函数返回非零值,否则返回零值

isupper() -> 功能:如果参数是大写字母字符,函数返回非零值,否则返回零值

 1 #include <ctype.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6     char c;
 7     scanf("%c", &c);
 8     if(islower(c))
 9           printf("是小写字母
");
10     if(isupper(c))
11         printf("是大写字母
");
12     
13    return 0;
14 } 
islower和isupper用法

(8)memchr()

语法:
#include <string.h>
void *memchr( const void *buffer, char ch, size_t count );
功能:函数在buffer指向的数组的count个字符的字符串里查找ch 首次出现的位置。返回一个指针,指向ch 在字符串中首次出现的位置, 如果ch 没有在字符串中找到,返回NULL
 1 #include <string.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6        char names[] = "Alan Bob Chris X Dave";
 7     if( memchr(names,'X',strlen(names)) == NULL )
 8           printf( "Didn't find an X
" );
 9     else
10           printf( "Found an X
" );
11 
12     
13    return 0;
14 } 
memchr用法

(9)memcmp()

语法:

#include <string.h>

int memcmp( const void *buffer1, const void *buffer2, size_t count );

功能:函数比较buffer1buffer2的前count 个字符。返回值如下:

Value

解释

less than 0

buffer1 is less than buffer2

equal to 0

buffer1 is equal to buffer2

greater than 0

buffer1 is greater than buffer2

(10)memcpy()和memmove()

语法:

#include <string.h>

void *memcpy( void *to, const void *from, size_t count );

void *memmove( void *to, const void *from, size_t count );

memcpy功能:函数从from中复制count 个字符到to中,并返回to指针。 如果tofrom 重叠,则函数行为不确定

memmove功能:  功能: 与mencpy相同,不同的是当tofrom 重叠,函数正常仍能工作

(11)memset()

语法:

#include <string.h>

void *memset( void *buffer, char ch, size_t count );

功能:

函数拷贝chbuffer 从头开始的count 个字符里, 并返回buffer指针。 memset() 可以应用在将一段内存初始化为某个值。例如:

    memset( the_array, '', sizeof(the_array) );

这是将一个数组的所有分量设置成零的很便捷的方法

(12)strcat()和strncat()

语法:

#include <string.h>

char *strcat( char *str1, const char *str2 );

char *strncat( char *str1, const char *str2, size_t count ); 

strcat功能: 函数将字符串str2 连接到str1的末端,并返回指针str1. 例如:

    printf( "Enter your name: " );
    scanf( "%s", name );
    title = strcat( name, " the Great" );
    printf( "Hello, %s
", title );

strncat功能: 将字符串from 中至多count个字符连接到字符串to中,追加空值结束符。返回处理完成的字符串

(13)strchr()和strrchr()

语法:

#include <string.h>

char *strchr( const char *str, char ch );

char *strrchr( const char *str, char ch );

strchr功能: 函数返回一个指向strch 首次出现的位置,当没有在str 中找ch到返回NULL

strrchr功能:  函数返回一个指针,它指向字符ch 在字符串str末次出现的位置,如果匹配失败,返回NULL

(14)strcmp()和strncmp()

语法:

#include <string.h>

int strcmp( const char *str1, const char *str2 );

int strncmp( const char *str1, const char *str2, size_t count );

strcmp功能:  比较字符串str1 and str2, 返回值如下:

返回值

解释

less than 0

str1 is less than str2

equal to 0

str1 is equal to str2

greater than 0

str1 is greater than str2

strncmp功能: 比较字符串str1str2中至多count个字符。返回值如下:

返回值

解释

less than 0

str1 is less than str2

equal to 0

str1 is equal to str2

greater than 0

str1 is greater than str2

如果参数中任一字符串长度小于count, 那么当比较到第一个空值结束符时,就结束处理

 1 #include <string.h>
 2 #include <stdio.h>
 3 
 4 int main()
 5 {
 6 printf( "Enter your name: " );
 7     scanf( "%s", name );
 8     if( strcmp( name, "Mary" ) == 0 )
 9       printf( "Hello, Dr. Mary!
" );
10 
11 return 0;
12 }
strcmp用法

(15)strcpy()和strncpy()

语法:

#include <string.h>

char *strcpy( char *to, const char *from );

char *strncpy( char *to, const char *from, size_t count );

strcpy功能:    复制字符串from 中的字符到字符串to,包括空值结束符。返回值为指针to

strncpy功能:  将字符串from 中至多count个字符复制到字符串to中。如果字符串from 的长度小于count,其余部分用''填补。返回处理完成的字符串

(16)strlen()

语法: 

#include <string.h>

size_t strlen( char *str );

功能:  函数返回字符串str 的长度

(17)strstr()

语法:

#include <string.h>

char *strstr( const char *str1, const char *str2 );

功能:  函数返回一个指针,它指向字符串str2 首次出现于字符串str1中的位置,如果没有找到,返回NULL

(18)tolower()和toupper()

语法:

#include <string.h>

char tolower(char ch );

char toupper(char ch );

功能: 将大写字母转化成小写字母,将小写字母转换成大写字母

三、C++ string类相关操作

 对于C++的string类来说,库函数定义了一系列的成员函数供我们使用,使用C++的string类来构建字符串,应包含头文件: 

#include <string>,并声明命名空间: using namespace std;    具体成员函数如下所示:

Constructors 构造函数,用于字符串初始化
Operators 操作符,用于字符串比较和赋值
append() 在字符串的末尾添加文本
assign() 为字符串赋新值
at() 按给定索引值返回字符
begin() 返回一个迭代器,指向第一个字符
c_str() 将字符串以C字符数组的形式返回
capacity() 返回重新分配空间前的字符容量
compare() 比较两个字符串
copy() 将内容复制为一个字符数组
data() 返回内容的字符数组形式
empty() 如果字符串为空,返回真
end() 返回一个迭代器,指向字符串的末尾。(最后一个字符的下一个位置)
erase() 删除字符
find() 在字符串中查找字符
find_first_of() 查找第一个与value中的某值相等的字符
find_first_not_of() 查找第一个与value中的所有值都不相等的字符
find_last_of() 查找最后一个与value中的某值相等的字符
find_last_not_of() 查找最后一个与value中的所有值都不相等的字符
get_allocator() 返回配置器
insert() 插入字符
length() 返回字符串的长度
max_size() 返回字符的最大可能个数
rbegin() 返回一个逆向迭代器,指向最后一个字符
rend() 返回一个逆向迭代器,指向第一个元素的前一个位置
replace() 替换字符
reserve() 保留一定容量以容纳字符串(设置capacity值)
resize() 重新设置字符串的大小
rfind() 查找最后一个与value相等的字符(逆向查找)
size() 返回字符串中字符的数量
substr() 返回某个子字符串
swap() 交换两个字符串的内容

以下是常用的成员函数的详细解释:

(1)Constructors  -> 构造函数,用于字符串初始化 

语法:

  • string();                
  • string( size_type length, char ch );      
  • string( const char *str );           
  • string( const char *str, size_type length );     
  • string( string &str, size_type index, size_type length ); 
  • string( input_iteartor start, input_iteartor end );   

字符串的构造函数创建一个新字符串,包括:

  • 空字符串
  • 以length为长度的ch的拷贝(即length个ch)
  • 以str为初值 (长度任意),
  • 以index为索引开始的子串,长度为length
  • 以从start到end的元素为初值.
1 string str;
2 string str1( 5, 'c' );
3 string str2( "Now is the time..." );
4 string str3( str2, 11, 4 );
5 
6 cout << str << endl;
7 cout << str1 << endl;
8 cout << str2 << endl;
9 cout << str3 << endl;

(2)Operators和at()

Operators:  string类重载了一系列的运算符,==, >, <, >=, <=, !=, +, +=, [], 可以使用这些运算符完成比较字符串、字符串相加、取值等操作

at():  按给定索引值返回字符

at()语法:  reference at( size_type index );

at()函数返回一个引用,指向在index位置的字符. 如果index不在字符串范围内, at() 将报告"out of range"错误,并抛出out_of_range异常。

 1 string str1 = "sagdfa";
 2 string str2 = "iuhkjgsaaaaf";
 3 
 4 string str = str1 +str2;
 5 
 6 cout << str <<endl;
 7 cout << str1 <<endl;
 8 cout << str2 <<endl;
 9 cout << str[0] <<endl;
10 cout << str.at(0) <<endl;

(3)c_str()

语法: const char *c_str();

用法: c_str()函数返回一个指向正规C字符串的指针, 内容与本字符串相同,把string 对象转换成c中的字符串样式

(4)compare()  -> 比较

语法:

int compare( const basic_string &str );

int compare( const char *str );

int compare( size_type index, size_type length, const basic_string &str );

int compare( size_type index, size_type length, const basic_string &str, size_type index2, size_type length2 );

int compare( size_type index, size_type length, const char *str, size_type length2 );

compare()函数以多种方式比较本字符串和str, 以上不同的函数:

  • 比较自己和str,
  • 比较自己的子串和str,子串以index索引开始,长度为length
  • 比较自己的子串和str的子串,其中index2和length2引用str,index和length引用自己
  • 比较自己的子串和str的子串,其中str的子串以索引0开始,长度为length2,自己的子串以index开始,长度为length

(5)copy()  ->  拷贝

语法: size_type copy( char *str, size_type num, size_type index );

copy()函数拷贝自己的num个字符到str中(从索引index开始),返回值是拷贝的字符数

(6)empty()  ->  判空

语法: bool empty();

如果字符串为空则empty()返回真(true),否则返回假(false).

(7)replace()  ->  替换

语法:

basic_string &replace( size_type index, size_type num, const basic_string &str );

basic_string &replace( size_type index1, size_type num1, const basic_string &str, size_type index2, size_type num2 );

basic_string &replace( size_type index, size_type num, const char *str );

basic_string &replace( size_type index, size_type num1, const char *str, size_type num2 );

basic_string &replace( size_type index, size_type num1, size_type num2, char ch );

basic_string &replace( iterator start, iterator end, const basic_string &str );

basic_string &replace( iterator start, iterator end, const char *str );

basic_string &replace( iterator start, iterator end, const char *str, size_type num );

basic_string &replace( iterator start, iterator end, size_type num, char ch );

replace()函数:

  • 用str中的num个字符替换本字符串中的字符,从index开始
  • 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,最多num1个字符
  • 用str中的num个字符(从index开始)替换本字符串中的字符
  • 用str中的num2个字符(从index2开始)替换本字符串中的字符,从index1开始,num1个字符
  • 用num2个ch字符替换本字符串中的字符,从index开始
  • 用str中的字符替换本字符串中的字符,迭代器start和end指示范围
  • 用str中的num个字符替换本字符串中的内容,迭代器start和end指示范围,
  • 用num个ch字符替换本字符串中的内容,迭代器start和end指示范围.

(8)substr()  ->  子串

语法:  basic_string substr( size_type index, size_type num = npos );

substr()返回本字符串的一个子串,从index开始,长num个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从index开始的剩余的字符串

用substr()求子串:

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     string str = "abcdefgh";
 8     int res = 0;
 9     
10     for(int i=0;i<str.length();i++)
11     {
12         for(int j=1;j<str.length()-i;j++)
13         {
14             res++;
15             cout << res << endl;
16             string s = str.substr(i, j);
17             cout << s << endl << endl;
18         }
19     }
20     
21     cout << endl << res+1 << endl; 
22     
23     return 0;
24 }

(9)swap()  ->  交换

语法:  void swap( basic_string &str );

swap()函数把str和本字符串交换

(10)find()和rfind()  ->  查找

语法:

find():

size_type find( const basic_string &str, size_type index );

size_type find( const char *str, size_type index );

size_type find( const char *str, size_type index, size_type length );

size_type find( char ch, size_type index );

rfind():

size_type rfind( const basic_string &str, size_type index );

size_type rfind( const char *str, size_type index );

size_type rfind( const char *str, size_type index, size_type num );

size_type rfind( char ch, size_type index );

find()函数:

  • 返回str在字符串中第一次出现的位置(从index开始查找)如果没找到则返回string::npos,
  • 返回str在字符串中第一次出现的位置(从index开始查找,长度为length)。如果没找到就返回string::npos,
  • 返回字符ch在字符串中第一次出现的位置(从index开始查找)。如果没找到就返回string::npos

rfind()函数:

  • 返回最后一个与str中的某个字符匹配的字符,从index开始查找。如果没找到就返回string::npos
  • 返回最后一个与str中的某个字符匹配的字符,从index开始查找,最多查找num个字符。如果没找到就返回string::npos
  • 返回最后一个与ch匹配的字符,从index开始查找。如果没找到就返回string::npos

(11)erase()  ->  删除

语法:

iterator erase( iterator pos );

iterator erase( iterator start, iterator end );

basic_string &erase( size_type index = 0, size_type num = npos );

erase()函数可以:

  • 删除pos指向的字符, 返回指向下一个字符的迭代器,
  • 删除从start到end的所有字符, 返回一个迭代器,指向被删除的最后一个字符的下一个位置
  • 删除从index索引开始的num个字符, 返回*this.

参数index 和 num 有默认值, 这意味着erase()可以这样调用:只带有index以删除index后的所有字符,或者不带有任何参数以删除所有字符

(12)insert()  ->  插入

语法:

iterator insert( iterator i, const char &ch ); basic_string &insert( size_type index, const basic_string &str );

basic_string &insert( size_type index, const char *str );

basic_string &insert( size_type index1, const basic_string &str, size_type index2, size_type num );

basic_string &insert( size_type index, const char *str, size_type num );

basic_string &insert( size_type index, size_type num, char ch );

void insert( iterator i, size_type num, const char &ch );

void insert( iterator i, iterator start, iterator end );

insert()函数的功能非常多:

  • 在迭代器i表示的位置前面插入一个字符ch
  • 在字符串的位置index插入字符串str
  • 在字符串的位置index插入字符串str的子串(从index2开始,长num个字符)
  • 在字符串的位置index插入字符串str的num个字符
  • 在字符串的位置index插入num个字符ch的拷贝
  • 在迭代器i表示的位置前面插入num个字符ch的拷贝
  • 在迭代器i表示的位置前面插入一段字符,从start开始,以end结束

(13)append()  ->  添加

append语法:

basic_string &append( const basic_string &str );

basic_string &append( const char *str );

basic_string &append( const basic_string &str, size_type index, size_type len );

basic_string &append( const char *str, size_type num );

basic_string &append( size_type num, char ch );

basic_string &append(input_iterator start, input_iterator end );

append() 函数可以完成以下工作:

  • 在字符串的末尾添加str
  • 在字符串的末尾添加str的子串,子串以index索引开始,长度为len
  • 在字符串的末尾添加str中的num个字符
  • 在字符串的末尾添加num个字符ch
  • 在字符串的末尾添加以迭代器start和end表示的字符序列
 1 string str = "start string";
 2 string s = "append";
 3 
 4 string ss = str.append(s);
 5 cout << ss <<endl;
 6 string ss = str.append("append");
 7 cout << ss <<endl;
 8 string ss = str.append(str, 5, 1);
 9 cout << ss <<endl;
10 string ss = str.append("append", 5);
11 cout << ss <<endl;
12 string ss = str.append(5, 'a');
13 cout << ss <<endl;

(14)assign()  ->  赋值

assign语法:

basic_string &assign( const basic_string &str );

basic_string &assign( const char *str );

basic_string &assign( const char *str, size_type num );

basic_string &assign( const basic_string &str, size_type index, size_type len );

basic_string &assign( size_type num, char ch );

函数以下列方式赋值:

  • 用str为字符串赋值
  • 用str的开始num个字符为字符串赋值
  • 用str的子串为字符串赋值,子串以index索引开始,长度为len
  • 用num个字符ch为字符串赋值
 1 string str1, str2 = "War and Peace";
 2 str1.assign(str2);
 3 cout << str1 << endl;
 4 str1.assign("aabbccdd");
 5 cout << str1 << endl;
 6 str1.assign("aabbccddee", 5);
 7 cout << str1 << endl;
 8 str1.assign( str2, 4, 3 );  
 9 cout << str1 << endl;
10 str1.assign('A', 5);
11 cout << str1 << endl;

(15)find_first_of()和find_first_not_of()  ->  查找第一个满足条件的字符

语法:

find_first_of():

size_type find_first_of( const basic_string &str, size_type index = 0 );

size_type find_first_of( const char *str, size_type index = 0 );

size_type find_first_of( const char *str, size_type index, size_type num );

size_type find_first_of( char ch, size_type index = 0 );

find_first_not_of():

size_type find_first_not_of( const basic_string &str, size_type index = 0 );

size_type find_first_not_of( const char *str, size_type index = 0 );

size_type find_first_not_of( const char *str, size_type index, size_type num );

size_type find_first_not_of( char ch, size_type index = 0 );

find_first_of()函数:

  • 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos
  • 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::npos,
  • 查找在字符串中第一个与ch匹配的字符,返回它的位置。搜索从index开始

find_first_not_of()函数:

  • 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
  • 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符。如果没找到就返回string::nops
  • 在字符串中查找第一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops

(16)find_last_of()和find_last_not_of()  ->  查找最后一个满足条件的字符

语法:

find_last_of():

size_type find_last_of( const basic_string &str, size_type index = npos );

size_type find_last_of( const char *str, size_type index = npos );

size_type find_last_of( const char *str, size_type index, size_type num );

size_type find_last_of( char ch, size_type index = npos );

find_last_not_of():

size_type find_last_not_of( const basic_string &str, size_type index = npos );

size_type find_last_not_of( const char *str, size_type index = npos);

size_type find_last_not_of( const char *str, size_type index, size_type num );

size_type find_last_not_of( char ch, size_type index = npos );

find_last_of()函数:

  • 在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
  • 在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::nops
  • 在字符串中查找最后一个与ch匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops

find_last_not_of()函数:

  • 在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops
  • 在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符如果没找到就返回string::nops
  • 在字符串中查找最后一个与ch不匹配的字符,返回它的位置。搜索从index开始。如果没找到就返回string::nops

(17)begin()和end()和rbegin()和rend()  ->  迭代器相关函数

begin语法:  iterator begin();    用法:  begin()函数返回一个迭代器,指向字符串的第一个元素

end语法:  iterator end();          用法:  end()函数返回一个迭代器,指向字符串的最后一个元素

rbegin语法: const reverse_iterator rbegin();  用法:  rbegin()返回一个逆向迭代器,指向字符串的最后一个字符

rend语法:    const reverse_iterator rend();  用法:  rend()函数返回一个逆向迭代器,指向字符串的开头(第一个字符的前一个位置)

(18)length()和size()和max_size()  ->  大小

length():  返回字符串的长度. 这个数字应该和size()返回的数字相同

size():     函数返回字符串中现在拥有的字符数

max_size(): 返回字符串能保存的最大字符数

原文地址:https://www.cnblogs.com/wyb666/p/8644342.html