c++ - boost::assign::map_list_of 中的奇怪函数

标签 c++ boost syntax

<分区>

我发现这个结构:

std::map<T, T> a = boost::assign::map_list_of(arg11, arg12)
                                             (arg21, arg22)
                                             ;

请告诉我这里面发生了什么,以及如何在 C++ 上实现它?

最佳答案

我没有阅读实现,但我认为您可以使用“累加器”对象重现它:

template<class T>
class Acc
{
public:
   Acc &operator()(T a, T b)
   {
       map.insert(std::make_pair(a, b));
   }
   operator std::map<T, T>()
   {
       return map;
   }
 private:
   std::map<T, T> map;
}

每次你做类似 (a,b) 的事情时,你都会调用 operator(),它只是在映射中添加 std::pair(a, b)。转换运算符允许获取 std::map 而不是 Acc 对象。

用途是:

std::map<int, int> = Acc<int>()(1,2)(3,4)(6,7);

注意:代码未经测试。

关于c++ - boost::assign::map_list_of 中的奇怪函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17576058/

相关文章:

C++ 编写解释器 - 确定 break 语句的循环 objective-c ++

c++ - 使用 boost::asio 进行简单的 udp 通信

c++ - boost::filesystem::space() 报告错误的磁盘空间

c++ - 传递 shared_array<T> 参数

java - token "System"出现语法错误,删除此 token - 输出无法解析或不是字段

c++ - 如何将QImage转换为opencv Mat

c++ - 我目前正在创建我的第一个 Windows 设备驱动程序 "Hello world."事件。我正在使用 Microsoft Visual Studio 2012。

C++语法问题

c - 使用唯一 ASCII 字符的最长 C 代码行是哪一行?

c++ - C++ 中的舍入和往返数字