<分区>
我发现这个结构:
std::map<T, T> a = boost::assign::map_list_of(arg11, arg12)
(arg21, arg22)
;
请告诉我这里面发生了什么,以及如何在 C++ 上实现它?
<分区>
我发现这个结构:
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/