在C++中,有多种方式可以定义并赋值字符串。
- 使用字符数组:
char str[] = "Hello World";
- 使用
std::string
类:
std::string str = "Hello World";
- 使用
const char*
指针:
const char* str = "Hello World";
- 使用
std::string_view
类(C++17及以上版本):
std::string_view str = "Hello World";
这些方法都可以用于定义字符串并赋值,选择哪一种方式取决于具体的需求和使用场景。