如何使用迭代器和 find/find_f 在 c++ 中查找实际值类的向量
我在尝试让班级协会正常工作时遇到了一些麻烦。
我有一个类对象向量,名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在现代 C++ 中,这对于 lambda 来说相当简单:
当然,将 lambda 拼写为传统谓词并不难:
In modern C++, this is fairly simple with a lambda:
It's not hard to spell out the lambda as a traditional predicate, of course:
与此接近的东西应该可以工作:
这使用已弃用的
bind2nd
。只要您不理解它,就不要使用它。我们有关于 SO 的很棒的书单。Something close to this should work:
This uses the deprecated
bind2nd
. Don't use this, as long as you don't understand it. We have great book lists on SO.