help with std::none_of

hello everybody
i'm trying to check if any object in a vector contains a specified integer, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <algorithm>

class Foo{
public:
    int x;

    Foo(int x){
        this->x = x;
    }
};

int main(){
    std::vector<Foo> foo = {1, 2, 3, 4, 5};
    int i = 5;
    if (std::none_of(foo.begin(), foo.end(), foo.x == i)) {
        std::cout << "i isn't found in any of the objects";
    }
    return 0;
}


my problem is at "foo.x == i". how do i check for the member "x" in each object?

thanks in advance :)
1
2
3
4
5
6
    // lambda expression: http://www.stroustrup.com/C++11FAQ.html#lambda
    // note: i is captured by value
    if( std::none_of( seq.begin(), seq.end(), [i]( Foo f ) { return f.x == i ; } ) ) {

        std::cout << "i isn't found in any of the objects";
    }

thank you, it works :)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <algorithm>

class Foo{
public:
    int x;

    Foo(int x){
        this->x = x;
    }
};

int main(){
    std::vector<Foo> foo = {1, 2, 3, 4, 5};
    int i = 5;
    if( std::none_of(foo.begin(), foo.end(), [i](Foo f) {return f.x == i;})){
        std::cout << "i isn't found in any of the objects";
    }
    return 0;
}
Topic archived. No new replies allowed.