我正在努力从向量中删除元素,我似乎无法使我的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&#34; src / Main.d&#34; -MT&#34; SRC / Main.d&#34; -o
/ Library / Developer / CommandLineTools / usr / bin /../ lib / c ++ / v1 / algorithm:2117:18:错误:调用对象类型&#39; int&#39;不是函数或函数指针
if (!__pred(*__i))
^~~~~~
../ src / Main.cpp:95:16:注意:在实例化函数模板特化&#39; std :: __ 1 :: remove_if,bool&gt;&#39;这里要求 nodes.erase(remove_if(nodes.begin(),nodes.end(),shouldDelete(i)), ^
/ Library / Developer / CommandLineTools / usr / bin /../ lib / c ++ / v1 / algorithm:859:13:错误:调用对象类型&#39; int&#39;不是函数或函数指针
if (__pred(*__first))
^~~~~~
/ Library / Developer / CommandLineTools / usr / bin /../ lib / c ++ / v1 / algorithm:2110:22:注意:在实例化函数模板特化&#39; std :: __ 1 :: find_if,bool &安培;&GT;&#39;这里要求 __first = _VSTD :: find_if&lt; _ForwardIterator,typename add_lvalue_reference&lt; _Predicate&gt; :: type&gt; ^
../ src / Main.cpp:95:16:注意:在实例化函数模板特化&#39; std :: __ 1 :: remove_if,bool&gt;&#39;这里要求 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();
}
}
答案 0 :(得分:2)
将您的shouldDelete功能更改为:
bool shouldDelete(const vertex *i) {
return (i == nullptr || i->incomingEdges == 0);
}
并按原样调用该函数:
nodes.erase(remove_if(nodes.begin(), nodes.end(), shouldDelete), nodes.end());