Map of string string

  • Thread starter Thread starter Jürgen Ullmann
  • Start date Start date
J

Jürgen Ullmann

I have a code that goes something like this:

typedef map<string, string> AMap;

AMap animals;
string animal("dog");
stringstream MyStream;
MyStream(animals[animal]);

Can somebody tell me what goes on in the last line? I don't understand
what "animals[animal]" does.
Am I correct when I think that this searches for the second item in the
map to matches the first item?

For example when the map goes something like this:

"dog", "walk"
"cat", "stray"
"mouse", "jump"

Or in other words: Will it find "walk" and feed the stringstream with it?
 
Jürgen Ullmann said:
I have a code that goes something like this:

typedef map<string, string> AMap;

AMap animals;
string animal("dog");
stringstream MyStream;
MyStream(animals[animal]);

Can somebody tell me what goes on in the last line? I don't understand
what "animals[animal]" does.
Am I correct when I think that this searches for the second item in
the map to matches the first item?

For example when the map goes something like this:

"dog", "walk"
"cat", "stray"
"mouse", "jump"

Or in other words: Will it find "walk" and feed the stringstream with
it?

Actually, the code as posted won't compile since among other things,
stringtream doesn't have a function call operator that takes a string
argument (nor any argument, for that matter). The last two lines should be

stringstream MyStrean(animals[animal]);

to correctly illustrate the case you're asking abount.

The result is basically what you surmised. That is, it will find the key
"dog" in the map 'animals' and return the value "walk". If the key was not
present in the map (e.g. "pig"), it would return a reference to a
default-constructed (i.e. empty) string. The stringstring 'MyStream' will
then be constructed using that string as it's initial content.

-cd
 
Back
Top