将std :: map直接插入std :: vector [英] Insert std::map into std::vector directly

查看:314
本文介绍了将std :: map直接插入std :: vector的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果这个问题很琐碎。

Sorry if the question is very trivial.

我有一个地图矢量:

typedef map<char, int> edges;
typedef vector<edges> nodes;

nodes n;

现在,假设我想推动玩具的发展。我尝试了不同的事情,而我的工作是

Now let's say I want to push a toy edge. I tried different things and what I worked is

edges e;        //declare an edge
e['c'] = 1;     //initialize it
n.push_back(e);  //push it to the vector

我怎样才能将一对边的值(' c'和2),而不必声明变量并对其进行初始化?

How can I just push the pair of values of an edge ('c' and 2) without having to declare a variable and initialize it?

类似的东西:

n.push_back(edges('c',2));

但是编译器给出错误

error: no matching function for call to ‘std::map<char, int>::map(char, int)’


推荐答案

您可以列出初始化:

nodes vec {
    { {'a', 12}, {'b', 32} },
    { {'c', 77} },
};

vec.push_back(
        { {'d', 88}, {'e', 99} }
        );

这篇关于将std :: map直接插入std :: vector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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