从单键多值容器访问特定值

Accessing specific value from single key-multiple value containers

本文关键字:访问 单键多      更新时间:2023-10-16

我正在尝试使用C++实现多值容器,并自由访问其中的每个值。我有整数键;X、Y、宽度、高度等值作为输入。

我正在尝试从每个键中提取值。但显然,代码在这种情况下不起作用。

我想得到一些建议,是否可以做到这一点,或者任何预定义的容器库在访问多个值方面具有更好的灵活性。

我尝试了独立的单键、单值"多映射"容器,但它消耗了太多内存空间和拖拽性能

multimap<int, multimap <multimap<int, int>, multimap<int, int>>> BlobPos = {};
//[<1,{(2,3),(4,5)}>,<2,{(6,7),(8,9)}>

for (auto it = BlobPos.begin();it != BlobPos.end(); it++) { 
auto X = it->second-> first->first; 
auto Y = it->second->first->second;
auto H = it->second->second->first;
auto W =  it->second-second->second;
cout << X << Y << H << W;

2 3 4 5
6 7 8 9

下面是一个结构几乎如您的问题中所述的容器的示例:

#include <map>
#include <utility>
#include <iostream>
int main() {
    std::map<int, std::pair<std::pair<int, int>, std::pair<int, int>>> BlobPos = {{1, {{2, 3}, {4, 5}}}, {2, {{6, 7}, {8, 9}}}};
    for (auto it = BlobPos.begin();it != BlobPos.end(); it++) { 
        auto X = it->second.first.first; 
        auto Y = it->second.first.second;
        auto H = it->second.second.first;
        auto W = it->second.second.second;
        std::cout << X << Y << H << W << 'n';
    }
    return 0;
}

引用需要运算符->但下一级使用运算符.来访问元素。