任何人都可以请我向我解释代码中以下几行的含义
while (ss >> temp)
std::string str = "123:234:56:91";
for (int i=0; i<str.length(); i++)
{
if (str[i] == ':')
str[i] = ' ';
}
vector<int> array;
stringstream ss(str);
int temp;
while (ss >> temp)
array.push_back(temp);
因为ss
是流,所以>>
重载以从流中进行格式化读取,具体取决于右侧操作数的类型。
因此,while(ss >> temp)
将从中读取空格分隔的整数stringstream
。这就是为什么您将上面的' :
'替换为' '的原因。当以布尔值求值时,如果
false
在流的末尾读取了整数,则为true 。
有关更多详细信息,请参见此处的示例
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句