我想知道是否有一种迭代器可以迭代std :: string中的值,并在到达末尾时从开头重新开始。换句话说,该对象将无限迭代,一遍又一遍地输出相同的值序列。
谢谢!
谢谢!
#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, ";"));
}