Search code examples
c++c++11stdmapstd-pairstdarray

Initialize map with pair of std::arrays c++11


I would like to compile this lines. Insert to map pair of std::arrays.

#include<iostream>
#include<map>
#include<array>
#include<utility>

using namespace std;

int main()
{
  array<double, 8> l;
  array<double, 8> r;
  map<double, pair<array<double, 8>, array<double, 8>>> m;
  pair<array<double, 8>, array<double, 8>> p;
  p = make_pair(l, r);//ok
  m.insert(1., make_pair(l, r));//not ok
  return 0;
}

//clear && g++ getMinPosition.cpp -std=c++11 -o getMinPosition && ./getMinPosition

Solution

  • std::map::insert has various overloads, but non accepts two arguments of types as in your code. The closest to your use is the one accepting const value_type& where value_type is an alias of pair<const key_type, mapped_type>.

    so instead of :

    m.insert(1., make_pair(l, r));//not ok
    

    do:

    m.insert(make_pair(1., make_pair(l, r)));
    

    or:

    m.insert({ 1., make_pair(l, r) });
    m.insert({ 1., {l, r} });
    

    or for better performance use emplace:

    m.emplace(1., make_pair(l, r));