繁体   English   中英

从std :: vector中删除项目

[英]Removing an item from an std:: vector

在下面的第一个代码片段中,我试图基于提供给std :: remove函数的静态条件函数从成员函数中的向量中删除元素。 然后我收到第二个片段中显示的大量模板错误。 你能告诉我我错过了什么吗?

SNIPPET 1(代码)

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 (编译输出)

 /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>’

我猜你正在寻找std::remove_if() ,而不是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());

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM