How to insert a pair of std::pair inside another std::pair?
Solution 1
The >>>
can not be parsed correctly (unless you have a C++0x compiler).
Change to > > >
This:
reference.insert("First",
Should be:
reference.insert(L"First",
^^^
Also there is a utility function to make the construction of pairs easier:
std::pair<std::pair<long, long>, std::pair<long, long>>(std::pair<long, long>(-1, -1), std::pair<long, long>(0, 0))
Can be:
std::make_pair(std::make_pair(-1L,-1L),std::make_pair(0L,0L))
Try this:
reference[L"First"]
= std::make_pair(std::make_pair(-1L,-1L),std::make_pair(0L,0L));
Solution 2
C++ gets confused by the consecutive ">" when you close the template as it interprets that as the shift operator.
Add spaces between the closing templates, change >>> to > > >
Solution 3
map::insert
itself takes a single std::pair
argument, rather than two arguments. You can tidy up the code by using std::make_pair
(which infers the template arguments from the function arguments), to get something like:
reference.insert(std::make_pair("First",
std::make_pair(std::make_pair(-1L,-1L),
std::make_pair(0L,0L))));
Comments
-
Shane Larson over 3 years
I'm declaring a map of string to a pair of pairs as follow:
std::map<std::wstring, std::pair<std::pair<long, long>, std::pair<long, long>>> reference;
And I initialize it as:
reference.insert(L"First", std::pair<std::pair<long, long>, std::pair<long, long>>(std::pair<long, long>(-1, -1), std::pair<long, long>(0, 0)));
However, Visual C++ gives me the error "C2664, No constructor could take the source type, or constructor overload resolution was ambiguous".
I'm new to using templates and STL and I can't tell what I'm doing wrong.
-
Shane Larson over 13 yearsThanks for the help. I edited my question to add the L"" to my wstring to make the question more easier to "parse".
-
sth over 13 yearsThere is not such
insert()
function as the OP tries to use, even if you#include <string>
. The sample code you posted works and is quite readable, but because you replaced theinsert()
, not because of the include as you seem to say... -
Mike Seymour over 13 yearsA better answer than mine, but note that inserting with
reference[key] = value
can give different behaviour toreference.insert(make_pair(key,value))
; using[]
will overwrite an existing element, whileinsert
won't. -
sth over 13 yearsIf you remove the "you just need
<string>
" part, which I think is somewhat misleading, I would think it is helpful and would upvote it. -
ttt over 13 yearsok, you got it. I don't know if you realise you wrote "operator[] for insertion results in easier to read code" in your answer. As you say, insert doesn't exist!
-
sth over 13 yearsYeah I realize that and I saw your answer after posting mine and thought "who has downvoted this, operator[] is the way to go!". Then I got confused by the
<string>
stuff and went off to try if maybe actually some missing constructor forwstring(const char*)
or such would produce an error message like in the question. I couldn't reproduce anything like that, though, so I decided to comment. (+1)