无法迭代平面地图

时间:2014-01-31 16:08:33

标签: boost map iterator flatmap

我正在使用boost flat_map并尝试迭代它,但是,我无法弄清楚如何创建迭代器。

 my_map = mySeg.find<tlsSHMMap>("temp_map").first;   //fetch a pointer to the map
 tlsShmemAllocator alloc_inst (mySeg.get_segment_manager());


 for (boost::container::flat_map<int, tlsStorage, std::less<int>() ,alloc_inst >::const_iterator row = my_map->begin();
 row != my_map->end();
 ++row)
 {
    //do stuff

 }

'tlsStorage'是一个用于存储数据库数据的结构。 boost平面图在代码中的其他地方声明如下:

boost::container::flat_map tls_temp_map = mySeg.construct<tlsSHMMap>("temp_map")    (std::less<int>() ,alloc_inst);     //object name

我上面的代码不起作用。以下是错误,任何想法?

src/dbm/dbm_shm_server.cc: In member function 'int redcom::dbm::ShmServer::StartServer()':
src/dbm/dbm_shm_server.cc:353:24: warning: unused variable 'tls_main_map' [-Wunused-variable]
             tlsSHMMap* tls_main_map;
                        ^
src/dbm/dbm_shm_server.cc:354:24: warning: unused variable 'tls_temp_map' [-Wunused-variable]
             tlsSHMMap* tls_temp_map;
                        ^
src/dbm/dbm_shm_server.cc: In member function 'void redcom::dbm::ShmServer::fake_notify()':
src/dbm/dbm_shm_server.cc:2023:84: error: the value of 'alloc_inst' is not usable in a constant expression
                 for (boost::container::flat_map<int, tlsStorage, std::less<int>() ,alloc_inst >::const_iterator row = my_map->begin();
                                                                                    ^
src/dbm/dbm_shm_server.cc:2021:40: note: 'alloc_inst' was not declared 'constexpr'
                const tlsShmemAllocator alloc_inst (mySeg.get_segment_manager());
                                        ^
src/dbm/dbm_shm_server.cc:2023:95: error: type/value mismatch at argument 4 in template parameter list for 'template<class Key, class T, class Compare, class Allocator> class boost::container::flat_map'
                 for (boost::container::flat_map<int, tlsStorage, std::less<int>() ,alloc_inst >::const_iterator row = my_map->begin();
                                                                                               ^
src/dbm/dbm_shm_server.cc:2023:95: error:   expected a type, got 'alloc_inst'
src/dbm/dbm_shm_server.cc:2023:113: error: invalid type in declaration before 'row'
                 for (boost::container::flat_map<int, tlsStorage, std::less<int>() ,alloc_inst >::const_iterator row = my_map->begin();
                                                                                                                 ^
src/dbm/dbm_shm_server.cc:2023:113: error: expected ';' before 'row'
src/dbm/dbm_shm_server.cc:2023:113: error: 'row' was not declared in this scope
src/dbm/dbm_shm_server.cc:2024:37: error: expected ')' before ';' token
                 row != my_map->end();
                                     ^
src/dbm/dbm_shm_server.cc:2023:98: warning: unused variable 'const_iterator' [-Wunused-variable]
                 for (boost::container::flat_map<int, tlsStorage, std::less<int>() ,alloc_inst >::const_iterator row = my_map->begin();
                                                                                                  ^
src/dbm/dbm_shm_server.cc:2025:19: error: 'row' was not declared in this scope
                 ++row)
                   ^
src/dbm/dbm_shm_server.cc:2025:22: error: expected ';' before ')' token
                 ++row)
                      ^
distcc[31606] ERROR: compile src/dbm/dbm_shm_server.cc on localhost failed
scons: *** [debug/build/x86_64-unknown-freebsd9.2/dbm/dbm_shm_server.o] Error 1
scons: building terminated because of errors.

1 个答案:

答案 0 :(得分:2)

您的代码似乎彻底混淆了。并且错误仅与显示的代码略有关系... [1]

没关系,我已经发现在您的实际代码(ShmServer::fake_notify)中,您 宣布allocInst 几乎 < / strong>就像你展示的那样,但const

const tlsShmemAllocator alloc_inst (mySeg.get_segment_manager());

这也很好地解释了为什么你的循环控制变量有一个无效的类型:

error: the value of 'alloc_inst' is not usable in a constant expression
error:   expected a type, got 'alloc_inst'

我的意思是,真的,编译器无法更明确地说明它。如果这个不够清楚,它增加了漂亮的ascii艺术:

for(flat_map<int, tlsStorage, std::less<int>() ,alloc_inst >::const_iterator row = my_map->begin();

所以是的...你试图将分配器作为分配器类型参数传递?相反,使用类型作为模板参数。注意使用typedef来减少代码混乱:

typedef boost::container::flat_map<
    int, tlsStorage, std::less<int>, tlsShmemAllocator> ShmTable;
typedef ShmTable::const_iterator ShmTableIt;

for(ShmTableIt rowit=my_map->begin(); rowit!=my_map->end(); ++rowit)
{
     ShmTableIt::value_type const& row = *rowit;

     int id                  = row.first;
     tlsStorage const& rowData = row.second;
}

现在,当然,使用C ++ 11,您可以在没有所有typedef的情况下编写

for(auto rowit=my_map->begin(); rowit!=my_map->end(); ++rowit)
{
     int id       = rowit->first;
     auto& rowData = rowit->second;
}

或更重要的是:

for(auto const& row : *my_map)
{
     int id       = row.first;
     auto& rowData = row.second;
}

尝试减少残缺,这样你就不会被代码所淹没:)


[1] 以下几点:

  • boost :: container :: flat_map是一个模板,因此您的声明可能不正确。我怀疑你确实有

    tlsSHMMap* tls_temp_map;
    

    你为什么要给我们假代码?这是无关的?

  • 事实上,你的实际代码中是my_map吗?还是tls_temp_map?或者tls_main_map(你没有展示,但是宣布并且从未使用过......)?