Reputation: 93
I am working on deleting elements from a vector and I cannot seem to get my remove_if statement to work. I have a vector filled with my structs and while I am looping through the vector I delete any elements that match a criteria.
My vector:
vector<vertex*> nodes { &s, &A, &G, &D, &B, &H, &E, &C, &I, &F, &t };
and the remove_if statement:
nodes.erase(remove_if(nodes.begin(), nodes.end(), shouldDelete(i)),
nodes.end());
This is to decide if I should delete:
bool shouldDelete(vertex i) {
return (i.incomingEdges == 0);
}
Usually I can work my way through a problem however the error that I am getting is super strange:
Building file: ../src/Main.cpp Invoking: GCC C++ Compiler 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 object type 'int' is not a function or function pointer
if (!__pred(*__i))
^~~~~~
../src/Main.cpp:95:16: note: in instantiation of function template specialization 'std::__1::remove_if, bool>' requested here nodes.erase(remove_if(nodes.begin(), nodes.end(), shouldDelete(i)), ^
/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1/algorithm:859:13: error: called object type 'int' is not a function or function pointer
if (__pred(*__first))
^~~~~~
/Library/Developer/CommandLineTools/usr/bin/../lib/c++/v1/algorithm:2110:22: note: in instantiation of function template specialization 'std::__1::find_if, bool &>' requested here __first = _VSTD::find_if<_ForwardIterator, typename add_lvalue_reference<_Predicate>::type> ^
../src/Main.cpp:95:16: note: in instantiation of function template specialization 'std::__1::remove_if, bool>' requested here nodes.erase(remove_if(nodes.begin(), nodes.end(), shouldDelete(i))
Here is the rest of the code, sorry for the length:
#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();
}
}
Upvotes: 0
Views: 469
Reputation: 43264
Change your shouldDelete function to:
bool shouldDelete(const vertex *i) {
return (i == nullptr || i->incomingEdges == 0);
}
And call the function as so:
nodes.erase(remove_if(nodes.begin(), nodes.end(), shouldDelete), nodes.end());
Upvotes: 2