|
-
October 31st, 2001, 07:39 AM
#1
How to use map
Could some one give me a quick tutorial on using map I have been using CMap but I'd like to use map.
-
October 31st, 2001, 05:52 PM
#2
Re: How to use map
#include <map>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
stringstream strsTemp;
map<int, string> Map;
// Fill map
for(int iCnt = 0; iCnt < 10; ++iCnt)
{
strsTemp.str("");
strsTemp << "This is string " << iCnt + 1;
// Add to map
pair<map<int, string>::iterator, bool> pairmapRC = Map.insert(map<int, string>::value_type(iCnt, strsTemp.str()));
if(pairmapRC.second == true)
// String added to map
else
// Could not add string to map
}
// Write string number 5
map<int, string>::iterator iter = Map.find(5);
if(iter != Map.end())
cout << iter->second << endl;
else
// String not found in map
return 0;
}
This example generates 10 different strings and adds them to the map. After that it will print out the fifth string. I didn't test it but it should work...
Ciao, Andreas
"Software is like sex, it's better when it's free." - Linus Torvalds
-
January 10th, 2002, 05:16 AM
#3
Re: How to use map - How do I iterate through a map?
I have another problem concerning maps. I do not know how to iterate through the elements of a map. What I would need is an example of that. Do you know? // Stefan
-
January 10th, 2002, 07:33 AM
#4
Re: How to use map - How do I iterate through a map?
Iterate through elements in map:
#pragma warning (disable : 4786) //disable stl warning
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef map<int, string, less<int> > myMapType;
int main()
{
myMapType myMap;
myMapType::iterator iter;
char tmp[128];
// insert items
for(int i=0; i<16; i++) {
sprintf(tmp, "string no. %02d", i);
myMap.insert(myMapType::value_type(i, tmp) );
}
//iterate through items
for(iter=myMap.begin(); iter!=myMap.end(); ++iter) {
cout << (*iter).first << " --- "; //first element in map (int)
cout << (*iter).second.data() << endl; //second element in map (std::string)
}
return 0;
}
Regards,
Kjetil Haga
-
January 11th, 2002, 05:25 AM
#5
Re: How to use map - How do I iterate through a map?
you've answered but made it very complicated.
And you can do iter->first and iter->second (instead of (*iter).first etc. )
The best things come to those who rate
-
January 11th, 2002, 05:30 AM
#6
Re: How to use map - How do I iterate through a map?
extern std::map< T1, T2 > t12Map; // declared externally with values
//
std::map< T1, T2 >::iterator it = t12Map.begin();
for (; it != t12Map.end(); ++it )
{
T1 key = it->first;
T2 value = it->second;
}
The method of iteration is the same as for all STL collections.
You can use const_iterator instead of iterator, which will make it->first and it->second both const.
Note that the prefix ++ is slightly more efficient than the suffix ++ because the latter has to make a copy of the iterator.
You can add a value to an iterator eg it + 2 but you can't do it += 2 (well not in Microsoft anyway).
The best things come to those who rate
-
January 11th, 2002, 10:08 AM
#7
Re: How to use map - How do I iterate through a map?
-
April 6th, 2002, 03:41 PM
#8
Re: How to use map
In my Header file I put this:
public:
int m_iMapCount;
protected:
typedef std::map< long, std::string, std::less< long > > ANYNAME_I_WANT;
ANYNAME_I_WANT m_mapANYNAME_I_WANT;
Then in my CPP I have:
mapANYNAME_I_WANT[m_iMapCount] = szNewVal; //passing in an std::string
m_iMapCount += 1;
--
Chizl
[email protected]
http://www.chizl.com/
-
April 7th, 2002, 05:29 AM
#9
Re: How to use map
1. There is no need for you to put in std::less< long >. You only normally use this parameter on a user-defined class which either does not have operator<, or does not have the one you wish to use - eg std::string where you want to compare case-insensitive instead of case-sensitive, therefore you want to use a different compare function (Scott Meyers discusses this case, although often I find I use map with upper-case or lower-case key, where the key is often a member of the second element anyway, i.e. I use it instead of "set" with my own predicate. I do this because I'm sure it is quicker)
2. You seem to be sticking them in in the order in which they are added - so what you are actually creating is simply a vector: why not, instead, use a vector and push_back()
The best things come to those who rate
-
April 7th, 2002, 07:08 PM
#10
Re: How to use map
Well, I kind of did a cut and paste. I've been using C++ on and off for some time, mostly ATL, but I don't use it every day. Maybe once a month. hehe Here is what I got my code from:
//header.h has:
protected:
typedef std::map< long, std::string, std::less< long > > LONG2STRING;
LONG2STRING m_mapErrorLookup;
struct response_code
{
UINT nResponse; // Response we're looking for
TCHAR* sMessage; // Error message if we don't get it
};
static response_code response_table[];
Then in my CPP I have this:
CInit::response_code CInit::response_table[] =
{
//error codes found at:
//http://sourcesite.geeksatwork.com/cplusplus/files/Winsock_Error_Codes.txt
//if site is down, search google.com for, "Windows Sockets Error Codes, Values, and Meanings"
{ 0, "OK." },
{ 10004, "Interrupted system call." },
{ 10009, "Bad file number." },
{ 10013, "Permission denied."},
{ 10014, "Bad address."},
{ 10022, "Invalid argument."},
{ 10024, "Too many open files."},
{ 10035, "Operation would block."},
{ 10036, "Operation now in progress."},
{ 10037, "Operation already in progress."},
{ 10038, "Socket operation on nonsocket."},
{ 10039, "Destination address required."},
{ 10040, "Message too long."},
{ 10041, "Protocol wrong type for socket."},
{ 10042, "Protocol not available."},
{ 10043, "Protocol not supported."},
{ 10044, "Socket type not supported."},
{ 10045, "Operation not supported on socket."},
{ 10046, "Protocol family not supported."},
{ 10047, "Address family not supported by protocol family."},
{ 10048, "Address already in use."},
{ 10049, "Cannot assign requested address."},
{ 10050, "Network is down."},
{ 10051, "Network is unreachable."},
{ 10052, "Network dropped connection on reset."},
{ 10053, "Software caused connection abort."},
{ 10054, "Connection reset by peer."},
{ 10055, "No buffer space available."},
{ 10056, "Socket is already connected."},
{ 10057, "Socket is not connected."},
{ 10058, "Cannot send after socket shutdown."},
{ 10059, "Too many references: cannot splice."},
{ 10060, "Connection timed out."},
{ 10061, "Connection refused."},
{ 10062, "Too many levels of symbolic links."},
{ 10063, "File name too long."},
{ 10064, "Host is down."},
{ 10065, "No route to host."},
{ 10091, "Returned by WSAStartup(), indicating that the network subsystem is unusable."},
{ 10092, "Returned by WSAStartup(), indicating that the Windows Sockets DLL cannot support this application."},
{ 10093, "Winsock not initialized."},
{ 10101, "Disconnect."},
{ 11001, "Host not found."},
{ 11002, "Nonauthoritative host not found."},
{ 11003, "Nonrecoverable error."},
{ 11004, "Valid name, no data record of requested type."}
};
void CInit::InitErrors()
{
//find mapped error description
for (int i =0; i<sizeof(response_table)/sizeof(response_code); i++) {
m_mapErrorLookup[response_table[i].nResponse] = response_table[i].sMessage;
}
return;
}
Does this make more since to ya.. Not me, just the only way I know how to use maps. 
--
Chizl
[email protected]
http://www.chizl.com/
-
April 7th, 2002, 07:36 PM
#11
Re: How to use map - How do I iterate through a map?
I tried this, because I use several maps off the same map, and I see this doesn't work.
//Doesn't work..
typedef std::map< long, std::string> STRING_STORAGE;
STRING_STORAGE m_mapHeaders;
m_mapTo::iterator iter;
//Does work..
typedef std::map< long, std::string> STRING_STORAGE;
STRING_STORAGE::iterator iter;
I use for examle:
STRING_STORAGE m_mapHeaders;
STRING_STORAGE m_mapAttachments;
How would you iterate through this, using it this way? I found .begin() and .end() doesn't work either. .clear() does. Weird..
--
Chizl
[email protected]
http://www.chizl.com/
-
April 7th, 2002, 07:38 PM
#12
Re: How to use map - How do I iterate through a map?
I'm coping and pasting so I missed the correct copy, but i'm using the same map var.
// doesn't work.
STRING_STORAGE m_mapHeaders;
m_mapHeaders::iterator iter;
--
Chizl
[email protected]
http://www.chizl.com/
-
April 8th, 2002, 02:15 AM
#13
Re: How to use map - How do I iterate through a map?
Hi,
It's an error in your declaration of the iterator. You should declare it like this:
STRING_STORAGE m_mapHeaders;
STRING_STORAGE::iterator iter;
This should work.
Regards,
Kjetil Haga
-
April 8th, 2002, 08:03 AM
#14
Re: How to use map - How do I iterate through a map?
Surely map's iterator is a model of Bidirectional Iterator, so only has operator++ and operator-- defined, not operator+/operator- ? You can't do it = it + 2 on a std::map<>::iterator.
He who breaks a thing to find out what it is, has left the path of wisdom - Gandalf
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
April 8th, 2002, 06:02 PM
#15
Re: How to use map - How do I iterate through a map?
yeah, probably wasn't thinking. Why would you want to, anyway?
The best things come to those who rate
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|