c++ - 从 std::vector 中移除一个项目

标签 c++ stdvector

在下面的第一个代码片段中,我试图根据馈入 std::remove 函数的静态条件函数从成员函数内的 vector 中删除一个元素。然后我在第二个片段中看到了很多模板错误。你能告诉我我错过了什么吗?

片段 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());

}

片段 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());

}

关于c++ - 从 std::vector 中移除一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15025083/

相关文章:

c++ - 基于字符串的派生类选择 - C++

C++ - 使用 std::vector 和相关内存管理的正确方法

c++ - 我如何从具有 X 元素的 std::vector 中的 p 中提取 n 个元素

c++ - 试图从指针 vector 中取消引用指针的段错误

c++ - 使用 std::vector 成员的 std::vector 为类实现 move 语义的正确方法

c++ - STL 迭代器和 'const'

c++ - QT5.7 - 为什么我使用 QString 得到格式错误的 json 值,但使用 std::string 却完美无缺?

c++ - GetPrivateProfileInt 方法的参数

c++ - 如何查看用户登录Windows的时间?

C++ 将 pair<int,int> 的 vector 复制到 vector<int>