C++容器存储(insert/get/remove)具有整数值多键的结构是指针
C++ container to store(insert/get/remove) structure with multi key of integer value is pointer
如何存储以下结构
键=[k1,k2,...,kn];价值=无效*
n 在编译时是未知的。它对于 object 是不变的,但对于 class 是变化的。
它必须像树,但 C++ STL。它必须尽可能快地插入、移除和获取。
我认为它必须是 map[integer, map[integer, ... map[integer, void*]...]] 但我无法理解如何以未知级别 "n".[=11 实现它=]
它用于"queue",其中项目可以以未知顺序插入,但必须按正确顺序出列 1,1,1 然后 1,1,2, .... 1,1, V3max, 1,2,1, ...
您可以使用带有 operator<
和 operator==
的 std::vector<int>
作为键。
bool operator<(const std::vector<int>& a, const std::vector<int>& b)
{
if (a.size() != b.size())
{
// decide what to do
}
for (std::vector<int>::size_type index = 0 ; index < a.size() ; ++index)
{
if (a[index] < b[index])
{
return true;
}
}
return false;
}
bool operator==(const std::vector<int>& a, const std::vector<int>& b)
{
// ...
}
您可以像这样创建一个简单的结构:
struct MyType
{
std::string name;
std::map<int, MyType> mA;
};
然后创建一个接受由键标识的 MyType 的根映射
std::map<int, MyType> mRootMap;
然后你可以这样添加(为了简单,没有递归)
MyType a;
MyType b;
MyType c;
c.name = "This is C";
b.mA[789] = c;
a.mA[456] = b;
mRootMap[123] = a;
那么你可以这样访问
std::cout << mRootMap[123].mA[456].mA[789].name << std::endl;
输出:
This is C
如何存储以下结构 键=[k1,k2,...,kn];价值=无效* n 在编译时是未知的。它对于 object 是不变的,但对于 class 是变化的。 它必须像树,但 C++ STL。它必须尽可能快地插入、移除和获取。 我认为它必须是 map[integer, map[integer, ... map[integer, void*]...]] 但我无法理解如何以未知级别 "n".[=11 实现它=]
它用于"queue",其中项目可以以未知顺序插入,但必须按正确顺序出列 1,1,1 然后 1,1,2, .... 1,1, V3max, 1,2,1, ...
您可以使用带有 operator<
和 operator==
的 std::vector<int>
作为键。
bool operator<(const std::vector<int>& a, const std::vector<int>& b)
{
if (a.size() != b.size())
{
// decide what to do
}
for (std::vector<int>::size_type index = 0 ; index < a.size() ; ++index)
{
if (a[index] < b[index])
{
return true;
}
}
return false;
}
bool operator==(const std::vector<int>& a, const std::vector<int>& b)
{
// ...
}
您可以像这样创建一个简单的结构:
struct MyType
{
std::string name;
std::map<int, MyType> mA;
};
然后创建一个接受由键标识的 MyType 的根映射
std::map<int, MyType> mRootMap;
然后你可以这样添加(为了简单,没有递归)
MyType a;
MyType b;
MyType c;
c.name = "This is C";
b.mA[789] = c;
a.mA[456] = b;
mRootMap[123] = a;
那么你可以这样访问
std::cout << mRootMap[123].mA[456].mA[789].name << std::endl;
输出:
This is C