C++ 删除类型为structure的元素形式向量
我正在尝试这段代码,我想从类型结构的向量中删除一个元素C++ 删除类型为structure的元素形式向量,c++,vector,structure,C++,Vector,Structure,我正在尝试这段代码,我想从类型结构的向量中删除一个元素 #include <iostream> #include <vector> #include <algorithm> using namespace std; typedef struct _list { int x; }list; std::vector<list> v; list li[5]; void PushElements() { for(int i = 0;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct _list
{
int x;
}list;
std::vector<list> v;
list li[5];
void PushElements()
{
for(int i = 0; i < 5; i++)
{
li[i].x = i+2;
v.push_back(li[i]);
}
}
void display()
{
for(int i = 0; i < v.size(); i++)
{
cout << v[i].x << endl;
}
}
bool test()
{
int flag = false;
for(int i = 0; i < 5; i++)
{
if(li[i].x < 3)
flag = true;
}
return flag;
}
void DeleteElement()
{
v.erase(remove_if(v.begin(), v.end(), test), v.end());
}
int main()
{
PushElements();
display();
DeleteElement();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
类型定义结构列表
{
int x;
}名单;
std::向量v;
列表李[5];
无效元素()
{
对于(int i=0;i<5;i++)
{
li[i].x=i+2;
v、 推回(li[i]);
}
}
无效显示()
{
对于(int i=0;i [
1> _OutIt=std::_Vector_迭代器,
1> _Ty=列表,
1> _Alloc=std::分配器,
1> _InIt=std::_Vector_迭代器,
1> Pr=bool(cdecl*)(无效)
1> ]
1> c:\program files(x86)\microsoft visual studio 9.0\vc\include\algorithm(1311):如果正在编译(\u InIt,\u InIt,\u OutIt,\u Pr),请参阅函数模板实例化“\u OutIt stdext::unchecked\u remove\u copy”的参考
1> 与
1> [
1> _OutIt=std::_Vector_迭代器,
1> _Ty=列表,
1> _Alloc=std::分配器,
1> _FwdIt=std::_Vector_迭代器,
1> _InIt=std::_Vector_迭代器,
1> Pr=bool(cdecl*)(无效)
1> ]
我无法理解这个错误是什么以及如何解决?
有人能帮我解决这个错误吗???希望谓词函数的签名应该与
bool pred(const Type &a);
但是bool test()
不接受任何参数,它不匹配。您可能需要
bool test(const list& v)
{
// judgement process based on the individual element passed
// ...
}
我很困惑作者想做什么。如果你想在一个程序中添加或删除一些数字,你可以删除很多行
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct _list
{
int x;
_list(int x)
{
this->x=x;
}
}list;
std::vector<list> v;
void PushElements()
{
for(int i = 0; i < 5; i++)
{
v.push_back(list(i+2));//the number you want to add
}
}
void display()
{
for(int i = 0; i < v.size(); i++)
{
cout << v[i].x << "\t";
}
cout<<endl;
}
bool test(list &a)
{
return a.x>3;
}
void DeleteElement()
{
v.erase(remove_if(v.begin(), v.end(), test), v.end());
}
int main()
{
PushElements();
display();
DeleteElement();
display();
return 0;
}
#包括
#包括
#包括
使用名称空间std;
类型定义结构列表
{
int x;
_列表(INTX)
{
这个->x=x;
}
}名单;
std::向量v;
无效元素()
{
对于(int i=0;i<5;i++)
{
v、 向后推(列表(i+2));//要添加的数字
}
}
无效显示()
{
对于(int i=0;i 例如,不能阅读。请特别注意谓词(您传递的函数),以及它应该如何声明和如何使用。所有以下划线开头的标识符都保留在全局命名空间中作为名称使用(由实现)。不允许使用您的结构名(\u list
)。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct _list
{
int x;
_list(int x)
{
this->x=x;
}
}list;
std::vector<list> v;
void PushElements()
{
for(int i = 0; i < 5; i++)
{
v.push_back(list(i+2));//the number you want to add
}
}
void display()
{
for(int i = 0; i < v.size(); i++)
{
cout << v[i].x << "\t";
}
cout<<endl;
}
bool test(list &a)
{
return a.x>3;
}
void DeleteElement()
{
v.erase(remove_if(v.begin(), v.end(), test), v.end());
}
int main()
{
PushElements();
display();
DeleteElement();
display();
return 0;
}