0

我正在使用 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.
4

1 回答 1

2

您的代码似乎完全混乱。并且错误仅与显示的代码略有相关... [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();

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

于 2014-01-31T23:10:18.173 回答