HOWTO · C++

C++ 中字符串和字符的比较

一篇关于 C++ 中 string 和 char 的使用及其比较的文章。

这个简单的指南是关于在 C++ 中使用字符串以及如何将这些字符串与其他文字进行比较。在继续之前,我们将简要介绍 C++ 中的字符串。

在 C++ 中,字符串可以按两种不同的方式分类:

  1. 创建一个 Character 数组以形成一个字符串
  2. 在 C++ 中使用标准的 String

在 C++ 中创建字符数组

与 C 语言一样,C++ 也为我们提供了字符数组,即 char 数组,它可以用作字符串文字。它是一个以 null 结尾的一维字符数组。

因此,字符串是通过形成一个 char 数组并以 null 字符终止它来创建的。

请注意,要使用聚合方法(例如,cout<< charArray)打印字符数组,null 字符必须终止字符数组。C++ 使用 \0 作为 NULL 字符,它有助于聚合方法来检测字符串的结尾(如果没有一些结束标记符号就不可能检测到数组的结尾)。

因此,下面的示例声明了一个大小为 11 的字符数组,尽管 C 语言仅包含十个字符。

char word[11] = {'C', '-', 'l', 'a', 'n', 'g', 'u', 'a', 'g', 'e', '\0'};

上面的语句也可以写成下面这样,前提是你遵循初始化规则。

char word[] = "C-language";

如果你忘记将 null 字符放在末尾,编译器会隐式地将 null 字符放在末尾。让我们看看下面的程序。

#include <iostream>
using namespace std;

int main() {
  char word[11] = {'C', '-', 'l', 'a', 'n', 'g', 'u', 'a', 'g', 'e', '\0'};
  cout << "First message: ";
  cout << word << endl;
  return 0;
}

上述代码片段中的第 06 行将向输出提供 First Message:,而第 07 行将显示 word 变量中的所有字符,直到遇到\0。因此,上面的代码片段将生成以下输出。

First Message: C-language

在 C++ 中使用 String

C++ 有一个内置的 string.h 头文件作为其标准库的一部分。它提供了一系列功能(例如,strcpystrlen 等)来处理 C 风格的字符串(即,以 null 结尾的字符串)。

请注意,所有没有 .h 的现代 string 库都与 string.h 不同。string 类库 是一个用于操作现代 C++ 字符串的 C++ 库,而 string.h 是一个用于操作 C 样式字符串(即以空字符结尾的字符串)的 C 头文件。

让我们看看下面的代码来理解 string.h 库。

#include <string.h>

#include <iostream>
using namespace std;

int main() {
  char country[] = "Pakistan";
  char countryTemp[50] = "abc";

  cout << "countryTemp length before initializing is:";
  cout << strlen(countryTemp) << endl;

  // strcpy()
  cout << "Let's copy country to countryTemp" << endl;
  strcpy(countryTemp, country);
  cout << "countryTemp=" << countryTemp << endl;

  cout << "countryTemp length after copying country is:";
  cout << strlen(countryTemp) << endl;

  return 0;
}

上面的程序首先声明了两个字符数组(C-strings)并用 Pakistan 初始化第一个。第 10 行打印 countryTemp 的长度,即 3。

虽然 countryTemp 的总大小为 50 个字节,但 strcpy 只能根据 NULL 字符(放置在数组的第四个字节)计算大小。因此,strcpy 返回 3。

第 14 行使用 strcpy 函数将 country 数组的内容复制到 countryTemp。因此,countryTemp 的新长度变为 8

输出:

countryTemp length before initializing is:3
Let's copy country to countryTemp
countryTemp=Pakistan
countryTemp length after copying country is:8

注意:string.h 头文件为 NULL 终止的字符串(也称为 C 样式字符串)提供了许多函数。可以在此处找到更多可用的库函数。

C++ 中字符串与 Char 的比较

人们将字符串与字符常量进行比较是一个常见的问题。这实际上是不可能的。

参考下面的代码。

#include <iostream>
using namespace std;

int main() {
  cout << "Do you want to proceed (y or n)?\n";
  char inp;
  cin >> inp;
  if (inp == "y")  // error here
    cout << "Hello again" << endl;
  else
    cout << "Good Bye" << endl;
  return 0;
}

在这个函数中,我们从用户那里做了一个输入,它是一个 char 变量,在 if 条件下,我们将它与一个用双引号括起来的字符串文字进行比较。此代码将生成错误:ISO C++ 禁止在指针和整数之间进行比较错误。

建议在 string 变量而不是 char 变量中进行输入以避免此错误。

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

int main() {
  cout << "Do you want to proceed (y or n)?\n";
  string ans;
  cin >> ans;
  if (ans == "y")  // error here
    cout << "Hello again" << endl;
  else
    cout << "Good Bye" << endl;
  return 0;
}

输出:

Do you want to proceed (y or n)?
n
Good Bye