gpt4 book ai didi

c++ - 在 map C++ 中创建特定大小的 vector

转载 作者:行者123 更新时间:2023-11-28 08:17:03 26 4
gpt4 key购买 nike

嗨,假设我想创建一个 map<int, vector<node> > 形式的 map 定义节点的位置作为

struct node{
string key;
double pnum;
node():key(""),pnum(0) {}
};

现在如果有人给我一个 map 键,说“Element_1”和一个大小为 2 的 vector 是否可以/安全地创建键“Element_1”下的 vector ,如下所示?

int main(void)
{
map<string,vector<node> > samplemap;
samplemap["Element_1"] = vector<node>(2);
}

上面的代码可以编译,我可以打印出存储在键“Element_1”下的 vector 。 (打印的默认值)。

最佳答案

If someone gives me a map key, say "Element_1" and a vector size 2 is it alright/safe to create the vector under the key "Element_1" as it has been done below?

是的,我的 friend 。如果键已经存在,那么它会更新关联的值,否则它会创建一个带有默认值[即vector<node>()]的的新条目。 ] 然后更新指定的值 [即 vector<node>(2) ].

顺便说一下,vector<node>(2)创建一个大小为 2 的 vector .这意味着,它将有两个 node 类型的元素。通过调用 node 的默认构造函数创建.

关于c++ - 在 map C++ 中创建特定大小的 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7391649/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com