Re: How to use map - How do I iterate through a map?
I have:
STRING_STORAGE m_mapHeaders;
STRING_STORAGE m_mapTo;
STRING_STORAGE m_mapCC;
If I have 3 inserts in m_mapHeaders, 2 in m_mapTo and 1 insert into m_mapCC, how am I going to iterator depending on the map being used?
What you show will iterator the one with the most inserts, would it not?
--
Chizl
[email protected]
http://www.chizl.com/
Re: How to use map - How do I iterate through a map?
Hi,
You can use the same iterator on each of the three maps because they're of the same type. But I think it's better to create one iterator for each map. However, here is an example using the same iterator on all three maps.
STRING_STORAGE m_mapHeaders;
STRING_STORAGE m_mapTo;
STRING_STORAGE m_mapCC;
STRING_STORAGE::iterator iter;
//--- insert strings
m_mapHeaders.insert(STRING_STORAGE::value_type(1, "mapHeaders string 1") );
m_mapHeaders.insert(STRING_STORAGE::value_type(2, "mapHeaders string 2") );
m_mapHeaders.insert(STRING_STORAGE::value_type(3, "mapHeaders string 3") );
m_mapTo.insert(STRING_STORAGE::value_type(1, "m_mapTo string 1") );
m_mapTo.insert(STRING_STORAGE::value_type(2, "m_mapTo string 2") );
m_mapCC.insert(STRING_STORAGE::value_type(1, "m_mapCC string 1") );
//--- iterate m_mapHeaders
for(iter=m_mapHeaders.begin(); iter!=m_mapHeaders.end(); ++iter) {
cout << (*iter).first << " --- " << (*iter).second << endl;
}
//--- iterate m_mapTo
for(iter=m_mapTo.begin(); iter!=m_mapTo.end(); ++iter) {
cout << (*iter).first << " --- " << (*iter).second << endl;
}
//--- iterate m_mapCC
for(iter=m_mapCC.begin(); iter!=m_mapCC.end(); ++iter) {
cout << (*iter).first << " --- " << (*iter).second << endl;
}
Hope this helps.
Regards,
Kjetil Haga
Re: How to use map - How do I iterate through a map?
That looks like exacly what I'm needing. I havn't tested it yet, but it looks like "The One". hehe
I guess code of:
m_mapHeaders[iCount] = "text";
isn't the right way to do it? It seemed pretty simple and it worked. Maybe that is why I can't iterate through it, huh.. hehe.
Thanx.
--
Chizl
[email protected]
http://www.chizl.com/
Re: How to use map - How do I iterate through a map?
1 last problem on this.. Since I have this in an ATL Environment, how can I get (*iter).first into a char* or std::string?
What is the difference between (*iter).first and (*iter).second? I only have one value in it..
--
Chizl
[email protected]
http://www.chizl.com/
Re: How to use map - How do I iterate through a map?
I tried:
sprintf(charTemp, "%s", (*m_iter).first);
and
szTest.append
but .append wouldn't compile and sprintf crashes on that line. What do I need to do to convert this to something readable?
--
Chizl
[email protected]
http://www.chizl.com/
Re: How to use map - How do I iterate through a map?
>> What is the difference between (*iter).first and (*iter).second? I only have one value in it..
'first' is the key of the map entry, 'second' is the associated value to that key...
Key Value
(*iter).first (*iter).second
iter->first iter->second
Ciao, Andreas
"Software is like sex, it's better when it's free." - Linus Torvalds
Re: How to use map - How do I iterate through a map?
Thanx, now do you know how to take a :
(*iter).second
or
iter->second
and convert it to a std::string or char*?
sprintf errors when trying.
--
Chizl
[email protected]
http://www.chizl.com/
Re: How to use map - How do I iterate through a map?
You got it almost right...
char charTemp[256];
sprintf(charTemp, "%d", (*m_iter).first );
//.. and
sprintf(charTemp, "%s", (*m_iter).second.data() );
//or
std::string std_str = (*m_iter).second
Regards,
Kjetil Haga
Re: How to use map - How do I iterate through a map?
As far as I remember you are using the following maps:
std::map<long, std::string> Map;
You had a typedef though but it doesn't matter right now.
In this example the 'second' would access the string itself already. Regarding 'sprintf' that would mean...
char szBuffer[100] = "";
sprintf(szBuffer, "%s", (*iter).second.c_str());
or
char szBuffer[100] = "";
sprintf(szBuffer, "%s", iter->second.c_str());
In the examples above you have to take care that the buffer is large enough to hold the complete string otherwise it will write over the boundaries...
Ciao, Andreas
"Software is like sex, it's better when it's free." - Linus Torvalds
Re: How to use map - How do I iterate through a map?
I got it to work. Sorry, I should have posted it lastnight.
sprintf(charTemp, "%d", iter->first);
and
sprintf(charTemp2, "%s", (*iter).second.c_str());
Thanx for the effort though..
--
Chizl
[email protected]
http://www.chizl.com/