Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6933069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:47:05+00:00 2026-05-27T11:47:05+00:00

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

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:47:06+00:00Added an answer on May 27, 2026 at 11:47 am

    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));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.