gpt4 book ai didi

c++ - 是否有类似于循环(循环)迭代器的东西?

转载 作者:太空狗 更新时间:2023-10-29 20:44:25 27 4
gpt4 key购买 nike

我想知道是否有某种迭代器可以迭代 std::string 中的值,当它到达末尾时从头开始。换句话说,这个对象会无限迭代,一遍又一遍地吐出相同的值序列。

谢谢!

最佳答案

生成器函数可能就是这样。 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, ";"));
}

关于c++ - 是否有类似于循环(循环)迭代器的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13279023/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com