如何使用迭代器和find/find_f在c++的类向量中查找实值
How to use iterators and find/find_f to find a real value in a c++ vector of classes
我在试图让类关联正确工作时遇到了一点麻烦。
我有一个类对象的向量,名为Items。每个项目都有一个值,如名称、价格等。Items类中有setter和getter,用于修改值和返回值。
std::string choice; // users choice
ListOfOrders::iterator iter = orderList->end(); iter--;
// the last order inserted, ignore this this is used to get the last order so
//we can pass the items to it (the order class has a vector of pointers
//(items) that we are trying to pass to now.)
ListOfItems::iterator itemiter; // make the items iter
listItems(itemList); // function in the main that returns the list of items using the getters and a vector iterator.
while(choice != "x") // until the user quits
{
// here is my prob, ofc, i can just compare the users entered choice of item (name) to the iterator because thats just returning a pointer to the class object, what i need to do it call the getName() getter from each of the objects and comparer that
(*itemiter)->getName() = find (itemList->begin(), itemList->end(), choice);
if (itemiter == itemList->end())
{
std::cout << "sorry item not found please try again." << std::endl;
}
else
{
(*iter)->addItem(*itemiter); // pass the item object off to the order object's vector of items.
}
}
我知道这样的东西(见下文(没有编译,只是快速键入给你一个想法))可以使用,它会工作,但一定有一个更好的方法,对吗?
std::string choice; // users choice
cin >> choice;
ListOfOrders::iterator iter = orderList->end(); iter--; // the last order inserted
if(lookForItem(choice))
{
std::cout << "Yesn";
}
else
{
std::cout << "non";
}
bool lookForItem(std::string choice)
{
ListOfItems::iterator itemiter; // make the items iter
itemiter = itemList->begin();
while(itemiter != itemList->end())
{
if((*itemiter)->getName() == choice)
{
(*iter)->addItem(*itemiter);
}
iter++;
}
return false;
}
在现代c++中,使用lambda:
就相当简单了:auto it = std::find_if(itemList.begin(), itemList.end(),
[&choice](Item const & x) { return x.name == choice; } );
当然,用传统的谓词来拼写lambda并不难:
struct FindItemByName
{
FindItemByName(std::string const & s) : choice(s) { }
bool operator()(Item const & x) const { return x.name == choice; }
private:
std::string const & choice;
};
ListOfItems::iterator it
= std::find_if(itemList.begin(), itemList.end(), FindItemByName(choice));
类似的代码应该可以工作:
#include <vector>
#include <algorithm>
#include <functional>
struct Item {
std::string name;
// two items are equal, when their name is equal
bool operator==(const Item& x) { return name == x.name; }
};
struct Cmp_item_by_name {
bool operator()(const Item& x, const std::string& y) { return x == y; }
};
typedef std::vector<Item> Items;
Items items;
Items::const_iterator
findItem(const std::string& name) {
return std::find_if(items.begin(), items.end(), std::bind2nd(Cmp_item_by_name(), name));
}
使用已弃用的bind2nd
。不要用这个,只要你不懂就行。我们在SO上有很棒的书单。
相关文章:
- 写入向量<向量<bool>>
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 为什么在向量上使用 std::find() 时会出现错误?
- C++ std::find() 寻址返回向量的类函数时的意外行为
- 如何使用 std::find 从向量的第一个元素中查找字符串<字符串对<字符串、字符串> >类型?
- 向量元素数据损坏了Find()操作
- 使用 "find" 访问向量中的值
- 使用 std::find 在向量中定位随机生成的数字
- 在无序映射<字符串上使用 find(),向量<string>>与 C++ 中的有序映射花费相同的时间
- 为什么对排序向量进行的二进制搜索比STD :: SET FIND慢
- 自定义类对象的向量的std::find的返回值
- 如何在数组向量上使用 std::find
- 如何在地图向量中的地图上使用find()
- map.find和指向向量的指针的怪异行为
- 使用std::find()搜索用户定义结构的向量
- 使用std::find根据字符串向量检查字符串时出现的问题
- 如何使用迭代器和find/find_f在c++的类向量中查找实值
- 向量的std::find()出错
- 如何将std::find/std::find_if用于自定义类对象的向量
- 使用std::find搜索对的向量时忽略其中一个值