C++ #include <cstring>(详解)

里面有疑问或者不正确的地方可以给我留言。

#include <cstring> 是在 C++ 程序中引入头文件 <cstring> 的语句。这个头文件提供了用于处理 C 风格字符串(即以空字符 '\0' 结尾的字符数组)的函数。

具体来说,<cstring> 定义了一些常用的字符串操作函数,如:

(1)strcpy: 复制字符串。

strcpycstring 中用于复制 C 风格字符串的函数,它将源字符串的内容复制到目标字符串中,包括末尾的空字符 ('\0')。使用 strcpy 时需要确保目标数组有足够的空间来存放源字符串。

char* strcpy(char* destination, const char* source);
  • destination:目标字符数组,存放复制后的字符串。
  • source:源字符数组,被复制的字符串。

示例代码:

#include <iostream>
#include <cstring>

int main() {
    // 创建源字符串
    const char source[] = "Hello, World!";
    
    // 创建目标数组,确保大小足够容纳源字符串
    char destination[20];
    
    // 使用 strcpy 复制字符串
    std::strcpy(destination, source);
    
    // 输出结果
    std::cout << "Source: " << source << std::endl;
    std::cout << "Destination: " << destination << std::endl;

    return 0;
}

输出:

Source: Hello, World!
Destination: Hello, World!

注意:

  • destination 数组必须足够大,以容纳 source 字符串及其末尾的空字符 ('\0')。
  • 如果 destination 的空间不足,strcpy 不会进行检查,可能导致缓冲区溢出等问题。因此,程序员要手动确保内存安全。

(2)strncpy: 复制指定长度的字符串。

strncpycstring 中的一个函数,用于从源字符串复制指定长度的字符到目标字符串。与 strcpy 不同的是,strncpy 只会复制指定长度的字符,且如果源字符串的长度小于指定长度,则会在目标字符串中填充空字符 ('\0') 以确保长度一致。

char* strncpy(char* destination, const char* source, size_t num);
  • destination:目标字符数组,存放复制后的字符串。
  • source:源字符数组,被复制的字符串。
  • num:要复制的字符数。

示例代码:

#include <iostream>
#include <cstring>

int main() {
    // 创建源字符串
    const char source[] = "Hello, World!";
    
    // 创建目标数组,确保大小足够
    char destination[20];
    
    // 使用 strncpy 复制指定长度的字符串
    std::strncpy(destination, source, 5);
    
    // 手动添加空字符,以确保目标字符串正确结束
    destination[5] = '\0';
    
    // 输出结果
    std::cout << "Source: " << source << std::endl;
    std::cout << "Destination: " << destination << std::endl;

    return 0;
}

输出:

Source: Hello, World!
Destination: Hello

说明:

  • strncpy(destination, source, 5) 只复制源字符串的前 5 个字符 Hello 到目标数组。
  • 手动添加 destination[5] = '\0'; 是为了确保目标字符串以空字符结尾,否则在输出时可能会导致意外行为。
  • 如果 source 字符串的长度小于 num,则 strncpy 会在目标数组中补充空字符 (\0) 直到复制了 num 个字符。

注意:

  • strncpy 不会自动添加结尾的空字符 ('\0'),因此需要手动添加。
  • 如果源字符串的长度小于指定的字符数,目标字符串会用空字符填充。

(3)strcmp: 比较两个字符串。

strcmpcstring 中的一个函数,用于按字典序比较两个 C 风格字符串。它比较的是字符串的 ASCII 值,并返回一个整数,表示两个字符串的相对顺序。

  • str1str2:要比较的两个字符串。
  • 返回值:
    • 0:str1str2 相等。
    • 正值:str1 大于 str2
    • 负值:str1 小于 str2

示例代码:

#include <iostream>
#include <cstring>

int main() {
    // 定义两个字符串
    const char str1[] = "apple";
    const char str2[] = "banana";
    
    // 使用 strcmp 比较两个字符串
    int result = std::strcmp(str1, str2);
    
    // 输出比较结果
    if (result == 0) {
        std::cout << "str1 and str2 are equal." << std::endl;
    } else if (result > 0) {
        std::cout << "str1 is greater than str2." << std::endl;
    } else {
        std::cout << "str1 is less than str2." << std::endl;
    }

    return 0;
}

输出:

str1 is less than str2.

说明:

  • 在这个例子中,strcmp("apple", "banana") 返回一个负值,因为在字典序(基于 ASCII 值)中,a(ASCII 值为 97)比 b(ASCII 值为 98)小,因此 apple 小于 banana

strcmp 的返回值详解:

  • 如果 strcmp 返回 0,表示两个字符串相等。
  • 如果返回正值,表示 str1 在字典序中比 str2 大。
  • 如果返回负值,表示 str1 在字典序中比 str2 小。
举几个简单例子:
  • strcmp("apple", "apple") == 0:两个字符串相等。
  • strcmp("apple", "banana") < 0apple 小于 banana
  • strcmp("banana", "apple") > 0banana 大于 apple

(4)strlen: 获取字符串的长度。

strlencstring 中的一个函数,用于计算 C 风格字符串的长度(不包括结尾的空字符 '\0')。它返回的是字符串中字符的个数。

size_t strlen(const char* str);
  • str:要计算长度的字符串。
  • 返回值:字符串的长度(不包括终止的空字符 '\0')。

示例代码:

#include <iostream>
#include <cstring>

int main() {
    // 定义一个字符串
    const char str[] = "Hello, World!";
    
    // 使用 strlen 计算字符串长度
    size_t length = std::strlen(str);
    
    // 输出字符串长度
    std::cout << "The length of the string \"" << str << "\" is " << length << "." << std::endl;

    return 0;
}

输出:

The length of the string "Hello, World!" is 13.

说明:

  • 在这个例子中,strlen("Hello, World!") 返回 13,因为 "Hello, World!" 中有 13 个字符(不包括字符串末尾的空字符 '\0')。
  • strlen 函数遍历字符串直到遇到终止空字符 '\0',然后返回遍历的字符数。

重要注意事项:

  • strlen 计算的长度不包括字符串的结束符 '\0'
  • 如果传递给 strlen 的字符串没有以 '\0' 结尾(即不是合法的 C 风格字符串),它可能导致未定义行为,因为 strlen 函数会继续读取内存,直到找到 '\0'。因此,确保传递的字符串是正确终止的非常重要。

(5)strcat: 拼接字符串。

strcatcstring 中的一个函数,用于将一个 C 风格字符串追加到另一个 C 风格字符串的末尾。它会将源字符串的内容附加到目标字符串的末尾,包括结尾的空字符 '\0'

语法

char* strcat(char* destination, const char* source);
  • destination:目标字符数组,原有内容会被保留,源字符串会被追加到其末尾。
  • source:要追加的源字符串。
  • 返回值:返回目标字符串 destination 的指针。
#include <iostream>
#include <cstring>

int main() {
    // 定义目标字符串数组和源字符串
    char destination[50] = "Hello, ";
    const char source[] = "World!";
    
    // 使用 strcat 拼接字符串
    std::strcat(destination, source);
    
    // 输出结果
    std::cout << "Resulting string: " << destination << std::endl;

    return 0;
}

输出:

Resulting string: Hello, World!
  • 在这个例子中,strcat(destination, source)"World!" 追加到 "Hello, " 的末尾。
  • destination 字符数组在使用 strcat 之前已经分配了足够的空间来容纳拼接后的结果(这里是 50 个字符)。strcat 依赖于目标数组有足够的空间来存储拼接后的字符串,否则可能会导致缓冲区溢出。

注意事项:

  • 确保 destination 数组有足够的空间来存放原有内容加上追加的源字符串,以及结尾的空字符 '\0'
  • strcat 不会自动检查目标数组的大小,所以在使用 strcat 时必须手动确保目标数组足够大。否则,可能会导致内存溢出、数据损坏或其他未定义行为。

(6)strchr: 查找字符在字符串中的第一次出现位置。

strchrcstring 中的一个函数,用于在 C 风格字符串中查找指定字符第一次出现的位置。它返回一个指向找到的字符位置的指针。如果字符未找到,则返回 nullptr

语法

char* strchr(const char* str, int character);
  • str:要搜索的字符串。
  • character:要查找的字符,作为 int 类型传递,通常是 char 类型的值。
  • 返回值:返回一个指向找到的字符位置的指针;如果字符未找到,则返回 nullptr

示例代码:

#include <iostream>
#include <cstring>

int main() {
    // 定义一个字符串
    const char str[] = "Hello, World!";
    
    // 查找字符 'W' 在字符串中的第一次出现位置
    char* position = std::strchr(str, 'W');
    
    // 输出结果
    if (position != nullptr) {
        std::cout << "Character 'W' found at position: " << (position - str) << std::endl;
        std::cout << "Sub-string starting from 'W': " << position << std::endl;
    } else {
        std::cout << "Character 'W' not found." << std::endl;
    }

    return 0;
}

输出:

Character 'W' found at position: 7
Sub-string starting from 'W': World!

说明:

  • std::strchr(str, 'W') 查找字符 'W' 在字符串 "Hello, World!" 中的位置。函数返回一个指向字符 'W' 的位置的指针。
  • (position - str) 计算字符 'W' 在字符串中的位置,结果是 7(索引从 0 开始)。
  • position 指向 "World!",即从字符 'W' 开始的子字符串。

注意事项:

  • strchr 查找的是第一个匹配的字符,如果要查找所有匹配的字符,需要在找到匹配项后继续搜索。
  • 传递的字符 character 作为 int 类型,但通常以 char 类型的值传递,strchr 内部会处理这个值。

通过 #include <cstring>,你可以使用这些函数来处理 C 风格字符串。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值