使用>>运算符

Use of >> operator

本文关键字:gt 运算符 使用      更新时间:2023-10-16

>任何人都可以解释一下代码中以下行的含义

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中读取空格分隔的整数。这就是为什么您将上面的":"替换为">"的原因。当评估为布尔值时,如果在流的末尾读取并CC_7整数,则为 true。

有关更多详细信息,请参阅此处的示例