文章目录
在C++中,处理字符串类型的头文件主要有以下几个:
#include <string>
:用于标准的C++字符串类std::string
,提供了许多成员函数和操作符来处理字符串。#include <cstring>
:用于C风格字符串处理的库函数,例如strlen
,strcpy
,strcat
等。#include <sstream>
:用于字符串流std::stringstream
,可以方便地进行字符串与其他类型之间的转换。#include <cctype>
:用于字符处理的库函数,例如isdigit
,isalpha
,tolower
,toupper
等。#include <algorithm>
:虽然不是专门用于字符串处理的,但其中的一些算法如std::find
,std::sort
等可以用来处理字符串。
下面是每个头文件的简要说明和一些示例:
1. 基本使用 #include <string>
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, World!";
std::cout << str << std::endl;
std::cout << "Length: " <