目录

  1. 字符串概述
  2. C 风格字符串
  3. C++ 字符串类 std::string
  4. 字符串操作
  5. 字符串与字符数组的区别
  6. 字符串常用函数
  7. 字符串输入输出
  8. 参考资料

1. 字符串概述

在 C++ 中,字符串 是一种用于存储文本数据的数据结构。C++ 提供了两种主要的字符串表示方式:

  • C 风格字符串:以 null 终止的字符数组(char[])。
  • C++ 字符串类 std::string:C++ 标准库中的字符串类,提供了更强大、更方便的字符串处理功能。

我们将分别介绍这两种方式,并展示它们的常见使用方法。


2. C 风格字符串

C 风格字符串是通过字符数组来表示的字符串。它是一个字符数组,以空字符('\0')作为结束符。C 风格字符串的操作需要手动管理内存和字符串的结束符。

2.1 声明和初始化 C 风格字符串

语法:

char str[] = "Hello, World!";

C 风格字符串的最后一个字符总是 '\0',表示字符串的结束。

2.2 操作 C 风格字符串

你可以使用标准库 <cstring> 中提供的函数来操作 C 风格字符串。

示例:

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    // 声明并初始化 C 风格字符串
    char str1[] = "Hello";
    char str2[] = "World";
    
    // 字符串拼接
    strcat(str1, str2);  // 拼接 str2 到 str1
    
    cout << "Concatenated string: " << str1 << endl;  // 输出拼接后的字符串

    // 字符串长度
    cout << "Length of str1: " << strlen(str1) << endl;

    return 0;
}

输出:

Concatenated string: HelloWorld
Length of str1: 10

2.3 C 风格字符串的缺点

  • 需要手动管理内存和字符串结束符('\0')。
  • 在字符串操作时容易出错(例如,数组越界)。
  • 不支持动态扩展。

3. C++ 字符串类 std::string

std::string 是 C++ 标准库提供的字符串类,能够更方便地管理字符串。std::string 自动管理内存和字符串的结束符,并提供了许多有用的函数。

3.1 声明和初始化 std::string

std::string 可以像其他数据类型一样声明和初始化。

示例:

#include <iostream>
#include <string>
using namespace std;

int main() {
    // 声明并初始化 std::string
    string str1 = "Hello";
    string str2 = "World";

    // 输出字符串
    cout << str1 << " " << str2 << endl;

    return 0;
}

输出:

Hello World

3.2 C++ 字符串的操作

std::string 提供了多种操作方法,比如拼接、获取长度、查找子字符串等。

  • 拼接字符串string str1 = "Hello"; string str2 = "World"; string result = str1 + " " + str2; cout << result << endl;
  • 获取字符串长度string str = "Hello"; cout << "Length: " << str.length() << endl;
  • 查找子字符串string str = "Hello, World!"; size_t pos = str.find("World"); if (pos != string::npos) { cout << "'World' found at position: " << pos << endl; }
  • 替换子字符串string str = "Hello, World!"; str.replace(7, 5, "C++"); cout << str << endl; // 输出 "Hello, C++!"

4. 字符串操作

std::string 提供了多种操作函数,下面列出一些常用的字符串操作。

4.1 字符串拼接

使用 + 运算符可以轻松地拼接字符串。

string str1 = "Hello";
string str2 = "World";
string result = str1 + " " + str2;
cout << result << endl;  // 输出 "Hello World"

4.2 字符串比较

可以使用 ==!=<> 等运算符进行字符串比较。

string str1 = "Hello";
string str2 = "Hello";
if (str1 == str2) {
    cout << "Strings are equal!" << endl;
}

4.3 子字符串提取

使用 substr() 函数提取子字符串。

string str = "Hello, World!";
string sub = str.substr(7, 5);  // 提取 "World"
cout << sub << endl;

4.4 查找字符或子字符串

使用 find() 函数查找字符或子字符串。

string str = "Hello, World!";
size_t pos = str.find("World");
if (pos != string::npos) {
    cout << "'World' found at position: " << pos << endl;
}


5. 字符串与字符数组的区别

  • 字符数组 是一个固定大小的字符集合,并以 '\0' 作为结束符。
  • std::string 是一个动态数组,能够自动扩展大小,并且有很多内置的成员函数,使得字符串操作更加方便和安全。

优点对比

  • 字符数组:需要手动管理内存、容易发生越界错误。
  • std::string:自动管理内存,内置很多有用的函数,处理起来更加简便和安全。

6. 字符串常用函数

以下是一些常用的 C++ 字符串类 std::string 的常用函数。

函数描述
length()返回字符串的长度。
empty()如果字符串为空,则返回 true
find(substring)查找子字符串在字符串中的位置。
substr(start, len)提取从 start 开始长度为 len 的子字符串。
replace(start, len, str)替换字符串中的一部分。
c_str()返回 C 风格的字符串(char[])。

7. 字符串输入输出

在 C++ 中,使用 cincout 来进行字符串的输入输出。

7.1 字符串输入

可以使用 cin 来输入字符串,但注意 cin 只会读取空格前的内容。

示例:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    cout << "Enter a string: ";
    cin >> str;  // 只会读取第一个空格前的内容
    cout << "You entered: " << str << endl;

    return 0;
}

7.2 输入包含空格的字符串

若想输入包含空格的字符串,可以使用 getline() 函数。

#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    cout << "Enter a string: ";
    getline(cin, str);  // 读取包含空格的整行
    cout << "You entered: " << str << endl;

    return 0;
}


8. 参考资料


总结

C++ 提供了两种主要的字符串表示方式:C 风格字符串和 std::string 类。C 风格字符串需要手动管理内存和结束符,而 std::string 提供了更强大、更方便的字符串操作功能。根据需求选择合适的方式进行字符串处理,有助于提高程序的效率和可维护性。