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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:19:37+00:00 2026-05-31T08:19:37+00:00

The problem I encountered and am unable to solve goes something like this. I

  • 0

The problem I encountered and am unable to solve goes something like this. I have two classes:

class1
{
private:
  int identifier;
  double value;
public:
  setters,getters,etc...
}
class2
{
private:
  vector<class1> objects;
  vector<int> some_value;
  vector<double> other_value;
...
}

The problem is I need to search through the vector of objects in an object of the second class by its identifier in the class1 object(from a member function of class2). I tried something like:

int getObj(const int &ident, double &returnedValue, double &returnedOther_value)
{
  int p;
  p = find(objects.begin()->getIdentifier(),objects.end()->getIdentifier(),ident);
  ..

.. and then i was hoping to find a way to return from the found iterator values of corresponding(non-const) member variables value and other_value from both classes, but the code so far does not compile, because I’m likely doing the search all wrong. Is there a way I could do this with the find(or any other algorithm) or should I stick to my previous working realization with no algorithms?

  • 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-31T08:19:38+00:00Added an answer on May 31, 2026 at 8:19 am

    You need to use find_if with a custom predicate. Something like:

    class HasIdentifier:public unary_function<class1, bool>  
    {  
    public:
        HasIdentifier(int id) : m_id(id) { }  
        bool operator()(const class1& c)const  
        {  
            return (c.getIdentifier() == m_id);  
        }  
    private:
        int m_id;  
    };  
    
    
    // Then, to find it:
    vector<class1>::iterator itElem = find_if(objects.begin(), objects.end(), HasIdentifier(ident));  
    

    I haven’t tested it, so maybe it needs some tweaking.

    If you have C11, I guess you can use lambdas, but I don’t have it, so I haven’t had the chance to learn them.

    UPDATE:
    I’ve added an example in http://ideone.com/D1DWU

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've encountered this apparently common problem and have been unable to resolve it. If
When trying to solve this problem , I encountered some articles, etc. referring to
I encountered this problem Unable to Copy File *\bin\debug\*.dll to bin\*.dll And when this
I have encountered a problem that I have not come accross yet when setting
I've encountered this problem (while trying to add SQL Server Database (.mdf) file to
I'm playing around with Microsoft's CodeContracts and encountered a problem I was unable to
here's the problem: when requesting url like - http://server/path/to/file.html?param=../../something/something i get response: 500 Internal
I have encountered some unforeseen consequences of porting to mono 2.8.1. Problem can be
I have a small question about a problem I encountered writing my first python
I am developing a web app and have encountered a problem. I need to

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.