温馨提示×

c++字符串变量如何定义

c++
小亿
188
2023-11-14 10:24:44
栏目: 编程语言

C++中可以通过以下几种方式定义字符串变量:

  1. 使用字符数组定义字符串变量:
char str[] = "Hello World";
  1. 使用std::string类定义字符串变量:
std::string str = "Hello World";
  1. 使用指针定义字符串变量:
const char* str = "Hello World";
  1. 使用std::string_view类定义字符串变量(C++17及以后的版本):
std::string_view str = "Hello World";

需要注意的是,使用字符数组和指针定义的字符串变量是不可修改的,而使用std::stringstd::string_view定义的字符串变量是可修改的。另外,使用std::string类可以更方便地进行字符串操作和处理。

0