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

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

使用字符数组定义字符串变量:

char str[] = "Hello World";

使用std::string类定义字符串变量:

std::string str = "Hello World";

使用指针定义字符串变量:

const char* str = "Hello World";

使用std::string_view类定义字符串变量(C++17及以后的版本):

std::string_view str = "Hello World";

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

阅读剩余
THE END