如何使用迭代器和find/find_f在c++的类向量中查找实值

How to use iterators and find/find_f to find a real value in a c++ vector of classes

本文关键字:find 向量 c++ 查找 迭代器 何使用      更新时间:2023-10-16

我在试图让类关联正确工作时遇到了一点麻烦。

我有一个类对象的向量,名为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上有很棒的书单。