3

std::string の値を反復処理して、最後に到達したときに最初からやり直すことができるある種のイテレータがあるかどうか疑問に思っています。つまり、このオブジェクトは無期限に繰り返され、同じ値のシーケンスを何度も吐き出します。

ありがとう!

4

1 に答える 1

5

ジェネレーター関数はそれである可能性があります。Boost Iterator には、そのためのイテレータ アダプタがあります。

サンプル: http://coliru.stacked-crooked.com/a/267279405be9289d

#include <iostream>
#include <functional>
#include <algorithm>
#include <iterator>
#include <boost/generator_iterator.hpp>

int main()
{
  const std::string data = "hello";
  auto curr = data.end();

  std::function<char()> gen = [curr,data]() mutable -> char
  { 
      if (curr==data.end())
          curr = data.begin();
      return *curr++;
  };

  auto it = boost::make_generator_iterator(gen);
  std::copy_n(it, 35, std::ostream_iterator<char>(std::cout, ";"));
}
于 2012-11-07T22:03:12.510 に答える