我不明白为什么第二个>父母一直向我显示2。 矢量包含
{{2,4,0},{2,5,0},{2,6,0},{1,2,0},{8,7,0},{7,3,0},{10,9,0},{11,9,0},{3,9,0}, {1,3,0}, {0,1,11}}
在VS 2013上,我在Ideone它的工作中遇到了一个错误,但它一直看到我第二个> parent = 2.
struct tester {
int parent, number, child;
};
std::vector <tester> test;
test.push_back({ 0,1, 11 });
std::vector <tester>::iterator first, second;
for (first = test.begin() + 1, second = test.begin(); first != test.end(); ++first) {
if (first->parent <= second->parent) {
parent = first->parent;
auto pred = [parent](pracownik& item) {
return item.parent == parent;
};
second = std::find_if(first - 1, test.begin(), pred);
first->child = first - second;
if (first->child == 1) first->child = 0;
second = first;
}
}
程序应将矢量更改为:
{ {2,4,0},{2,5,0},{2,6,0},{1,2,3},{8,7,0},{7,3,1},{10,9,0},{11,9,0},{3,9,2}, {1,3,5}, {0,1,11}}
答案 0 :(得分:1)
std::find_if(InputIterator f, InputIterator l, UnaryPredicate pred)
搜索范围[f, l)
。在你的情况std::find_if(first - 1, test.begin(), pred);
中,它应该是first - 1 <= test.begin()
,但只有在for
循环的第一次迭代时才会出现。