1

有什么方法可以利用 boost::bind 在进行迭代器插入时“绑定”一组键的值?本质上,我想遍历一组键,并将它们插入到具有给定值的映射中。

map<int, int> mymap;
set<int> myset;
myset.insert(1);
myset.insert(2);
myset.insert(3);
....
myset.insert(100);


for_each(myset.begin(), myset.end(), boost::bind(&mymap.insert,_1, 5); //Should be some make_pair() in here, but not sure how to make this work
4

1 回答 1

1

是的,这是可能的,但你不会对此感到满意。

它看起来像:

    std::for_each(
      myset.begin()
    , myset.end()
    , std::bind(
          &map<int, int>::insert
        , &mymap
        , std::bind(
              std::make_pair<int, int>
              , std::bind(
                    &std::set<int>::iterator::operator*
                  , std::placeholders::_1
                  )
              , 5
            )
        )
    );

(未测试此代码)

于 2013-03-18T21:04:04.913 回答