Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello, I created a map in c++, but I do not know how to initialize it. the map is as follow:

std::map<int, list<std::tuple<int, int> >> groups;


actually I want to create a dictionary in C++ and I want to put the elements with the same attribute in one element of dictionary. for example I want to put the tuples {1,2},{2,2},{2,4} in groups[0] of dictionary. what is the solution?

What I have tried:

I tried the following line. but, it has error:

groups[0]=(make_tuple(4,4));
Posted
Updated 4-Apr-17 10:58am
v4

1 solution

You forgot the list. Try, for instance
C++
#include <iostream>
#include <map>
#include <tuple>
#include <list>
using namespace std;


int main()
{
  std::map<int, list<std::tuple<int, int> >> groups;

  groups[0] = list<std::tuple<int,int>>{make_tuple(3,4)};

  cout << get<1>(groups[0].front()) << endl;
}
 
Share this answer
 
Comments
Philippe Mori 4-Apr-17 18:56pm    
There is a missing new before list constructor...
Member 11807654 5-Apr-17 3:10am    
when I write " groups[0] = list<std::tuple<int,int>>{make_tuple(3,4)};" it gives the error:"type name is not allowed" and when I write "groups[0] = new list<std::tuple<int,int>>{make_tuple(3,4)};" it gives error:"error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::list<_Ty> *' (or there is no acceptable conversion)"
CPallini 5-Apr-17 3:32am    
Which version of Visual Studio are you using?
Such piece of code compiles and runs fine both on Linux with g++ 5.4 and on Windows, with CL 18.00 (Visual Studio 2013).
Member 11807654 5-Apr-17 4:09am    
visual studio 2012
Philippe Mori 5-Apr-17 7:31am    
Well, I didn't pay attention it was a value. Usually ehen I have such embedded container, I would use push_back (or push_front if reversed order is desired) similar to CPallini comment below. Usually, it does not make much sense to replace the whole list as you typically want to update existing list.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900