 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
ritesh Guest
|
Posted: Tue Oct 25, 2005 4:30 pm Post subject: Inserting elements in Map of Map |
|
|
Hi,
I've declared a multimap containing a map as
multimap<int, map MyMap;
Now when I try to insert elements in to MyMap I get compiler errors.
The statement where I get the error is -
MyMap.insert(pair<int, AtrMap(intVar, (pair<char*,
char*>(sgStrdup(strName), sgStrdup(strValue)))));
I also tried this insert statement but got errors
MyMap.insert(pair<int, pair(intVar, (pair<char*,
char*>(sgStrdup(strName), sgStrdup(strValue)))));
The errors i get are
-error: no matching function for call to `std::pair<int, map
char*, std::less >
| Quote: | ::pair(int&, std::pair<char*, char*>)
|
My guess is that I am not able to create the pair<...> statement
properly. Could you help me correct this and also a brief explanation
would be great.
Thanks,
-ritesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Wed Oct 26, 2005 1:09 pm Post subject: Re: Inserting elements in Map of Map |
|
|
ritesh wrote:
| Quote: | I've declared a multimap containing a map as
multimap<int, map MyMap;
|
Looks like a performance nightmare to me, but if you really need this
kind of container, I guess you must have good reasons.
| Quote: | Now when I try to insert elements in to MyMap I get compiler errors.
The statement where I get the error is -
MyMap.insert(pair<int, AtrMap(intVar, (pair<char*,
char*>(sgStrdup(strName), sgStrdup(strValue)))));
I also tried this insert statement but got errors
MyMap.insert(pair<int, pair(intVar, (pair<char*,
char*>(sgStrdup(strName), sgStrdup(strValue)))));
|
Of course they are both wrong. First of all, the best thing you should
do is to use typedefs instead of repeating templates parameters all over
again. That is, instead of writing:
multimap<int, map MyMap;
use:
typedef map<char *, char *> InnerMap_t;
typedef multimap<int, InnerMap_t> Outermap_t;
Outermap_t MyMap
(of course you can use whatever name you prefer, possibly more
meaningful, considering the context).
Then, instead of writing pair<blah, blah>, you can use the much more
descriptive syntax MapTypeHere::value_type. Your example becomes:
MyMap.insert(
OuterMap_t::value_type(
intVar,
InnerMap_t::value_type(sgStrdup(strName), sgStrdup(strValue))));
and if you do that, you'll immediately spot the error! That's because a
OuterMap_t::value_type takes a int and an InnerMap_t object but you are
providing an int and a InnerMap::value_type object instead!
You've got a real problem here, because map has no constructor that
takes a value_type. You have to create an temporary InnerMap_t map
first, add the element to it and then add the temporary to MyMap. So:
InnerMap_t temp;
temp.insert(
InnerMap_t::value_type(sgStrdup(strName), sgStrdup(strValue)));
MyMap.insert(
OuterMap_t::value_type(intVar, temp));
Alternatively, you can insert an empty map and then add the strings
later, as in:
OuterMap_t::iterator justAdded = MyMap.insert(
OuterMap_t::value_type(intVar, InnerMap_t())).first;
justAdded->second.insert(
InnerMap_t::value_type(sgStrdup(strName), sgStrdup(strValue)));
which is probably slightly better performance-wise, although the
behaviour is different in case the key intVar is already present in MyMap.
HTH,
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Wed Oct 26, 2005 1:16 pm Post subject: Re: Inserting elements in Map of Map |
|
|
ritesh wrote:
| Quote: | Hi,
I've declared a multimap containing a map as
multimap<int, map MyMap;
Now when I try to insert elements in to MyMap I get compiler errors.
The statement where I get the error is -
MyMap.insert(pair<int, AtrMap(intVar, (pair<char*,
char*>(sgStrdup(strName), sgStrdup(strValue)))));
|
1st mistake: MyMap.insert() takes a
pair<int, map
not a
pair<int, AtrMap
2nd mistake (possibly): AtrMap<char*, char*> probably has not ctor taking a
pair<char*,char*>.
3rd mistake: using manual memory management and raw pointers. This is just
plainly too insecure, use std::string instead. Once you know what you're
doing and if you know there is need to avoid the little overhead you might
consider taking advantage of the greater control, but not until then.
| Quote: | I also tried this insert statement but got errors
MyMap.insert(pair<int, pair(intVar, (pair<char*,
char*>(sgStrdup(strName), sgStrdup(strValue)))));
|
Again, a map is not a pair or vice versa.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|