C++常用字符串函数大全(2)

来自:互联网
时间:2021-11-03
阅读:

1、cstring.h常用函数介绍

cstring.h库即C语言中的string.h库,它是C语言中为字符串提供的标准库。C++对此进行了兼容,所以我们在C++当中一样可以使用。

这个库当中有大量的关于字符串操作的api,本文选择了其中最常用的几个进行阐述。

2、strlen

由于编译器是按照\0的位置来确定字符串的结尾的,所以字符串的长度并不等于数组的长度。

我们可以使用strlen函数求得字符串的真实长度:

char name[100] = "hello world";
cout << strlen(name) << endl;


比如我们这里用一个长度为100的char数组存储了“helloworld”字符串,当我们使用strlen函数求它的实际长度只有11。

3、strcat

strcat函数可以将两个字符串进行拼接,它的函数签名为:

char *strcat(char *dest, const char *src)


我们可以看到它接受两个参数,一个是dest,一个是src,都是char*类型,返回的结果也为char *类型。在C++当中,数组名是指向数组中第一个元素的常量指针。所以虽然签名中写的参数是指针类型,但我们传入数组名同样可以。

我们传入两个字符串之后,strcat函数会将src字符串拼接在dest字符串末尾,并且返回指向拼接之后结果的指针。

所以下面两种方式输出结果得到的值是一样的。

char name[100] = "hello world";
char level[100] = "concat test";

char *ret = strcat(name, level);
cout << ret << endl; // 方式1
cout << name << endl; // 方式2

4、strncat

strcat函数的变种,函数额外多接收一个参数控制拷贝src字符串的最大长度。

char *strncat(char *dest, const char *src, size_t n)


我们使用刚才同样的例子:

char name[100] = "hello world";
char level[100] = "concat test";

char *ret = strncat(name, level, 4);
cout << ret << endl;
cout << name << endl;


由于我们传入了4,限制了level字符串拷贝的长度,所以最终得到的结果为:hello worldconc。

5、strcpy

字符串拷贝函数,可以将src字符串中的内容复制到dest

char *strcpy(char *dest, const char *src)


使用方法和前面介绍的其他函数类似,有两点需要注意。

如果dest字符串长度不够长,在编译时不会报错,但运行时可能导致问题。

char name[10] = "";
char level[100] = "concat test";

strcpy(name, level);
cout << name << endl;


上面这段代码可以编译运行,但是运行的时候终端会出现出错信息。

C++常用字符串函数大全(2)

所以在使用strcpy的时候千万小心,一定要保证dest有足够长度。

如果dest中原本就有内容,会被覆盖。

char name[15] = "abc";
char level[100] = "concat test";

strcpy(name, level);
cout << name << endl;


运行完strcpy之后,name中的内容会被清空。

6、strncpy

strcpy加入长度限制的版本,可额外多传入一个参数n表示最多赋值n个字符。当src长度小于n时,剩余部分将会使用空字节填充。

char *strncpy(char *dest, const char *src, size_t n)
char name[15] = "abc";
char level[100] = "concat test";

strncpy(name, level, 4);
cout << name << endl;

输出结果为conc。

7、memset

字符串的批量设置函数,可以将字符串批量设置成某一个字符。

void *memset(void *str, int c, size_t n)


int c表示要被设置的字符,size_t n表示设置的字符数量。

char name[15] = "abc";
char level[100] = "concat test";

memset(name, 'c', 10);
cout << name << endl;


上述代码的运行结果为10个c。

多说一句,memset除了可以用来给字符串进行批量设置之外也可以给int型的数组进行批量设置。由于一个32位的int占据4个字节,也就是4个字符长度。所以使用memset进行批量设置的时候,最终得到的结果将是4个传入的int c拼接的结果。

int a[100];
memset(a, 1, sizeof a); // memset(a, 1, 400); 因为一个int占据4个字节


我们这样设置之后,a数组当中的元素并不是1,而是0x01010101,转成10进制是16843009

所以使用memsetint型数组进行初始化一般只用3种操作:

// 1. 批量设置成0
memset(a, 0, sizeof a);
// 2. 批量设置成-1
memset(a, -1, sizeof a);
// 3. 批量设置成一个接近最大整数上限的值
memset(a, 0x7f, sizeof a);
memset(a, 0x3f, sizeof a);


关于memset使用的一些具体细节将在后续题解的实际问题当中再做详细说明。

8、memcpy

strcpy类似,从str2中复制N个字符到str1中。

void *memcpy(void *str1, const void *str2, size_t n)
char name[15] = "abc";
char level[100] = "concat test";

memcpy(name, level, 10);


9、strcmp

将两个字符串按照字典顺序进行比较,对于字典序的顺序定义为:两个字符串自左向右逐个字符相比(按 ASCII 值大小相比较),直到出现不同的字符或遇 \0 为止。

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


返回的结果为一个int,如果它小于0,说明str1小于str2,如果它等于0,说明两者相等,如果大于0,说明str1大于str2

char name[15] = "abc";
char level[100] = "abcd";

cout << strcmp(name, level) << endl;


运行结果为-100,说明name小于level。

10、strncmp

strcmp长度限制版,可以额外接受一个数字n,表示最多比较长度为n的字符。

int strncmp(const char *str1, const char *str2, size_t n)

11、strstr

char *strstr(const char *haystack, const char *needle)


haystack中搜索needle第一次出现的位置,返回指向该位置的指针。

char name[15] = "search-test";
char level[100] = "-";

char *ret = strstr(name, level);
cout << ret << endl;

运行之后,屏幕输出的结果为:

C++常用字符串函数大全(2)

因为当我们使用cout输出一个char *变量的时候,它会当做是字符串进行输出,即一直输出字符,直到遇见\0为止。

输出的结果为-test,说明我们通过strstr函数找到了“-”出现的位置,跳过了之前的内容。

除了上文介绍的这些函数之外,cstring当中还有很多其他的api,例如strtok,memmove等等,大家感兴趣不妨去翻阅相关文档,也许会有惊喜。

注:文章转自微信公众号:Coder梁(ID:Coder_LT)

返回顶部
顶部