0

Map is a container class that is used to store the aggregate data... Its very easy to retreive the datas stored in it as it uses hash algorithm for retrieval. map is a key value pair...The data can be retrieved with the corresponding key... Here in this declaration below I'm defining that the key has to be integer(4 bytes) and data as the string value...

typedef map<INT32U,string> EventMapType; 

I searched for the example program of using map in wikipedia... But i could not understand the example given over there..I need to know how the datas and keys are stored in the map and how it is retreived through the key...I am new to MFC...

4
  • 7
    cplusplus.com/reference/stl/map/map Commented Apr 24, 2011 at 8:20
  • 1
    I don't understand your question clearly. Are you interested in inner representation of data in the map or you are interested in how to work with map? If second, could you point more specifically to places, that you can't understand in wikipedia or any other example. Just paste the code and point to place which you don't understand. And I think we are talking about STL, not MFC. Commented Apr 24, 2011 at 8:24
  • @Brian Roach:Thanks for the link Commented Apr 24, 2011 at 8:24
  • 1
    Np - that's a really good reference site for all the std:: stuff. Commented Apr 24, 2011 at 8:25

3 Answers 3

3

Beata,

I just did a quick google and came up with http://erunways.com/c-using-the-standard-template-library-stl-map-example/ I won't just copy paste that code here... it's only about 50 lines.

I suggest you read through that code, and then compile and run it (as is). If you run into problems or just stuff that doesn't make sense to you, then ask specific questions here. K?

Cheers. Keith.

Sign up to request clarification or add additional context in comments.

Comments

3

map does not use hashing. It can't, because the constraints do not require hashable keys. It is ordinarily implemented as a binary search tree, sorted by key. Thus, it requires keys be <-comparable

In contrast, C++0x will provide an unordered_map, which does use hashing.

If you want specific help, you should tell us what code you've tried so far, and which examples you don't understand.

Comments

2

the STL's map class allows you to store data by any type of key instead of simply by a numerical key, the way you must access an array or vector. So instead of having to compute a hash function and then access an array, you can just let the map class do it for you.

typedef map<INT32U,string> MyEventMapType; MyEventMapType EventMapType; 

Use below as reference code.

To Store values :

EventMapType[key1] = string1 ; EventMapType[key2] = string2 ; EventMapType[key3] = string3 ; 

To check the value at key1 ...

if(EventMapType.find("key1") == EventMapType.end()) { std::cout<<"string1 is not in the map!"<<endl; } 

For more read the documentation

Iterators can also be used as a general means for accessing the data stored in a map; you can use the basic technique from before of getting an iterator:

3 Comments

EventMapType is a type, you cant just : EventMapType[key1] = string1. first declare variables...
@ soulSurfer2010: I declared it like typedef map<INT32U,string> EventMapType; plz see the answer again.
#include "stdafx.h" #include <iostream> #include <map> #include <string> using namespace std; int main() { typedef map<int,string> EventTypeMap; EventTypeMap EventType; EventType[1]="beata"; EventType[2]="jane"; EventType[3]="declan"; if(EventType.find(1)==EventType.end()) { cout<<"string is not in the map!"<<endl; } return 0; } output:Nothing is displayed.when used .find("1") i'm getting an error that its impossible to convert from const char to const int.when tried with .find('1') its displaying me "string is not in the map".please tell what i have done wrong in this code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.