【问题标题】:Cannot iterate a flat map无法迭代平面地图
【发布时间】:2014-01-31 16:08:33
【问题描述】:

我正在使用 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 flat map 在代码的其他地方声明如下:

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.

【问题讨论】:

  • 请使用自包含示例代码:sscce.org 所以问题不应该是代码难题,如果您希望它们得到回答

标签: boost map iterator flatmap


【解决方案1】:

您的代码似乎完全混乱。并且错误仅与显示的代码略有相关...[1]

没关系,我发现在您的实际代码 (ShmServer::fake_notify) 中,您 声明了 allocInst几乎 喜欢你展示了,但是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();

所以是的...您正在尝试将分配器作为分配器 type 参数传递?相反,使用类型作为模板参数。注意使用 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(您没有显示,但已声明且从未使用过......)?

【讨论】:

    猜你喜欢
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-11
    • 2013-01-07
    • 1970-01-01
    相关资源
    最近更新 更多