我该如何定义一个map::iterator列表和一个list::iterator的map?

4
我需要一个Map::iterator的列表和一个List::iterator的map,请问如何实现:

我该怎样做:

typedef std::list<Map::iterator> List;
typedef std::map<int, List::iterator> Map;

也许我可以使用类似于迭代器的前向声明?

听起来像是递归定义。你确定这是解决你问题的正确方法?你试图解决什么问题? - PazO
我确定。 如果我只使用普通指针,那就不是问题,因为我可以进行前向声明结构/类。在这种情况下,我想要像普通指针一样使用迭代器。 - Anton Todua
所以,您将拥有一个Map,当提供一个Int时,它将返回一个指向List的迭代器,该List包含Map的迭代器,该Map又包含List的迭代器......依此类推。也许您的解决方案是3种不同的类型? - PazO
可能的解决方案之一是使用类型抹消,将第二个参数设置为boost::any - Revolver_Ocelot
@PazO 我上面提出的代码只是一个简单的例子,仅用于说明问题。 - Anton Todua
显示剩余3条评论
1个回答

1
像这样的东西应该会对你有所帮助:

#include <cassert>
#include <iostream>
#include <list>
#include <map>
#include <string>

struct decl_t {
    typedef std::map<std::string, decl_t> map_t;
    typedef std::list<std::pair<int, typename map_t::iterator>> list_t;

    list_t::iterator it;
};

int main(int argc, const char* argv[])
{
    decl_t::map_t map;
    decl_t::list_t list;

    auto list_it = list.emplace(list.end(), 42, decl_t::map_t::iterator());
    const auto pair = std::make_pair(std::string("key"), decl_t{list_it});
    auto result = map.insert(pair);
    assert(result.second);
    auto map_it = result.first;
    list_it->second = map_it;

    std::cout << list_it->second->first << std::endl;
    std::cout << map_it->second.it->first << std::endl;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接