是否可以用向量 <T> 定义 unordered_map 作为值?
Is it possible to define an unordered_map with a vector<T> as value?
我有一个向量模式,它们的吸气剂是这样的:
vector<A>& getA() const { return a; }
vector<B>& getB() const { return b; }
vector<C>& getC() const { return c; }
...
我对 vector<T>& getByName(string s) const
这样的函数很感兴趣,这样我就可以这样调用函数:getByName("A")
、getByName("B")
等等。
我试过使用无序地图,但我还没有找到任何合适的方法来制作像这样的线:unordered_map< string, vector<T> >
。
A、B、C...是完全不同的结构,所以多态的解决方案不是我尽可能寻找的。
由于 vector<T>
是一个固定大小的容器(如果我没记错的话是 24 字节),我不明白为什么地图无法将这些字节存储在内存中的某个位置,无论矢量的类型。
As vector is a container with a fixed size (24 bytes if I'm not wrong), I don't see why the map is not able to store those bytes somewhere in memory no matter the vector's type.
因为C++是强类型语言,而且vector<A>
、vector<B>
和vector<C>
类型是相同的size没关系,因为它们 类型 不同。
A pair<int16_t, int16_t>
与 int32_t
大小相同(在大多数实现中),但这并不能使它们互换。
您可以(我想)有 map<string, variant<...>
或 map<string, any>
,但这需要您自己管理所有类型。
[稍后]
I'm interesed in having some kind of function like vector<T>& getByName(string s)
此类函数的问题在于您必须(在编译时)指定函数 return 的类型。您希望 return 类型由运行时传递给函数的参数值决定。
您不能在一个 unordered_map
中包含 vector<A>
、vector<B>
、..,因为它们具有不同的类型。我认为下面的解决方案最接近您的需要:
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
template<typename T>
class my_unordered_map :
public unordered_map<string, vector<T>> {
};
template<typename T>
vector<T>& get_by_name(const string& s, my_unordered_map<T>& mm) {
return mm[s];
}
class A {
public:
string name;
A() = default;
explicit A(const string& s) : name{s} {}
};
int main() {
vector<A> vec_a1{A{"A1"}};
my_unordered_map<A> mm;
mm["A"] = vec_a1;
vector<A>& vec_a2 = get_by_name("A", mm); //vec_a2 is the same as mm["A"]
vec_a2.emplace_back("B");
for (const auto& item : vec_a2) {
cout << item.name << "\n";
}
return 0;
}
我有一个向量模式,它们的吸气剂是这样的:
vector<A>& getA() const { return a; }
vector<B>& getB() const { return b; }
vector<C>& getC() const { return c; }
...
我对 vector<T>& getByName(string s) const
这样的函数很感兴趣,这样我就可以这样调用函数:getByName("A")
、getByName("B")
等等。
我试过使用无序地图,但我还没有找到任何合适的方法来制作像这样的线:unordered_map< string, vector<T> >
。
A、B、C...是完全不同的结构,所以多态的解决方案不是我尽可能寻找的。
由于 vector<T>
是一个固定大小的容器(如果我没记错的话是 24 字节),我不明白为什么地图无法将这些字节存储在内存中的某个位置,无论矢量的类型。
As vector is a container with a fixed size (24 bytes if I'm not wrong), I don't see why the map is not able to store those bytes somewhere in memory no matter the vector's type.
因为C++是强类型语言,而且vector<A>
、vector<B>
和vector<C>
类型是相同的size没关系,因为它们 类型 不同。
A pair<int16_t, int16_t>
与 int32_t
大小相同(在大多数实现中),但这并不能使它们互换。
您可以(我想)有 map<string, variant<...>
或 map<string, any>
,但这需要您自己管理所有类型。
[稍后]
I'm interesed in having some kind of function like
vector<T>& getByName(string s)
此类函数的问题在于您必须(在编译时)指定函数 return 的类型。您希望 return 类型由运行时传递给函数的参数值决定。
您不能在一个 unordered_map
中包含 vector<A>
、vector<B>
、..,因为它们具有不同的类型。我认为下面的解决方案最接近您的需要:
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
using namespace std;
template<typename T>
class my_unordered_map :
public unordered_map<string, vector<T>> {
};
template<typename T>
vector<T>& get_by_name(const string& s, my_unordered_map<T>& mm) {
return mm[s];
}
class A {
public:
string name;
A() = default;
explicit A(const string& s) : name{s} {}
};
int main() {
vector<A> vec_a1{A{"A1"}};
my_unordered_map<A> mm;
mm["A"] = vec_a1;
vector<A>& vec_a2 = get_by_name("A", mm); //vec_a2 is the same as mm["A"]
vec_a2.emplace_back("B");
for (const auto& item : vec_a2) {
cout << item.name << "\n";
}
return 0;
}