Igate Interview Question
Technical ArchitectsCountry: India
Interview Type: In-Person
There are plenty of times you'd want to use char* in a map. For instance, if you're associating the identity and not the value of c-style strings (as a key) with some other value.
@Evgeny
I am not sure that I quite understood the idea of using the " char* " as a key in a map for associating the identity. Could you please elaborate on that?
I mean, what if you didn't want to pass a string by value, but you wanted to just pass it by identity (two strings are equal if they are in the same place in memory). If these are C-style strings and if we wish to use them as keys in maps, char* would make sense. Not a common case, but plausible.
char * is a address to char. So, problem is map will store char * which will be numerically order (which is not expected).
However, char& is reference to char and compiler can treat it as char. So, There will not have any problem to store a char (OR char &) as a key in map.
That's not what happens when you declare the template type to be char&, as in
std::map<char&, valueType> foo;
it will just make a reference to char. So, map will be maintained correctly either key is "char" (copy of a char) or "char &" (reference to a char).
functors is a solution for using any non standard data types.check below link
stackoverflow.com/questions/356950/c-functors-and-their-uses
link was just passed looking at the header. read this, i hope you will understand that functors is the solution.
stackoverflow.com/questions/4157687/using-char-as-a-key-in-stdmap
I'm sorry to tell you that I don't understand why you think functors are relevant here. While functors are a very useful construct that I myself have happily used in the past, I don't see how they answer the question here. The original problem statement just asks whether it's possible to use char* as the key type in a map. And the answer is yes, it is. When someone answered that it's useless, I felt compelled to tell them that there are sometimes occasions to do it. But what do functors have to do with this discussion?
Okey....... May be question is incomplete because no one will just asked whether "char *" can be used as a map key.
The answer to actual question. Yes, you can use char * as a key. and answer to "how?" is functors and that is explained in earlier link.
You can use char* as a key without using functors. That's why I'm having a lot of trouble seeing how functors are relevant here.
Right !!! You can use char * without any problem. But think, what is the use of it and how it going to work? Take an example. you have defined map like,
std::map <char *, valuetype> myMap;
Now, You have added, addresses of these string "abc", "xyz", "ttt","oooo" with there respective value (of type valueType) into map. First thing, how those get inserted in map ? Now you want to search "ttt" in a map. will it work as expected?
Lucky that I don't expect searching for "ttt" to work then. I don't expect value-based searches to work. In this case, I would only expect searching for the pointer to work. This is still quite useful if I'm getting these pointers from another data structure that's supposed to work with this one or something like that.
so it means, you need maintain another data structure which will search your value and return you address to be searched into map. And that increases complexity. functors provides you easy solution.
You don't need to maintain anything. I'm saying, you might use a map with a char* if you were maintaining such a thing. There are times when it's useful. It's useful for the same reason why using pointers instead of passing everything by value is sometimes useful.
And what in the world do functors have to do with anything? You still haven't explained that. The problem statement is to make a map with char*, not to do anything else.
Yes, you can. But this is going to be pretty useless cause the key comparison is going to be done for pointers themselves, not for the memory content they point to, which is what is presumed the char* is meant to be used for. Consider the less predicate acting upon two pointers: char *p1 < char*p2. Is this what we want ? Definitely not.
- ashot madatyan July 24, 2012