Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Map of string string

0 views
Skip to first unread message

Jürgen Ullmann

unread,
Oct 2, 2009, 12:18:10 PM10/2/09
to
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?

Carl Daniel [VC++ MVP]

unread,
Oct 2, 2009, 1:05:25 PM10/2/09
to

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


0 new messages