向量作为哈希映射中的键值对
Vector as key value pair in Hash map
我正在尝试使用 std::vector 类型的键值对之一在 C++ 中创建一个 hash_map。我没有得到的是如何在散列的向量部分插入多个值-table?
hash_map<string, int> hm;
hm.insert(make_pair("one", 1));
hm.insert(make_pair("three", 2));
上面的例子是使用不带向量的散列映射作为键值对值的简单方法。
下面的示例使用 Vector。我正在尝试为每个相应的字符串值添加多个 int 值,例如=> "one" & (1,2,3) 而不是 "one" & (1).
hash_map<string, std::vector<int>> hm;
hm.insert(make_pair("one", ?)); // How do I insert values in both the vector as well as hash_map
hm.insert(make_pair("three", ?)); // How do I insert values in both the vector as well as hash_map
如果您想知道为什么在这里使用向量,基本上我是在尝试为每个对应的字符串值添加多个值而不是单个 int 值。
hash_map<string, std::vector<int>> hm;
hm.insert(make_pair("one", vector<int>{1,2,3})); // How do I insert values in both the vector as well as hash_map
hm.insert(make_pair("three", vector<int>{4,5,6}));
您可以执行以下操作:
std::unordered_map<std::string, std::vector<int>> hm;
hm.emplace("one", std::vector<int>{ 1, 2, 3 });
如果您想稍后添加,您可以执行:
hm["one"].push_back(4);
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
int main()
{
std::unordered_map<std::string, std::vector<int> > hm;
hm["one"]={1,2,3};
hm["two"]={5,6,7};
for (const auto&p : hm)
{
std::cout<< p.first << ": ";
for (const auto &i : p.second)
std::cout<< i << ", ";
std::cout<< std::endl;
}
}
这个输出:
两个:5、6、7,
一个: 1, 2, 3,
前面的回答基本都对(我只是没测试)。在核心中,他们使用 vector
构造函数,该构造函数采用初始化列表,这是直接创建枚举值的向量的唯一方法。不过,我想展示我认为更好的方法来做你真正想做的事——为给定的字符串键设置一个新值。
此容器的 operator[string]
return 对应值的引用,此处 vector<int>
。如果键是新的,它首先也创建一个新值(向量),然后插入该对。然后,vector<int>
的 operator=
将从初始化列表中分配。我想说的是,只有当你找到不使用它的重要理由时,你才应该使用其他变体而不是这个直接变体,因为它更惯用也更直接。
我正在尝试使用 std::vector 类型的键值对之一在 C++ 中创建一个 hash_map。我没有得到的是如何在散列的向量部分插入多个值-table?
hash_map<string, int> hm;
hm.insert(make_pair("one", 1));
hm.insert(make_pair("three", 2));
上面的例子是使用不带向量的散列映射作为键值对值的简单方法。
下面的示例使用 Vector。我正在尝试为每个相应的字符串值添加多个 int 值,例如=> "one" & (1,2,3) 而不是 "one" & (1).
hash_map<string, std::vector<int>> hm;
hm.insert(make_pair("one", ?)); // How do I insert values in both the vector as well as hash_map
hm.insert(make_pair("three", ?)); // How do I insert values in both the vector as well as hash_map
如果您想知道为什么在这里使用向量,基本上我是在尝试为每个对应的字符串值添加多个值而不是单个 int 值。
hash_map<string, std::vector<int>> hm;
hm.insert(make_pair("one", vector<int>{1,2,3})); // How do I insert values in both the vector as well as hash_map
hm.insert(make_pair("three", vector<int>{4,5,6}));
您可以执行以下操作:
std::unordered_map<std::string, std::vector<int>> hm;
hm.emplace("one", std::vector<int>{ 1, 2, 3 });
如果您想稍后添加,您可以执行:
hm["one"].push_back(4);
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
int main()
{
std::unordered_map<std::string, std::vector<int> > hm;
hm["one"]={1,2,3};
hm["two"]={5,6,7};
for (const auto&p : hm)
{
std::cout<< p.first << ": ";
for (const auto &i : p.second)
std::cout<< i << ", ";
std::cout<< std::endl;
}
}
这个输出:
两个:5、6、7,
一个: 1, 2, 3,
前面的回答基本都对(我只是没测试)。在核心中,他们使用 vector
构造函数,该构造函数采用初始化列表,这是直接创建枚举值的向量的唯一方法。不过,我想展示我认为更好的方法来做你真正想做的事——为给定的字符串键设置一个新值。
此容器的 operator[string]
return 对应值的引用,此处 vector<int>
。如果键是新的,它首先也创建一个新值(向量),然后插入该对。然后,vector<int>
的 operator=
将从初始化列表中分配。我想说的是,只有当你找到不使用它的重要理由时,你才应该使用其他变体而不是这个直接变体,因为它更惯用也更直接。