Boost 无法迭代平面映射

Boost 无法迭代平面映射,boost,map,iterator,flatmap,Boost,Map,Iterator,Flatmap,我正在使用boost flat_映射,并试图对其进行迭代,但是,我不知道如何创建迭代器 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, st

我正在使用boost flat_映射,并试图对其进行迭代,但是,我不知道如何创建迭代器

 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

 }
my_map=mySeg.find(“temp_map”)。首先//获取指向地图的指针
tlsShmemAllocator alloc_inst(mySeg.get_segment_manager());
for(boost::container::flat_map::const_iterator row=my_map->begin();
行!=我的地图->结束();
++(世界其他地区)
{
//做事
}
“tlsStorage”是我用来存储数据库数据的结构。boost平面图在代码中的其他地方声明如下:

boost::container::flat_map tls_temp_map = mySeg.construct<tlsSHMMap>("temp_map")    (std::less<int>() ,alloc_inst);     //object name
boost::container::flat\u map tls\u temp\u map=mySeg.construct(“temp\u map”)(std::less(),alloc\u inst)//对象名
我上面的代码不起作用。这里是错误,有什么想法吗

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.
src/dbm/dbm_shm_server.cc:在成员函数“int redcom::dbm::ShmServer::StartServer()”中:
src/dbm/dbm_shm_server.cc:353:24:警告:未使用的变量“tls\u main\u map”[-Wunused variable]
tlsSHMMap*tls_主地图;
^
src/dbm/dbm_shm_server.cc:354:24:警告:未使用的变量“tls_temp_map”[-Wunused variable]
tlsSHMMap*tls\U temp\U map;
^
src/dbm/dbm_shm_server.cc:在成员函数“void redcom::dbm::ShmServer::fake_notify()”中:
src/dbm/dbm_shm_server.cc:2023:84:错误:“alloc_inst”的值在常量表达式中不可用
for(boost::container::flat_map::const_iterator row=my_map->begin();
^
src/dbm/dbm_shm_server.cc:2021:40:注意:“alloc_inst”未声明为“constexpr”
常量tlsShmemAllocator alloc_inst(mySeg.get_segment_manager());
^
src/dbm/dbm_shm_server.cc:2023:95:错误:“模板类boost::容器::平面映射”的模板参数列表中参数4的类型/值不匹配
for(boost::container::flat_map::const_iterator row=my_map->begin();
^
src/dbm/dbm_shm_server.cc:2023:95:错误:应为类型,获取“alloc_inst”
src/dbm/dbm_shm_server.cc:2023:113:错误:“行”之前的声明中的类型无效
for(boost::container::flat_map::const_iterator row=my_map->begin();
^
src/dbm/dbm_shm_server.cc:2023:113:错误:在“行”之前应为“;”
src/dbm/dbm_shm_server.cc:2023:113:错误:未在此作用域中声明“行”
src/dbm/dbm_shm_server.cc:2024:37:错误:在“;”标记之前应为“')
行!=我的地图->结束();
^
src/dbm/dbm_shm_server.cc:2023:98:警告:未使用的变量“const_iterator”[-Wunused variable]
for(boost::container::flat_map::const_iterator row=my_map->begin();
^
src/dbm/dbm_shm_server.cc:2025:19:错误:未在此范围内声明“行”
++(世界其他地区)
^
src/dbm/dbm_shm_server.cc:2025:22:错误:应为“;”before')标记
++(世界其他地区)
^
distcc[31606]错误:在本地主机上编译src/dbm/dbm_shm_server.cc失败
scons:**[debug/build/x86_64-unknown-freebsd9.2/dbm/dbm_shm_server.o]错误1
scons:由于错误而终止生成。

您的代码似乎完全混淆了。而错误只与所示代码稍有关联…[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();
或者更切题地说:

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\u temp\u map
    ?或者
    tls\u main\u map
    (您没有显示,但是声明了并且从未使用过…)


请使用自足的示例代码:因此,如果您希望得到答案,问题不应该是代码难题
for(auto const& row : *my_map)
{
     int id       = row.first;
     auto& rowData = row.second;
}
tlsSHMMap* tls_temp_map;