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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:43:27+00:00 2026-05-24T06:43:27+00:00

Say I had a std::vector<std::pair<int, std::vector<int> > > , that is, a vector containing

  • 0

Say I had a std::vector<std::pair<int, std::vector<int> > >, that is, a vector containing pairs of ints to vectors. ( I know I could acheive the same thing with a map, but that’s not what I’m asking )

How would I search for the int (the one in the pair) using STL? I wrote a solution that works:

struct FindFirst {
    FindFirst(int i) : toFind(i) { }
    int toFind;
    bool operator() 
        ( const std::pair<int, std::vector<int> > &p ) {
            return p.first==toFind;
    }
};

That can be used like this:

int valueToFind = 4;
std::find_if(myVec.begin(), myVec.end(), FindFirst(valueToFind));

But it looks to me like this a little ugly; there must be a better way.
So are there better ways of doing this without using anything from C++0x or Boost?
(Because I’m trying to learn the best way of doing it with, and I stress, only STL)

EDIT:
Looks like there’s a little confusion on what I’m asking. What I’m more interested in is whether it’s bad practice to use the constructor for search values on a functor, especially when they are used in STL algo’s.

  • 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-24T06:43:28+00:00Added an answer on May 24, 2026 at 6:43 am

    This is the best way to do it, when using only the STL. What exactly do you not like about it (except maybe it’s verbosity)?

    It is versatile, because you can use with other algorithms like for instance std::binary_search.

    It is quite simple and direct (at least to someone used to C++ functors).

    The only thing to really improve here would be the choice of the data-structure. Such use-cases are covered by std::map, and you have not explained why you cannot use it. I understand that for your real problems a map really might not be suited, but in that case your example is not representative of your actual problem. You should fix that.

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

Sidebar

Related Questions

Let's say I had a program in C# that did something computationally expensive, like
Say I had a class SuperClass and two subclasses SubClassA and SubClassB that inherit
Let's say I had a char pointer pointing to a buffer that contained these
Let's say you had a web app where people could submit links, links for
If I had a STL container, say a list of pointers I could remove
Let's say I had a table full of records that I wanted to pull
Say you had an Excel file containing a column filled with words, and you'd
Let's say you had a /companies resource that allowed clients to lookup public companies
Let's say I had an app that was an address book. I'd like to
Say I had class/struct Foo struct Foo { int a, b; bool operator< (Foo

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.