简体   繁体   中英

How to insert a pair of std::pair inside another std::pair?

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.

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));

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

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))));

It helps to use typedefs when debugging this sort of thing.

// test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <map>
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
    typedef std::pair<long, long> ElementType;
    typedef std::pair<ElementType, ElementType> ValueType;
    typedef std::wstring KeyType;
    std::map<KeyType, ValueType> reference;

    KeyType key = L"First";
    reference[key] = ValueType(ElementType(-1, -1), ElementType(0, 0));

    return 0;
}

You could simplify your code by creating a helper function to create pairs of pairs, analog to the std::make_pair helper function that is available in the standard library. Also using the maps operator[] for insertion results in easier to read code:

template<typename T, typename U, typename V, typename W>
std::pair< std::pair<T,U>, std::pair<V,W> > make_pair_pair(T t, U u, V v, W w) {
   // using std::make_pair instead of the constructor for better readability
   return std::make_pair(std::make_pair(t, u), std::make_pair(v, w));
}

reference[L"First"] = make_pair_pair(1,2,3,4);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM