c++ - 为同一行的多个声明赋值

标签 c++

我正在创建变量

std::string str1,str2,str3,str4 = "Default";

但是变量没有默认值。如何为以这种方式创建的变量赋值

最佳答案

str4 将具有您要查找的值。你只是没有初始化其他人。使用:

std::string str1 = "Default", str2 = "Default", str3 = "Default", str4 = "Default";

或者,可能更好:

std::string str1 = "Default";
std::string str2 = "Default";
std::string str3 = "Default";
std::string str4 = "Default";

如果你担心输入太多,你可以使用赋值而不是初始化:

std::string str1, str2, str3, str4;
str1 = str2 = str3 = str4 = "Default";

但这有不同的语义,并且(恕我直言)有点古怪。

关于c++ - 为同一行的多个声明赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18730494/

相关文章:

c++ - 暴露复杂的短暂对象的正确方法

c++ - C++ 中类似 Python 的 map

c++ - 使用 delete 调用析构函数时出现调试错误

c++ - 如何在 istream& 运算符中输入字符串

c++ - 如何使用 C/C++ 制作 .com 文件?

c++ - sfml 2 和 cmake 构建

c++ - 删除两个 vector 中的公共(public)对象/获取 vector 中的重复对象(不删除)

c++ - C++ cout 的时间复杂度?

c++ - 引用被重新安置在这里还是别的什么

c++ - 无法访问 const static std::map 枚举结构