围绕迭代器的一些帖子,使用insert
和splice
here函数,列表为here,但我仍然无法为我的情况翻译它们我正在遍历列表,如果满足条件,我想将元素拼接(移动)到另一个列表,但是如here所述,迭代器跳转到splices容器。如下例所示,如何使迭代器与原始循环相关。
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <ctime>
#include <list>
using namespace std;
class Individual {
public:
Individual(bool state) : state_(state) {}
bool my_state(void) {
return state_;
}
private:
bool state_ = true;
};
int main () {
cout << "----------Enter Main----------" << endl;
list<Individual> list_individuals;
list<Individual> cache_list_individuals;
// initialise
for (auto i = 0; i < 100; ++i) {
if (i <= 50)
list_individuals.push_back(new Individual(true));
else
list_individuals.push_back(new Individual(false));
}
unsigned counter = 0;
for (auto iter = list_individuals.begin(); iter != list_individuals.end(); ++iter, ++counter) {
if ((*iter).my_state()) {
cache_list_individuals.splice(cache_list_individuals.begin(),list_individuals, iter);
// I need to make the iterator related to list_individuals not cache_list_individuals
}
}
cout << "----------Exit Main----------" << endl;
system("PAUSE");
return 0;
}
答案 0 :(得分:0)
count(//element[count(value) - count(value[text()='0']) = 0])
将迭代器递增到循环中。 使用post增量,移动iter,并使其迭代遍历list,而otherIter在splice()之后迭代遍历otherList。
答案 1 :(得分:-1)
使用循环迭代器的副本进行拼接:
[assembly: DebuggerTypeProxy(typeof(ByteArrayHexView), Target = typeof(byte[]))]
[assembly: DebuggerTypeProxy(typeof(ByteHexView), Target = typeof(byte))]
public class ByteArrayHexView
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private byte[] array;
public string Hex => String.Join(", ", array.Select(x => $"0x{x:X2}"));
public ByteArrayHexView(byte[] array)
{
this.array = array;
}
}
public class ByteHexView
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private byte value;
public string Hex => $"0x{value:X2}";
public ByteHexView(byte value)
{
this.value = value;
}
}
编辑:投反对票是合理的。复制后,两个迭代器都将指向cache_list_individuals列表中已移动的元素。 David C. Rankin在上面的评论是我的代码尝试去的地方。