Skipping in Range-based for based on 'index'?

后端 未结 7 1316
一向
一向 2021-01-02 01:10

Is there a way to access the iterator (suppose there\'s no loop index..?) in a C++11 range-based for loop?

Often we need to do something special with the fi

相关标签:
7条回答
  • 2021-01-02 02:14

    Boost provides a nice succinct way to do this:

    std::vector<int> xs{ 1, 2, 3, 4, 5 };
    for (const auto &x : boost::make_iterator_range(xs.begin() + 1, xs.end())) {
      std::cout << x << " ";
    }
    // Prints: 2 3 4 5
    

    You can find make_iterator_range in the boost/range/iterator_range.hpp header.

    0 讨论(0)
提交回复
热议问题