获取内部 STL 容器的迭代器?

发布于 2024-08-04 17:07:23 字数 521 浏览 3 评论 0原文

我在尝试获取内部子容器的迭代器时遇到问题。

基本上想象一下这个简化的代码:

typedef map<string, map<string, map> > double_map;

double_map dm;
.. //some code here

for(double_map::iterator it = dm.begin(); it != dm.end(); it++){
    //some code here
    it->first // string
    it->second // <---- this is the 2nd map, how do i get its iterator so I can wrap it 
                       //in a for loop like above?
}

我需要能够在不为每个内部容器使用 typedef 的情况下执行此操作,有没有办法获取内部容器的迭代器?我的结构有 4 个内部容器,我需要遍历它们。

I am having trouble trying to get iterators for inner sub-containers.

Basically imagine this simplified code:

typedef map<string, map<string, map> > double_map;

double_map dm;
.. //some code here

for(double_map::iterator it = dm.begin(); it != dm.end(); it++){
    //some code here
    it->first // string
    it->second // <---- this is the 2nd map, how do i get its iterator so I can wrap it 
                       //in a for loop like above?
}

I need to be able to do this without using typedefs for every single inner container, is there a way to get an iterator for the inner container? I have structure that has 4 inner containers and I need to iterate through them all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独行侠 2024-08-11 17:07:23

(请注意,以下代码片段中没有编译任何内容。)

for(double_map::iterator it = dm.begin(); it != dm.end(); it++){
    //some code here
    it->first; // string
    it->second.begin();
    it->second.end();
}

编辑:我正确理解您的评论,您想要获取内部映射的迭代器的类型。如果是这样,这是一种方法:

double_map::mapped_type::iterator inner_iter = it->second.begin();

在我的脑海中,这也应该有效:

double_map::value_type::second_type::iterator inner_iter = it->second.begin();

(Beware, nothing compiled in the following snippets.)

for(double_map::iterator it = dm.begin(); it != dm.end(); it++){
    //some code here
    it->first; // string
    it->second.begin();
    it->second.end();
}

Edit: I fI understand your comment correctly, you want to get at the type of the iterators of the inner map. If that is so, here's one way:

double_map::mapped_type::iterator inner_iter = it->second.begin();

Off the top of my head, this should work, too:

double_map::value_type::second_type::iterator inner_iter = it->second.begin();
泅人 2024-08-11 17:07:23

简单的:

typedef map<string, map> inner_map; //Typedef for readability
typedef map<string, inner_map > double_map;

double_map dm;
.. //some code here
for(double_map::iterator it = dm.begin(); it != dm.end(); it++){
    //some code here
    it->first // string
    inner_map &innerMap(it->second); //Reference to the inner map for readability
    for(inner_map::iterator it2 = innerMap.begin(); it2 != innerMap.end(); ++it2) {
        //Do whatever you like
    }
}

Simple:

typedef map<string, map> inner_map; //Typedef for readability
typedef map<string, inner_map > double_map;

double_map dm;
.. //some code here
for(double_map::iterator it = dm.begin(); it != dm.end(); it++){
    //some code here
    it->first // string
    inner_map &innerMap(it->second); //Reference to the inner map for readability
    for(inner_map::iterator it2 = innerMap.begin(); it2 != innerMap.end(); ++it2) {
        //Do whatever you like
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文