c++ - 如何在 Boost 中使用元组映射?

标签 c++ boost

尝试将 id 映射到包含任意数据的元组:

typedef boost::tuples::tuple<std::string, std::string, unsigned char> data;
typedef std::map<uint64, data> data_map;

const data_map DATA = boost::assign::map_list_of
        (0, boost::tuples::make_tuple("XXX", "xxx", 0))
        (0, boost::tuples::make_tuple("YYY", "yyy", 1))
        (0, boost::tuples::make_tuple("ZZZ", "zzz", 2))

如何访问 DATA 映射中的元组?我正在尝试:

data_map::const_iterator itData = DATA.find(id);

然后访问它(在检查是否找到之后):

data metadata = itData->second;

抛出错误:

error: invalid initialization of reference of type 'boost::tuples::cons<const char (&)[12], boost::tuples::cons<int, boost::tuples::null_type> >::stored_head_type {aka const char (&)[12]}' from expression of type 'const char [11]'

最佳答案

吸引我的 Crystal 球:

我认为您使用过 decltype(obj),其中 object 是 std::make_tuple("abcdefghijk",...)std 的结果::领带(“abcdefghij”,...)

这是因为错误消息谈到这种类型的元组元素 (const char (&)[12])。


证明上面的代码没问题:

Live On Coliru

#include <boost/assign.hpp>
#include <boost/tuple/tuple.hpp>
#include <map>

typedef boost::tuples::tuple<std::string, std::string, unsigned char> data;
typedef std::map<uint64_t, data> data_map;

int main() {

    const data_map DATA = boost::assign::map_list_of
            (0, boost::tuples::make_tuple("XXX", "xxx", 0))
            (1, boost::tuples::make_tuple("YYY", "yyy", 1))
            (2, boost::tuples::make_tuple("ZZZ", "zzz", 2));


    data_map::const_iterator itData = DATA.find(2);
    data metadata = itData->second;
}

关于c++ - 如何在 Boost 中使用元组映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32997984/

相关文章:

c++ - 'managed_shared_memory' 应该分配多少内存? ( boost )

c++ - Boost::GIL:读取具有 alpha channel 的 *.png 图像缺少抗锯齿功能

c++ - GetVolumeInformation() 产生不同的磁盘序列号

c++ - 错误:boost.fusion::for_each() 和从 boost.tuple 派生的结构

c++ - 在字符串 C++ 中转换 Long?

c++ - 为什么 const_cast 不修改调用函数中的值?

c++ - 使用 BOOST.python 将结构从 C++ 返回到 Python

c++ - 共享内存中的 pcl::PointCloud

C++ Map<Vertex*,Position> 并使用 MAP.FIND 与真实顶点比较

c++ - 用于存储 3D float 位置的莫顿代码