我试图解析一个格式为Key<whitespace>Value
的文件。我正在读取std::istringstream
对象中的文件行,并从中提取Key
字符串。我想要避免意外地更改Key
字符串的值,使其为const
。从“std :: istringstream”初始化“const std :: string”
我最好的尝试是初始化一个临时的VariableKey
对象,然后使它成为一个常量。
std::ifstream FileStream(FileLocation);
std::string FileLine;
while (std::getline(FileStream, FileLine))
{
std::istringstream iss(FileLine);
std::string VariableKey;
iss >> VariableKey;
const std::string Key(std::move(VariableKey));
// ...
// A very long and complex parsing algorithm
// which uses `Key` in a lot of places.
// ...
}
如何直接初始化一个常量Key
字符串对象?