开发者

Could not Understand the usage of map in c++

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...


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.


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.


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:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜