带有boost :: adapter :: indexed的基于范围的循环

gnz*_*lbg 6 c++11 boost-range

基于C++ 11范围的for循环取消引用迭代器.这是否意味着使用它没有意义boost::adaptors::indexed?例:

boost::counting_range numbers(10,20);
for(auto i : numbers | indexed(0)) {
  cout << "number = " i 
  /* << " | index = " << i.index() */ // i is an integer!
  << "\n";
}
Run Code Online (Sandbox Code Playgroud)

我总是可以使用计数器,但我喜欢索引迭代器.

  • 是否有可能以某种方式使用基于范围的for循环?
  • 使用带索引的基于范围的循环的习惯用法是什么?(只是一个普通的柜台?)

eca*_*mur 4

该问题已在 Boost 1.56(2014 年 8 月发布)中修复;该元素间接位于value_typewithindex()value()成员函数后面。

示例:http ://coliru.stacked-crooked.com/a/e95bdff0a9d371ea

auto numbers = boost::counting_range(10, 20);
for (auto i : numbers | boost::adaptors::indexed())
    std::cout << "number = " << i.value()
        << " | index = " << i.index() << "\n";
Run Code Online (Sandbox Code Playgroud)