2016-11-25 99 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