0

I tried the below code for move and initializer list insertions. The later works but not the move insertion. Can someone help me with the right solution here ?

#include <iostream>
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;


int main ()
{
  std::unordered_map<std::string,std::pair<double, double>>
              myrecipe;

  myrecipe.insert (std::make_pair<std::string,std::make_pair<double,double>>("eggs",(1.0,6.0)));
  myrecipe.insert ( {{"sugar",{0.8, 1.0}},{"salt",{0.1, 2.0}}} );   

  std::cout << "myrecipe contains:" << std::endl;
  for (auto& x: myrecipe)
    std::cout << x.first << ": " << x.second.first << ":" << x.second.second << std::endl;

  std::cout << std::endl;
  return 0;
}

Vinu
  • 147
  • 1
  • 7
  • What do you mean with "works"? What do you expect and what is the result you get? – RoQuOTriX Sep 21 '20 at 06:31
  • Welcome to Stack Overflow. Please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Sep 21 '20 at 06:31
  • Off-topic: When you are using `using namespace std;` you don't need to use `std::` before your functions. But it's a good practice to avoid `using namespace std;`:https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Farbod Ahmadian Sep 21 '20 at 06:59

3 Answers3

3

This line has a few problems:

myrecipe.insert (std::make_pair<std::string,std::make_pair<double,double>>("eggs",(1.0,6.0)));

The type you want to insert is a std::pair<std::string, std::pair<double, double>>, but that is not what you are making here. This is how to make it work with make_pair:

myrecipe.insert(std::make_pair<std::string, std::pair<double, double>>("eggs", std::make_pair<double, double>(1.0, 6.0)));

Or in a more readable format, that relies on template argument type deduction:

myrecipe.insert(std::make_pair("butter", std::make_pair(2.0, 3.0)));

Godbolt link, so you can see it work.

Frodyne
  • 3,547
  • 6
  • 16
0

You need to make the pair on the doubles, not the complete insert:

myrecipe.insert(("eggs",std::make_pair<double,double>(1.0,6.0)));

To clarify:
<std::string,std::pair<double, double>> is not a std::pair, it is a key-value pair (sic!).
Your std::pair<double, double> instead is a "real" std::pair (or someone could say a 2-tuple) which can be used in C++ as a std::pair. Therefore you need the std::make_pair_call

RoQuOTriX
  • 2,871
  • 14
  • 25
  • main.cpp:14:66: error: no matching function for call to ‘std::unordered_map, std::pair >::insert(std::pair)’ myrecipe.insert(("eggs",std::make_pair(1.0,6.0))); – Vinu Sep 21 '20 at 06:41
0

If you change that line to myrecipe.insert ({"eggs",{1.0,6.0}}); it should work as intended

Also, std::make_pair<double,double> should not appear as a template parameter since it is not a type but a function that returns an object.

tommy
  • 174
  • 1
  • 5
  • an initializer_list is created with the first entry being treated as a 'key' and the rest as the 'value' and they are 'moved' into the map – tommy Sep 21 '20 at 07:02