2016-11-25 93 views
0

的>>谁能请解释我下面一行的含义在代码使用运营商

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); 

回答

5

因为ss是一个流时,>>超载从流做格式化的阅读,这取决于右侧操作数的类型。

因此,while(ss >> temp)将从stringstream中读取空格分隔的整数。这就是为什么你用上面的''取代':'的原因。当作为布尔值计算时,如果读取了整数并且在数据流结尾处为false,则为true。

欲了解更多详情,请参阅例如here