2016-11-08 54 views
-1

我不明白为什么二线>父显示我2 载体包含的所有时间Find_if奇怪值

{{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得到一个错误的对的,但它锯我所有的时间二阶>父= 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

您应该阅读[如何创建最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。如果您提供可自行运行的示例,则会更好。没有这个很难完全理解你的问题。 –

回答

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循环的第一次迭代是真实的。

+0

换句话说,他可能是想写'std :: find_if(first-1,test.end(),pred)' –