Main Page | Report this Page
.NET DotNet Forum Index  »  Visual C++ Forum  »  Map of string string...
Page 1 of 1    

Map of string string...

Author Message
Jürgen Ullmann...
Posted: Fri Oct 02, 2009 10:18 am
Guest
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]...
Posted: Fri Oct 02, 2009 11:05 am
Guest
Jürgen Ullmann wrote:
Quote:
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
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Tue Dec 01, 2009 3:17 am