Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use make_pair to create pair of id and struct (object)?

Tags:

c++

c++11

I was trying to create a pair of id and object like this:

 #include <iostream>
  #include <utility>
  #include <functional>

  struct num{

      double x;
      double y;

  };


  int main(){

      auto tmp = std::make_pair(1, {1.0, 2.0});

 }

I get error as error: no matching function for call to 'make_pair(int, <brace-enclosed initializer list>)'

Is there a proper way to create pair of id and object?

like image 944
solti Avatar asked Mar 11 '23 08:03

solti


1 Answers

No, This is how you should create your pair:

auto tmp = std::make_pair(1, num{1.0, 2.0});

Or alternatively (as @StoryTeller mentioned):

std::pair<int,num> tmp {1, {1.0, 2.0}};

Now, in both cases, the compiler has a clue that {1.0, 2.0} is meant to be an initializer for the num.

like image 91
Humam Helfawi Avatar answered Mar 23 '23 15:03

Humam Helfawi