iterator adapter to iterate just the values in a map?

前端 未结 4 1128
故里飘歌
故里飘歌 2020-12-01 16:17

I\'m just getting back into C++ after a couple of years of doing a lot of C#, and recently Objective C.

One thing I\'ve done before is to roll my own iterator adapte

4条回答
  •  -上瘾入骨i
    2020-12-01 17:09

    I don't think there's anything out of the box. You can use boost::make_transform.

    template T2& take_second(const std::pair &a_pair) 
    {
      return a_pair.second;
    }
    
    void run_map_value()
    {
      map a_map;
      a_map[0] = "zero";
      a_map[1] = "one";
      a_map[2] = "two";
      copy( boost::make_transform_iterator(a_map.begin(), take_second),
        boost::make_transform_iterator(a_map.end(), take_second),
        ostream_iterator(cout, "\n")
        );
    }
    

提交回复
热议问题