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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:21:18+00:00 2026-06-15T21:21:18+00:00

I have a vector of objects and I want to return the range of

  • 0

I have a vector of objects and I want to return the range of elements whose attribute have a specific value. This is the structure:

class A {
public:
  std::vector<B*> vec_;
  pair<vector<B*>::iterator, vector<B*>::iterator> getElements(unsigned int attr_val);
  unsigned int name() { return name_; }
private:
  unsigned int name_;
};

class B {
public:
  unsigned int attr() { return attr_; }
  A* source() { return source_; }
  B* dest() { return dest_; }
private:
  A* source_;
  B* dest_;
  unsigned int attr_;
};

The vector vec_ is already sorted by attr_ and dest_->name() (in that order). Now I want to return all elements, whose attr_ is equal to attr_val.

What is the appropriate stl algorithm (or is there even a vector member function?) to implement getElements(unsigned int attr_val)?

  • 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-15T21:21:19+00:00Added an answer on June 15, 2026 at 9:21 pm

    You need a value to pass to equal_range, and the obvious thing to use is a pointer. The obvious way to get one is just to create an instance of B with the correct value of attr_, and write a comparator that involves only the value of attr(). I’m going to assume you already know how to do that since you managed to get the vector sorted 😉

    Provided there are no null pointers in the vector you could do this instead:

    struct FindAttr {
        unsigned int attr;
        FindAttr(unsigned int attr) : attr(attr) {}
        bool operator()(B *left, B *right) {
            unsigned int leftval = left ? left->attr() : attr;
            unsigned int rightval = right ? right->attr() : attr;
            return leftval < rightval;
        }
    };
    
    ...
    
    return equal_range(vec_.begin(), vec_.end(), nullptr, FindAttr(value));
    

    You could make that a lambda:

    return equal_range(vec_.begin(), vec_.end(), nullptr, [=value](B *left, B *right) {
        unsigned int leftval = left ? left->attr() : attr;
        unsigned int rightval = right ? right->attr() : attr;
        return leftval < rightval;
    });
    

    You can actually cut out the pointer altogether, to give what I think is the “cleanest” solution:

    struct FindAttr {
        bool operator()(B *left, unsigned int rightval) {
            return left->attr() < rightval;
        }
        bool operator()(unsigned int leftval, B *right) {
            return leftval < right->attr();
        }
    };
    
    ...
    
    return equal_range(vec_.begin(), vec_.end(), value, FindAttr());
    

    AFAIK there’s no direct equivalent to this with a lambda, since lambdas can only have one call signature. I suppose you could write a lambda that takes a boost::variant (or any type that implicitly converts from both unsigned int and from B*, and that remembers which one it was).

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

Sidebar

Related Questions

I have a custom class and std::vector filled with objects of this class. And
I have a class that adapts std::vector to model a container of domain-specific objects.
I have a vector v of objects A in a Class B where each
I have a vector of pointers to class objects. These class objects invoke new
I have this class I want to serialize: (the private attr's) class Task {
I have the following code #include <iostream> #include <vector> class Entity { public: virtual
I have class and this class contains a number. And I have a vector
I have a class: class X { vector<shared_ptr<T>> v_; public: vector<shared_ptr<const T>> getTs() {
I want to have a deep copy of an vector with pointers to objects,
I have a vector of Student objects which I want to sort using #include

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.