0

I have a class S:

class S
{
public:
    S(int ssx, int ssy, int cnt)
    {
        sx = ssx;
        sy = ssy;
        count = cnt;
    }
    int sx;
    int sy;
    int count;
};

I want to make a container which can find a pointer (iterator) of the element that match by 2 parametres (like pair {sx, sy}, this pair should be equal to the pair of the element in container). Are there any STL methods or should I implement the search using the circle FOR(;;) and a simple vector of S*?

HackSaw
  • 201
  • 1
  • 3
  • 12

1 Answers1

1

You could use std::find_if().

First, declare a predicate class for the two members

struct CmpS {
    int x;
    int y;
    CmpS(int x, int y) { this.x = x; this.y = y; }
    bool operator()(const S& s) const { return s.sx == x && s.sy == y; }
};

Then, call std::find_if().

std::vector<S> vec;
std::vector<S>::iterator iter = std::find_if(vec.begin(), vec.end(), CmpS(x, y));
timrau
  • 22,578
  • 4
  • 51
  • 64