c++ - 如何从项目向量中按名称成员删除项目?
问题描述
我想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
解决方案
您需要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
单独的行以使其更具可读性。
推荐阅读
- css - 我想通过单击按钮来移动图像?
- powershell - 使用“ConvertFrom-Json”的 Powershell 输出和“运行和调试”终端输出损坏
- jax-rs - Jackson ContextResolver 已加载,但在 wildfly 18 中未调用 getContext 方法
- sql - 选择先进后出时间 - 不同的日期和空条件 - 从数据手指
- python-3.x - AttributeError:“RangeIndex”对象没有属性“inferred_freq”
- php - Laravel / Eloquent / Postgres:更新查询导致错误“无效参数号:未定义参数”
- ios - 按需资源 (ODR) 已完成/已下载标签的总文件大小
- flask - ModuleNotFoundError:Flask-sqlalchemy 中没有名为“MySQLdb”的模块
- sql - SQL 查询形成双对
- azure - Azure Synapse 加载:将大型压缩文件拆分为较小的压缩文件