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 9210529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:06:05+00:00 2026-06-18T01:06:05+00:00

specifically I have a list of objects of a class with multiple string object

  • 0

specifically I have a list of objects of a class with multiple string object data members(NID, customerNumber, studentNumber, fName, lName).

I want to reuse the following code to search for the node that matches the search key whether the data member that is looked for is NID or any other of the class’s string data members.

nodePtr = firstPtr;
for(; nodePtr != NULL && nodePtr->str != str; nodePtr = nodePtr->nextPtr);

if(nodePtr != NULL)
    //the nodePtr points to the node that matches the search key
else
    //no node matched the search key

if it was PHP code I could use the value of a variable as the name of another:

$node->${$var}

but in C++ is there anyway to reuse the code?

  • 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-06-18T01:06:06+00:00Added an answer on June 18, 2026 at 1:06 am

    The most flexible way to do this is to provide a predicate as a template parameter:

    template <typename Pred>
    Node * find_if(Node * node, Pred pred) {
        for (; node && !pred(node); node = node->next);
        return node;
    }
    

    In C++11, you can call it with a lambda:

    if (Node * node = find_if(first, [&](Node * n){return n->NID == nid;})) {
        // node points to the matching node
    } else {
        // not found
    }
    

    or, if you’re stuck in ages past, a function object:

    struct CompareNID {
        CompareNID(std::string nid) : nid(nid) {}
        bool operator() {Node * n) {return n->NID == nid;}
    
        std::string nid;
    };
    
    Node * node = find_if(first, CompareNID(nid));
    

    or, since all your fields are strings, you could sacrifice flexibility for tersity using member pointers, giving something similar to your PHP example:

    Node * find(Node * node, std::string Node::*member, std::string const & value) {
        for (; node && node->*member != value; node = node->next);
        return node;
    }
    
    Node * node = find(first, &Node::NID, nid);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this object Object1 defined as public Object1(String item, List<Object2> obj2) in a
We have a class Document which uses a private member of type Map<String, Object>
I have a class which inherits from a collection, specifically List<> and I've listed
I have a custom list in SharePoint (specifically, MOSS 2007.) One field is a
I have a List of Objects (roughly 100k) that is must iterate upon in
Suppose we have an object which represent a box. class Box { public: int
So i have a long list of objects that each have a slot that
I have the following code: public class Alert { public string Alias { get;
I have a list of 'request' objects, each of which has fairly normal activerecord
I have a List of objects and I'd like to loop over that list

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.