2014-04-15 42 views
0

我正在從矢量中刪除元素,我似乎無法讓我的remove_if語句正常工作。我有一個向量充滿了我的結構,當我循環遍歷向量時,我刪除了符合條件的任何元素。remove_if不工作,不知道什麼問題

我的矢量:

vector<vertex*> nodes { &s, &A, &G, &D, &B, &H, &E, &C, &I, &F, &t }; 

和的remove_if聲明:

nodes.erase(remove_if(nodes.begin(), nodes.end(), shouldDelete(i)), 
       nodes.end()); 

這是決定我是否應該刪除:

bool shouldDelete(vertex i) { 
return (i.incomingEdges == 0); 
} 

通常我可以工作,我的方式,通過一問題然而我得到的錯誤是非常奇怪的:

構建文件:../src/Main.cpp 調用:GCC C++編譯器 g ++ -O0 -g3 -Wall -c -fmessage-length = 0 -std = C++ 0x -MMD -MP -MF「 src/Main.d「-MT」src/Main.d「-o

/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1/algorithm:2117:18:error:called對象類型 'INT' 不是一個函數或函數指針

 if (!__pred(*__i)) 
      ^~~~~~ 

../src/Main.cpp:95:16:注意:在功能模板特殊化「的std :: __ 1周::的remove_if,實例化布爾>'這裏請求 nodes.erase(remove_if(nodes.begin(),nodes.end(),shouldDelete(i) ), ^

/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1/algorithm:859:13:錯誤:稱爲對象類型 'INT' 不是一個函數或函數指針

if (__pred(*__first)) 
     ^~~~~~ 

/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1/algorithm:2110:22:注:在函數模板專業化的實例化「的std :: __ 1個:: find_if,布爾&>'here here __first = _VSTD :: find_if < _ForwardIterator,typename add_lvalue_reference < _Predicate> :: type> ^

../src/Main.cpp:95:16:注意:在函數模板專門化'std :: __ 1 :: remove_if,bool>'的實例化中,這裏請求 nodes.erase(remove_if(nodes.begin() ,nodes.end(),shouldDelete(I))

這裏是代碼的其餘部分,遺憾的長度:

#include <iostream> 
#include <algorithm> 
#include <vector> 
#include <queue> 
#include <iterator> 

using namespace std; 

struct vertex { 
char vertexName; 
int incomingEdges; 
vertex* nextDoorTop = nullptr; 
vertex* nextDoorMiddle = nullptr; 
vertex* nextDoorBottom = nullptr; 
bool markToDelete = false; 
}; 
void queueNodes(vector<vertex*>& nodes, queue<vertex*>& q); 
bool shouldDelete(vertex i); 

int main() { 
vertex s, A, G, D, B, H, E, C, I, F, t; 
s.vertexName = 's'; 
s.incomingEdges = 0; 
s.nextDoorTop = &A; 
s.nextDoorMiddle = &D; 
s.nextDoorBottom = &G; 
A.vertexName = 'A'; 
A.incomingEdges = 2; 
A.nextDoorTop = &B; 
A.nextDoorMiddle = &E; 
G.vertexName = 'G'; 
G.incomingEdges = 1; 
G.nextDoorTop = &D; 
G.nextDoorMiddle = &E; 
G.nextDoorBottom = &H; 
D.vertexName = 'D'; 
D.incomingEdges = 2; 
D.nextDoorMiddle = &E; 
B.vertexName = 'B'; 
B.incomingEdges = 1; 
B.nextDoorTop = &C; 
H.vertexName = 'H'; 
H.incomingEdges = 1; 
H.nextDoorTop = &E; 
H.nextDoorMiddle = &I; 
E.vertexName = 'E'; 
E.incomingEdges = 4; 
E.nextDoorTop = &C; 
E.nextDoorMiddle = &F; 
E.nextDoorBottom = &I; 
C.vertexName = 'C'; 
C.incomingEdges = 3; 
C.nextDoorMiddle = &t; 
I.vertexName = 'I'; 
I.incomingEdges = 2; 
I.nextDoorTop = &F; 
I.nextDoorMiddle = &t; 
F.vertexName = 'F'; 
F.incomingEdges = 2; 
F.nextDoorMiddle = &t; 
t.vertexName = 't'; 
t.incomingEdges = 3; 

vector<vertex*> nodes { &s, &A, &G, &D, &B, &H, &E, &C, &I, &F, &t }; 
queue<vertex*> q; 
cout << "Vertex Name: " << " Number Of Edges: " << endl; 
for (const auto& n : nodes) { 
    const auto& i = *n; 
    cout << i.vertexName << "   " << i.incomingEdges 
      << "     " << endl; 
} 
int counter = 0; 
while (counter < 5) { 
    queueNodes(nodes, q); 
    counter++; 
} 
return 0; 
} 
bool shouldDelete(vertex i) { 
return (i.incomingEdges == 0); 
} 
void queueNodes(vector<vertex*>& nodes, queue<vertex*>& q) { 
for (auto n : nodes) { 
    auto& i = *n; 
    cout << endl << i.vertexName << "    " << i.incomingEdges; 
    if (i.incomingEdges == 0) { 
     if (i.nextDoorTop) 
      i.nextDoorTop->incomingEdges--; 
     if (i.nextDoorMiddle) 
      i.nextDoorMiddle->incomingEdges--; 
     if (i.nextDoorBottom) 
      i.nextDoorBottom->incomingEdges--; 
     cout << " foo"; 
     q.push(&i); 
     nodes.erase(remove_if(nodes.begin(), nodes.end(), shouldDelete(i)), 
       nodes.end()); 
    } else { 
     cout << " bar"; 
    } 
    cout << "Queue Size: " << q.size(); 
} 
} 

回答

2

更改shouldDelete功能:

bool shouldDelete(const vertex *i) { 
    return (i == nullptr || i->incomingEdges == 0); 
} 

而且調用該函數如下:

nodes.erase(remove_if(nodes.begin(), nodes.end(), shouldDelete), nodes.end()); 
+0

爲什麼你需要i == nullptr檢查? –

+0

確保不要取消引用空指針。 OP可以決定刪除它,但爲了安全起見,進行該檢查可能是明智的。他也可以將它改爲'i!= nullptr',它只會刪除長度爲0的傳入邊 – smac89

相關問題