我試圖解析一個格式爲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
字符串對象?