How to use stringstream constructor in getline?

删除回忆录丶 提交于 2019-12-12 12:42:04

问题


Following up https://stackoverflow.com/a/1120224/390066.

Why can't I use

getline(stringstream(line),cell,','){}

instead of

stringstream lineStream(line); 
getline(lineStream,cell,','){}

?

update

I should have clarified that I want to use getline within a loop.

Furthermore, I should have also noted that my initial intention was to read a file line-by-line using getline and use the line from that in the new getline that would divide on ',', which is more intuitive imo.

From what I understood so far, getline is not designed for that because it takes a non-const input and gives const token; therefore, getline cannot be blindly recursed.


回答1:


As show by @James Kanze you can.

The question is do you really want to?
The stream is destroyed at the end of the expression so you are only reading one cell from it.

If we look at this in the context of the original question:
i.e. You can not use that in a loop:

std::string       line = /* Init */;
std::stringstream lineStream(line); 

std::string cell;
while(std::getline(lineStream, cell, ','))
{
    // Do stuff with cell.
}

If you place your code into this context it will not work as expected:

std::string cell;
while(std::getline(std::istringstream(line).flush(), cell, ','))
{
    // Do stuff with cell.
}

As the expression inside the while() will be fully evaluated each time. So you go into an infinte loop reading the first cell continuously.




回答2:


You can, but it's ugly:

std::getline( std::istringstream( line ).flush(), cell, ',' );

The problem is that std::getline takes a non-const reference (which is logical, since it is going to modify the stream), and you cannot initialize a non-const reference with a temporary. You can, however, call member functions on it. std::istream::flush is a member function, which returns a non-const reference to the stream on which it was called (and if that stream is an std::istringstream, doesn't do anything else).

FWIW: you'd probably find:

cell = std::string( line.cbegin(), std::find( line.cbegin(), line.cend(), ',' ) );

a bit more efficient. And, at least in my opinion, easier to read and maintain.



来源:https://stackoverflow.com/questions/26979013/how-to-use-stringstream-constructor-in-getline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!