使用>>运算符

sum*_*ang 0 c++ c++11

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

 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); 
Run Code Online (Sandbox Code Playgroud)

小智 5

因为ss是一个流,所以>>会重载以从流中进行格式化读取,具体取决于右侧操作数的类型.

因此,while(ss >> temp)将从中读取空白分隔的整数stringstream.这就是你替换上面' :'with' ' above. When evaluated as a boolean, it will be true if an integer was read and false在流的末尾,则为true .

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