iterators to range of elements in a vector whose attributes have specific value

Posted by user1801173 on Stack Overflow See other posts from Stack Overflow or by user1801173
Published on 2012-12-13T11:02:38Z Indexed on 2012/12/13 11:03 UTC
Read the original article Hit count: 156

Filed under:
|
|
|

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) ?

Thanks for help.

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl