Hi, I've got 4 questions about C++ hash_map in "ext/hash_map":
1. Trying to access a non-existent key will introduce insertion, won't it?
For example, I know that:
1 2
|
hash_map<char* key, char* value> hm1; //hm1 is empty
hm1["wed"]= "The day after Tuesday";//now, hm1 contains one entry: "wed" ---> "The day after Tuesday"
|
The code piece above will actually insert the pair into hm1, an originally empty hash map. Based on the hash_map we have now (containing one pair), My question is: if
1 2 3 4 5
|
// I intend to check whether some key exists in the hm1; if not, print "no such key"
if(hm1["Tues"]==NULL)
{
cout << "No such key\n";
}
|
Is the code piece going to do what I intend to do? Will the code piece introduce a new pair <"Tues", NULL> into hm1?
2. How can I have hm1 sorted on key? I want to access pairs sequentially from smaller key to greater key, with an iterator.
3. Is there any way to guarantee the order of access with iterator? For example, I want to use an iterator to access/update <key, value> pairs with the SAME order I inserted them into hm1. How do I do this?
4. Theoretically, hash_map has O(1) time complexity for access. In real application, if my c string key is very long, say 4kb, will it significantly affect the time to access pairs <key, value>?
Any inputs are greatly appreciated!