简体   繁体   English

C ++相当于python的itertools :: cycle

[英]C++ equivalent to python's itertools::cycle

Is there a C++ class (or libboost class) similar to python's itertools::cycle ? 是否有类似于python的itertools :: cycle的C ++类(或libboost类)?

I am looking for a container class that I can "roll" or "shift in a cycle". 我正在寻找一个容器类,我可以“滚动”或“转换一个循环”。 Example with integers : 整数示例:

MyCont = {{1,2,3}}
MyCont.roll()  // MyCont now holds {{2,3,1}}
cout << MyCont[0] << endl; // returns 2
cout << MyCont[1] << endl; // returns 3

Do I need to implement it myself, or is there an existing implementation somewhere ? 我是否需要自己实现它,或者某处是否存在现有实现?

Edit 编辑

The above example demonstrates the behaviour I am looking for, but my case is slightly more difficult : my container holds objects, each of them embedding a large array. 上面的例子演示了我正在寻找的行为,但我的情况稍微有点困难:我的容器包含对象,每个对象都嵌入一个大型数组。 I want to reorder the container's items without performing deep copies. 我想重新排序容器的项目而不执行深层复制。 What is the best way to do so ? 这样做的最佳方法是什么?

Yes. 是。 There is rotate in algorithms. 算法中有rotate

For your updated issue you are correct in thinking to hold pointers to the objects in an array and rotate them. 对于您更新的问题,您可以考虑保持指向数组中对象的指针并旋转它们。 Or you could try list and use the internal swap method which "Does not invoke any move, copy, or swap operations on individual elements." 或者您可以尝试list并使用内部swap方法“不对单个元素调用任何移动,复制或交换操作”。 or the specialisation of swap for lists 或列表swap的专业化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM