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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:58:00+00:00 2026-06-16T21:58:00+00:00

I am attempting to optimize a std::vector search – index based iterating through a

  • 0

I am attempting to optimize a std::vector “search ” – index based iterating through a vector and returning and element that matches a “search” criteria

struct myObj {
   int id;
   char* value;
};

std::vector<myObj> myObjList;

create a few thousand entries with unique id‘s and values and push them to the vector myObjList.

What is the most efficient way to retrieve myObj that matches the id.
Currently I am index iterating like:

for(int i = 0; i < myObjList.size(); i++){
   if(myObjList.at(i).id == searchCriteria){
    return myObjList.at(i);
   }
}

Note: searchCriteria = int. All the elements have unique id‘s.
The above does the job, but probably not the most efficient way.

  • 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-16T21:58:02+00:00Added an answer on June 16, 2026 at 9:58 pm

    The C++ standard library has some abstract algorithms, which give C++ a kind of functional flavour, as I call it, which lets you concentrate more on the criteria of your search than on how you implement the search itself. This applies to a lot of other algorithms.

    The algorithm you are looking for is std::find_if, a simple linear search through an iterator range.

    In C++11, you can use a lambda to express your criteria:

    std::find_if(myObjList.begin(), myObjList.end(), [&](const myObj & o) {
        return o.id == searchCriteria;
    });
    

    When not having C++11 available, you have to provide a predicate (function object (=functor) or function pointer) which returns true if the provided instance is the one you are looking for. Functors have the advantage that they can be parameterized, in your case you want to parameterize the functor with the ID you are looking for.

    template<class TargetClass>
    class HasId {
        int _id;
    public:
        HasId(int id) : _id(id) {}
        bool operator()(const TargetClass & o) const {
            return o.id == _id;
        }
    }
    
    std::find_if(myObjList.begin(), myObjList.end(), HasId<myObj>(searchCriteria));
    

    This method returns an iterator pointing to the first element found which matches your criteria. If there is no such element, the end iterator is returned (which points past the end of the vector, not to the last element). So your function could look like this:

    vector<myObj>::iterator it = std::find_if(...);
    
    if(it == myObjList.end())
        // handle error in any way
    else
        return *it;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a search query that I'm inheriting and attempting to optimize. I am
So I'm attempting to optimize a product image carousel which cycles through items as
Attempting to apply .hover to a class that shows a div based on a
I am attempting to optimize a process for creating a SQL report based upon
In an effort to optimize the binary of a Qt-based dynamic library, I'm attempting
Attempting to make a NSObject called 'Person' that will hold the login details for
Attempting to move an uploaded file so that it is saved in the directory,
I'm attempting to optimise a simple cython routine I've written that accepts a 1D
Attempting to build an interface and generics based graph and getting an odd error
I am attempting to override TextAppearance.Medium in a custom theme that is applied 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.