2015-12-26 30 views
4

我需要列表Map :: iterator和List :: iterator的映射。我如何能做到這一點:我如何定義map :: iterator的列表和map :: iterator的地圖

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

也許我可以使用類似前向聲明的迭代器?

+0

聽起來像一個遞歸定義。你確定這是你的問題的正確解決方案嗎?你想解決什麼問題? – PazO

+0

我確定。 如果我只使用普通指針,這不是一個問題,因爲我可以提前申報結構/類。在這種情況下,我想使用像普通指針這樣的迭代器。 –

+0

所以,你將得到一個Map,當給定一個'Int'時,將返回一個'iterator'到一個'List',它將一個迭代器保存到一個'Map'中,該迭代器保存和'迭代器' ... 等等。也許你的解決方案是3種不同類型? – PazO

回答

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