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

  • Home
  • SEARCH
  • 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 174883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:36:06+00:00 2026-05-11T13:36:06+00:00

I have a class Class which has a member std::list, I want to search

  • 0

I have a class ‘Class’ which has a member std::list, I want to search that list/tree for an item, specifically an item with a specific name. A basic representation of my class is as follows:

#include <list> #include <string> class Class {     std::string _name;     std::list<Class*> _members; public:     Class(const std::string& name) : _name(name) {}     void addMember(Class* member) { _members.push_back(member); }     const std::string& name() const { return _name; }     const std::list members() const { return _members; }     Class* findItem(const std::string& name) const { ... } }; 

I could just do something like this in Class::findItem:

Class* Class::findItem(const std::string& n) const {     std::list<Class>::const_iteratior i;     for(i=_members.begin(); i!=_members.end(); ++i)         if((*i)->name() == n) return *i;     for(i=_members.begin(); i!=_members.end(); ++i) {       Class* cls = (*i)->findItem(n);       if(cls) return cls;     }     return 0; } 

But, what I want to happen is for findItem() to return the ‘closest’ item to the one searched from. For instance, if this is my tree, with each letter representing one level in the list hierarchy, and each number representing the items ‘value’. I want findItem(3) to return B(3), rather than C(3).

                         A(1)                         |         ----------------------------------         B(2)                           B(3)          |                                | ---------------------                   ----- C(3)             C(4)                    C(4)                  |                         |             ----------------           ----------             D(5)  D(6)  D(7)           D(5)  D(6) 
  • 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. 2026-05-11T13:36:07+00:00Added an answer on May 11, 2026 at 1:36 pm

    Use a breadth-first search. As you access a node that isn’t equal to the query value, you push that node onto the back of the queue. Process nodes from the front of the queue first. The first match you find will be the one closest to the root.

    Class* Class::findItem(const std::string& n) const {     std::list<Class>::const_iteratior i;     std::queue<Class*> q;     q.push(this);     while (!q.empty()) {         Class *c = q.front(); q.pop();         if (c->name() == n) return c;         for(i=c->_members.begin(); i!=c->_members.end(); ++i)             q.push(i);     }     return NULL; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 235k
  • Answers 236k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer 24 bit + alpha is actually 32 bits per pixel.… May 13, 2026 at 6:17 am
  • Editorial Team
    Editorial Team added an answer Obviously there's a danger in totally replacing the class definition… May 13, 2026 at 6:17 am
  • Editorial Team
    Editorial Team added an answer It's not the same as using the break keyword with… May 13, 2026 at 6:17 am

Related Questions

I have a class named Particle which has a std::set as a member. The
It's a design question. I have a business object, and 5 business object types
Ive got a problem that if I have a template class, which in turn
I have a pointer to an int. int index = 3; int * index_ptr

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.