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

发布于 2024-12-19 15:02:21 字数 1955 浏览 0 评论 0原文

我在尝试让班级协会正常工作时遇到了一些麻烦。

我有一个类对象向量,名为 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 << "Yes\n";
}
else
{
    std::cout << "no\n";
}

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;
}

I'm having a bit of trouble trying to get class association's to work correctly.

I have a vector of class objects, named Items. Each item has a values such as a name, price, etc. Inside the Items class there are setters and getters to change the values and to return them.

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.
    }
}

I know something like this(see below(haven't compiled it, just quickly typed it to give you a idea)) could be used and it would work, but there must be a better way right?

std::string choice; // users choice
cin >> choice;
ListOfOrders::iterator iter = orderList->end(); iter--; // the last order inserted
if(lookForItem(choice))
{
    std::cout << "Yes\n";
}
else
{
    std::cout << "no\n";
}

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

农村范ル 2024-12-26 15:02:21

在现代 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));

In modern C++, this is fairly simple with a lambda:

auto it = std::find_if(itemList.begin(), itemList.end(),
                       [&choice](Item const & x) { return x.name == choice; } );

It's not hard to spell out the lambda as a traditional predicate, of course:

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));
丢了幸福的猪 2024-12-26 15:02:21

与此接近的东西应该可以工作:

#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 的很棒的书单。

Something close to this should work:

#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));
}

This uses the deprecated bind2nd. Don't use this, as long as you don't understand it. We have great book lists on SO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文