首页 > 解决方案 > 如何从项目向量中按名称成员删除项目?

问题描述

我想Item通过使用和迭代器搜索项目的名称来删除一个find但不工作?我尝试了许多不同的方法,但我的控制台只是显示了很多错误。

这是我的代码:

struct Item {
  struct Item {
  string name;
  float price;
  unsigned int quantity;
};

Item i1 = {"Item 2", 0.05, 3};
Item i2 = {"Item 1", 0.10, 1};
Item i3 = {"Item 3", 0.25, 2};
  
vector <Item> inventory;
// Remove an Item, by name, from the inventory
// Return true if the Item is present and has been removed, false otherwise
bool Inventory::removeItem(string name) {
  if(hasItem(name)) {
      inventory.erase(remove(inventory.begin(), inventory.end(), name), inventory.end());
      return true;
  } else {  
     return false;
  }
}

//This function checks if hasItem
bool Inventory::hasItem(string name) {
  for (Item i : inventory) {
        if (i.name == name) {
            return true; 
        }
    }
    return false;
}

我不知道我是否没有得到正确的值,或者我需要找到值(名称)的索引。

以下是错误:

In file included from inventory.cpp:1:
In file included from ./inventory.h:7:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/algorithm:61:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_algobase.h:71:
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/predefined_ops.h:241:17: error: invalid operands to binary expression
      ('Item' and 'const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >')
        { return *__it == _M_value; }
                 ~~~~~ ^  ~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_algo.h:869:7: note: in instantiation of function template
      specialization '__gnu_cxx::__ops::_Iter_equals_val<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>
      > >::operator()<__gnu_cxx::__normal_iterator<Item *, std::vector<Item, std::allocator<Item> > > >' requested here
        if (!__pred(__first))
             ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_algo.h:906:19: note: in instantiation of function template
      specialization 'std::__remove_if<__gnu_cxx::__normal_iterator<Item *, std::vector<Item, std::allocator<Item> > >,
      __gnu_cxx::__ops::_Iter_equals_val<const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >' requested
      here
      return std::__remove_if(__first, __last,
                  ^
inventory.cpp:56:23: note: in instantiation of function template specialization 'std::remove<__gnu_cxx::__normal_iterator<Item *,
      std::vector<Item, std::allocator<Item> > >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >'
      requested here
      inventory.erase(remove(inventory.begin(), inventory.end(), name), inventory.end());
                      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/system_error:292:3: note: candidate function not viable: no known conversion
      from 'Item' to 'const std::error_code' for 1st argument
  operator==(const error_code& __lhs, const error_code& __rhs) noexcept
  ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/system_error:297:3: note: candidate function not viable: no known conversion
      from 'Item' to 'const std::error_code' for 1st argument
  operator==(const error_code& __lhs, const error_condition& __rhs) noexcept

标签: c++vectorstructfind

解决方案


您需要std::remove_if一个仅匹配名称的函数:

  auto newEnd = std::remove_if(inventory.begin(), 
                               inventory.end(), 
                               [&name](const auto& item){return item.name == name;});
  inventory.erase(newEnd, inventory.end());

我移到remove_if单独的行以使其更具可读性。


推荐阅读