C++容器存储(insert/get/remove)结构,具有整数值的多键是指针
C++ container to store(insert/get/remove) structure with multi key of integer value is pointer
如何存储以下结构密钥=[k1,k2,…,kn];值=无效*n在编译时是未知的。它对于对象是常量,但对于类是可变的。它必须像树,但是C++STL。它必须尽可能快地插入、移除和获取。我认为它一定是map[inter,map[interer,…map[interger,void*]…]]但我不明白如何在未知级别"n"的情况下实现它。
它用于"队列",其中项目可以按未知顺序插入,但必须按正确的顺序退出队列1,1,1,然后1,1,2。。。。1,1,V3max,1,2,1。。。
您可以创建这样一个简单的结构:
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
您可以将std::vector<int>
与operator<
和operator==
一起用作密钥。
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)
{
// ...
}
相关文章:
- 为什么在将 void 指针转换为整数指针时出现分段错误
- 整数指针数组的元素是否可以指向整数数组?
- 整数指针与字符指针的情况下悬空指针
- 整数指针数组的大小
- 使用整数指针将左值转换为右值
- 打印整数指针元素时的结果不同
- 将空间分配给整数指针时出错
- 异常包括字符指针和整数指针
- 我可以在 c++ 中使用整数指针调用类的成员函数吗?
- 为什么我们不能使用整数指针声明一个数字数组
- 当我们在 c++ 中将常量整数引用分配给整数指针时,行为是什么
- 为什么允许常量整数指针指向非常量整数
- 在C++中为整数指针编制索引
- 创建字符指针和整数指针数组
- 指向整数指针的 C++ 指针
- 字符结果和整数指针之间的差异
- 删除整数指针时析构函数末尾的分段错误
- 是否可以将整数指针转换为位于该内存位置的实际整数
- 将树的每个节点替换为节点左侧所有值的和,而不使用额外的整数指针参数
- 我允许传递一个整数指针到一个函数,在c++中接受一个整数数组作为参数