I want to read only first number in each line which is delimited with \n
and skip other data. Example:
123 123 456 asdf\n234 ewdwedwed
should be parsed, for example, into a vector with values {123, 234}
.
My first attempt is the following snippet:
int main()
{
std::stringstream ss { "123 123 456 asdf\n234 ewdwedwed" };
std::vector<int> values;
int value;
std::string skip;
while (ss >> value) {
values.push_back(value);
std::getline(ss, skip);
}
return 0;
}
But I wonder whether getline
will actually copy data into temp string. In such a case it will do unnecessary copying and consume more memory than needed. Am I right? Should the getline
be replaced with some kind of peek
and ignore
methods:
while (ss >> value) {
values.push_back(value);
while (ss.peek() != '\n'){
ss.ignore();
}
}