无法从非原始数据类型的向量中删除ID

时间:2016-09-23 18:15:20

标签: c++ vector erase-remove-idiom

我有一个用户定义数据类型city的向量。我试图通过它的ID从这个向量中删除一个元素;最终所有城市都将在while(!cityList.empty())循环中从此列表中删除。我计划使用擦除删除成语来完成此任务。

然而,当我在调用remove()生成我的代码时,我收到了一条非常粗略的错误消息。将city对象作为第三个参数传递给remove()会导致此错误,并传递city的(int)ID。 erase()不会发生此错误,remove()也会发生此错误,如果我尝试使用此错误,也会发生find()。以下是有问题的代码:

vector<city> cityList;
cityList.push_back(city(1));
cityList.push_back(city(2));
cityList.push_back(city(3));

city cityToRemove = cityList[0];
int idOfCityToRemove = cityList[0].getID();

remove(cityList.begin(), cityList.end(), cityToRemove);
//remove(cityList.begin(), cityList.end(), idOfCityToRemove);

Here is an updated simple, minimal demo of my problem

错误消息包括“模板参数扣除/替换失败”“'city'不是来自'const _gnu_cxx :: __ normal_iterator&lt; _IteratorL,_Container&gt;'”消息,我无法在网上找到与上述错误有关的问题。

编辑:我修改了我的代码,现在我已经:

int main(int argc, char** argv) {

vector<city> cityList;
cityList.push_back(city(1));
cityList.push_back(city(2));
cityList.push_back(city(3));

city cityToRemove = cityList[0];
int idOfCityToRemove = cityList[0].getID();
int i;

for (i = 0; i < cityList.size(); i++) {
    cityList.erase(remove_if(cityList.begin(), cityList.end(),  cityList[i] == cityToRemove), cityList.end());
}
//remove(cityList.begin(), cityList.end(), cityToRemove);
//remove(cityList.begin(), cityList.end(), idOfCityToRemove);

return 0;
}

bool operator ==(const city &a, const city &b)
{
    return (a.id == b.id);
}

我尝试编译时收到的错误是:

In file included from /usr/include/c++/5/bits/stl_algobase.h:71:0,
             from /usr/include/c++/5/bits/char_traits.h:39,
             from /usr/include/c++/5/ios:40,
             from /usr/include/c++/5/ostream:38,
             from /usr/include/c++/5/iostream:39,
             from main.cpp:2:
/usr/include/c++/5/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_pred<_Predicate>::operator()(_Iterator) [with _Iterator = __gnu_cxx::__normal_iterator<city*, std::vector<city> >; _Predicate = bool]’:
/usr/include/c++/5/bits/stl_algo.h:866:20:   required from _ForwardIterator std::__remove_if(_ForwardIterator, _ForwardIterator, _Predicate) [with _ForwardIterator = __gnu_cxx::__normal_iterator<city*, std::vector<city> >; _Predicate = __gnu_cxx::__ops::_Iter_pred<bool>]’
/usr/include/c++/5/bits/stl_algo.h:936:30:   required from ‘_FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = __gnu_cxx::__normal_iterator<city*, std::vector<city> >; _Predicate = bool]’
main.cpp:30:90:   required from here
/usr/include/c++/5/bits/predefined_ops.h:234:30: error: expression cannot be used as a function
{ return bool(_M_pred(*__it)); }
                          ^

这更接近,但我不确定需要什么。行main.cpp:30:90指向cityList[i] == cityToRemove函数的cityList.erase()部分,但是,我知道问题在我的比较表达式中。

我的演示也已更新。

1 个答案:

答案 0 :(得分:0)

您需要定义operator ==

class city {
    public:
        city(int idin);
        int getID();

    private:
        int id;

    friend bool operator==(const city &a, const city &b);
};

bool operator ==(const city &a, const city &b)
{
    return a.id == b.id;
}

并在example here中调用erase