unexpected input stream behavior
Maarten Ditzel
ditzel@cas.et.tudelft.nl
Fri Jan 11 08:23:00 GMT 2002
Hi,
I ran into a some unexpected behavior (at least by me) when running the
following code:
/**********************************************************************************/
#include <iostream.h>
#include <map>
#include <string>
int main(int argc, char *argv[]) {
map<string, string> M;
string key = "blaat";
// unexpected behavior
cin >> key >> M[key]; // first input
cin >> key >> M[key]; // second input
// should be the same ???
cin >> key; // third input (a)
cin >> M[key]; // third input (b)
// print complete map
map<gcc --verstring, string>::const_iterator iter;
for (iter=M.begin(); iter!=M.end(); iter++) {
cout << iter->first << '\t' << iter->second << endl;
}
return 0;
}
/**********************************************************************************/
I compiled it using g++ version 2.95.4 on a debian system (mostly
Woody).
Running it with the following inputs:
>>>>>>>>>>>>>>>>>>>>>>>>>
first 1st
second 2nd
third 3rd
>>>>>>>>>>>>>>>>>>>>>>>>>
gives:
>>>>>>>>>>>>>>>>>>>>>>>>>
blaat 1st
first 2nd
third 3rd
>>>>>>>>>>>>>>>>>>>>>>>>>
So what obviously happens is that in the first and second input
statements, the previous key value is used in M[key]. This does not
happen using the 3rd input statements (a and b).
I expected the first and second input statements to act the same as the
third, basically
cin >> a >> b;
equals
cin >> a; cin >> b;
I don't know if this is unspecified C++ behavior, or possibly a bug in
the standard libraries or compiler.
Any ideas/comments ?
Regards,
Maarten Ditzel
--
=================================================================
ir. Maarten Ditzel Delft University of Technology
Electrical Engineering
ditzel@cobalt.et.tudelft.nl Circuits and Systems
http://cobalt.et.tudelft.nl/~ditzel Ubiquitous Communications
=================================================================
More information about the Libstdc++
mailing list