从std :: vector中删除项目 [英] Removing an item from an std:: vector

查看:187
本文介绍了从std :: vector中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的第一个代码片段中,我试图从一个成员函数中的向量中删除一个元素,基于一个静态条件函数馈入std :: remove函数。然后,我得到大量的模板错误显示在第二个片段。


$ b

SNIPPET 1(CODE)

  void removeVipAddress(std :: string& uuid)
{
struct RemoveCond
{
static bool condition(const VipAddressEntity& o)
{
return o.getUUID()== uuid;
}
};

std :: vector< VipAddressEntity> :: iterator last =
std :: remove(
mVipAddressList.begin(),
mVipAddressList.end b $ b RemoveCond :: condition);

mVipAddressList.erase(last,mVipAddressList.end());

}

SNIPPET 2 (COMPILATION OUTPUT )

  /usr/include/c++/4.7/bits/random.h:4845:5:note:template< class _IntType> ; bool std :: operator ==(const std :: discrete_distribution< _IntType>& const std :: discrete_distribution< _IntType>&)
/usr/include/c++/4.7/bits/random.h:4845 :5:注意:模板参数扣除/替换失败:
在/usr/include/c++/4.7/algorithm:63:0中包含的文件中,
来自Entity.hpp:12:
/usr/include/c++/4.7/bits/stl_algo.h:174:4:注意:'ECLBCP :: VipAddressEntity'不是从'const std :: discrete_distribution< _IntType>'
派生的在从/ usr / include / c ++ / 4.7 / random:50:0,
来自/usr/include/c++/4.7/bits/stl_algo.h:67,
来自/usr/include/c++/4.7/算法:63,
来自Entity.hpp:12:
/usr/include/c++/4.7/bits/random.h:4613:5:note:template< class _RealType> bool std :: operator ==(const std :: extreme_value_distribution< _RealType>& const std :: extreme_value_distribution< _RealType>&)
/usr/include/c++/4.7/bits/random.h:4613 :5:注意:模板参数扣除/替换失败:
在/usr/include/c++/4.7/algorithm:63:0中包含的文件中,
来自Entity.hpp:12:
/usr/include/c++/4.7/bits/stl_algo.h:174:4:注意:'ECLBCP :: VipAddressEntity'不是从'const std :: extreme_value_distribution< _RealType>'派生的
std :: remove_if() / code>,而非 std :: remove()



std :: remove_if()接受一个谓词作为第三个参数,并删除满足该谓词的元素。



std: :remove()将值作为第三个参数,并删除等于该值的元素。





要使这项工作,您还必须将您的 RemoveCond 定义变成谓词对象,因为它需要州。像这样:

  void removeVipAddress(std :: string& uuid)
{
struct RemoveCond: public std :: unary_function< VipAddressEntity,bool>
{
std :: string uuid;

RemoveCond(const std :: string& uuid):uuid(uuid){}

bool operator()(const VipAddressEntity& o)
{
return o.getUUID()== uuid;
}
};

std :: vector< VipAddressEntity> :: iterator last =
std :: remove(
mVipAddressList.begin(),
mVipAddressList.end(),
RemoveCond(uuid));

mVipAddressList.erase(last,mVipAddressList.end());

}


In the 1st code snippet below, I am trying to remove an element from a vector within a member function based on a static condition function fed into std::remove function. Then I am getting lots of template errors shown in the 2nd snippet. Can you please tell me what I am missing?

SNIPPET 1 (CODE)

void removeVipAddress(std::string &uuid)
{
          struct RemoveCond
          {
            static bool condition(const VipAddressEntity & o)
            {
              return o.getUUID() == uuid;
            }
          };

          std::vector<VipAddressEntity>::iterator last =
            std::remove(
                    mVipAddressList.begin(),
                    mVipAddressList.end(),
                    RemoveCond::condition);

          mVipAddressList.erase(last, mVipAddressList.end());

}

SNIPPET 2 (COMPILATION OUTPUT)

 /usr/include/c++/4.7/bits/random.h:4845:5: note: template<class _IntType> bool      std::operator==(const std::discrete_distribution<_IntType>&, const   std::discrete_distribution<_IntType>&)
 /usr/include/c++/4.7/bits/random.h:4845:5: note:   template argument deduction/substitution failed:
 In file included from /usr/include/c++/4.7/algorithm:63:0,
             from Entity.hpp:12:
 /usr/include/c++/4.7/bits/stl_algo.h:174:4: note:   ‘ECLBCP::VipAddressEntity’ is not  derived from ‘const std::discrete_distribution<_IntType>’
 In file included from /usr/include/c++/4.7/random:50:0,
               from /usr/include/c++/4.7/bits/stl_algo.h:67,
               from /usr/include/c++/4.7/algorithm:63,
               from Entity.hpp:12:
 /usr/include/c++/4.7/bits/random.h:4613:5: note: template<class _RealType> bool  std::operator==(const std::extreme_value_distribution<_RealType>&, const  std::extreme_value_distribution<_RealType>&)
 /usr/include/c++/4.7/bits/random.h:4613:5: note:   template argument deduction/substitution failed:
 In file included from /usr/include/c++/4.7/algorithm:63:0,
             from Entity.hpp:12:
 /usr/include/c++/4.7/bits/stl_algo.h:174:4: note:   ‘ECLBCP::VipAddressEntity’ is not  derived from ‘const std::extreme_value_distribution<_RealType>’

解决方案

I guess you're looking for std::remove_if(), not std::remove().

std::remove_if() accepts a predicate as it third argument, and removes elements satisfying that predicate.

std::remove() takes a value as the third argument, and removes elements equal to the value.

EDIT

To make this work, you will also have to turn your RemoveCond definition into a predicate object, because it needs state. Like this:

void removeVipAddress(std::string &uuid)
{
      struct RemoveCond : public std::unary_function<VipAddressEntity, bool>
      {
        std::string uuid;

        RemoveCond(const std::string &uuid) : uuid(uuid) {}

        bool operator() (const VipAddressEntity & o)
        {
          return o.getUUID() == uuid;
        }
      };

      std::vector<VipAddressEntity>::iterator last =
        std::remove(
                mVipAddressList.begin(),
                mVipAddressList.end(),
                RemoveCond(uuid));

      mVipAddressList.erase(last, mVipAddressList.end());

}

这篇关于从std :: vector中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆