如何在C ++中初始化字典? [英] How to initialize a dictionary in C++?

查看:101
本文介绍了如何在C ++中初始化字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我用c ++创建了一个地图,但我不知道如何初始化它。地图如下:



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;





实际上我想用C ++创建一个字典,我想把具有相同属性的元素放在一个字典元素中。例如,我想将元组{1,2},{2,2},{2,4}放在字典的组[0]中。解决方案是什么?



我的尝试:



我试过以下这一行。但是,它有错误:





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

推荐答案

您忘记了列表。试试,例如

You forgot the list. Try, for instance
#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;
}


这篇关于如何在C ++中初始化字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆