Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从单键多值容器访问特定值 我试图用C++实现一个多值容器,并可以自由访问里面的每个值。我有int键;输入X、Y、宽度、高度等值_C++_Iterator_Containers - Fatal编程技术网

从单键多值容器访问特定值 我试图用C++实现一个多值容器,并可以自由访问里面的每个值。我有int键;输入X、Y、宽度、高度等值

从单键多值容器访问特定值 我试图用C++实现一个多值容器,并可以自由访问里面的每个值。我有int键;输入X、Y、宽度、高度等值,c++,iterator,containers,C++,Iterator,Containers,我试图从每个键中提取值。 但是很明显,代码在这种情况下不起作用 我想得到一些建议,是否可以这样做,或者任何预定义的容器库在访问多个值方面是否具有更好的灵活性 我尝试了独立的单键、单值“multimap”容器,但它消耗了太多的内存空间和拖拽性能 multimap<int, multimap <multimap<int, int>, multimap<int, int>>> BlobPos = {}; //[<1,{(2,3),(4,5)}>

我试图从每个键中提取值。 但是很明显,代码在这种情况下不起作用

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

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

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;
}

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

但显然,代码在这种情况下不起作用。请清楚地说明问题,包括什么不应该发生,什么不应该发生,你做了什么试图解决它,等等。并提出你的建议。你为什么不在结构中分组X、Y、宽度、高度?你到底想要什么?我非常确定这种深层多重映射不是必需的。三重甚至双重嵌套多重映射都是一个危险信号。通过猜测编程是行不通的。看起来你在不了解它们的功能的情况下抛出了随机结构。你需要先读一本教科书。