删除类型结构的元素表单向量

时间:2016-02-29 10:20:47

标签: 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; 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;
}

但是在编译时我得到了愚蠢的错误:

 c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(1241) : error C2197: 'bool (__cdecl *)(void)' : too many arguments for call
1>        c:\program files (x86)\microsoft visual studio 9.0\vc\include\algorithm(4951) : see reference to function template instantiation '_OutIt std::_Remove_copy_if<std::_Vector_iterator<_Ty,_Alloc>,_OutIt,bool(__cdecl *)(void)>(_InIt,_InIt,_OutIt,_Pr,std::_Range_checked_iterator_tag)' being compiled
1>        with
1>        [
1>            _OutIt=std::_Vector_iterator<list,std::allocator<list>>,
1>            _Ty=list,
1>            _Alloc=std::allocator<list>,
1>            _InIt=std::_Vector_iterator<list,std::allocator<list>>,
1>            _Pr=bool (__cdecl *)(void)
1>        ]
1>        c:\program files (x86)\microsoft visual studio    9.0\vc\include\algorithm(1311) : see reference to function template instantiation '_OutIt stdext::unchecked_remove_copy_if<std::_Vector_iterator<_Ty,_Alloc>,_FwdIt,bool(__cdecl *)(void)>(_InIt,_InIt,_OutIt,_Pr)' being compiled
1>        with
1>        [
1>            _OutIt=std::_Vector_iterator<list,std::allocator<list>>,
1>            _Ty=list,
1>            _Alloc=std::allocator<list>,
1>            _FwdIt=std::_Vector_iterator<list,std::allocator<list>>,
1>            _InIt=std::_Vector_iterator<list,std::allocator<list>>,
1>            _Pr=bool (__cdecl *)(void)
1>        ]

我无法理解这个错误是什么以及如何解决? 有人可以帮我解决这个错误吗?

2 个答案:

答案 0 :(得分:0)

std::remove_if期望谓词函数的签名应该等同于

bool pred(const Type &a);

bool test()不接受任何参数,但它不匹配。你可能想要

bool test(const list& v)
{
    // judgement process based on the individual element passed
    // ...
}

答案 1 :(得分:0)

我对作者想要做的事感到非常困惑。如果你想要一个程序来添加或删除一些数字,你可能会删除很多行;

#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;
}
相关问题