Как использовать итераторы и find / find_f, чтобы найти реальное значение в векторе c ++ классов - PullRequest
0 голосов
/ 05 декабря 2011

У меня проблемы с попыткой заставить ассоциацию классов работать правильно.

У меня есть вектор объектов класса с именем Items. Каждый элемент имеет такие значения, как имя, цена и т. Д. Внутри класса Items есть сеттеры и геттеры для изменения значений и их возврата.

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

Ответы [ 2 ]

1 голос
/ 05 декабря 2011

В современном C ++ это довольно просто с лямбдой:

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

Нетрудно разложить лямбду как традиционный предикат, конечно:

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));
0 голосов
/ 05 декабря 2011

Что-то близкое к этому должно работать:

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

...