In this answer I have the following code:
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iterator>
#include <limits>
using namespace std;
struct station{
string _stationName;
int _studentPass;
int _adultPass;
};
std::istream& operator>>(std::istream& is, station& rhs){
getline(is, rhs._stationName, ';');
is >> rhs._studentPass >> rhs._adultPass;
return is;
}
int main(){
istringstream foo("4;\nSpadina;76 156\nBathurst;121 291\nKeele;70 61\nBay;158 158");
foo.ignore(numeric_limits<streamsize>::max(), '\n');
vector<station> bar{ istream_iterator<station>(foo), istream_iterator<station>() };
for (auto& i : bar){
cout << i._stationName << ' ' << i._studentPass << ' ' << i._adultPass << endl;
}
return 0;
}
It's output is:
Spadina 76 156
Bathurst 121 291
Keele 70 61
Bay 158 158
My expected output does not double space:
Spadina 76 156
Bathurst 121 291
Keele 70 61
Bay 158 158
I get the expected output if I change my operator>>
to:
std::istream& operator>>(std::istream& is, station& rhs){
if (is >> rhs._stationName >> rhs._adultPass){
auto i = rhs._stationName.find(';');
rhs._studentPass = stoi(rhs._stationName.substr(i + 1));
rhs._stationName.resize(i);
}
return is;
}
This seems like a compiler bug or something, but that would be strange cause I'm seeing this behavior in both Visual Studio 2013 and gcc 4.9.2.
Can anyone explain this to me?
operator >>
returning int
does not discard the whitespace after it so when _adultPass
is read, the next character in the stream is \n
. If you then run getline
that stops at ';'
, this newline character is read as well and stored at the beginning of the string.