Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any built-in function to test if 4 is in [1,2,3,4] (vector)

In Ruby I can do:

 [1,2,3,4].include?(4) #=>True

In Haskell I can do :

4 `elem`   [1,2,3,4]   #=> True

What should I do in C++?

like image 757
pierrotlefou Avatar asked Nov 28 '22 08:11

pierrotlefou


1 Answers

Here an example using find:

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
        std::vector<int> Num(4);
        //insert values
        Num[0]=1;
        Num[1]=2;
        Num[2]=3;
        Num[3]=4;
        std::vector<int>::iterator p = find(Num.begin(), Num.end(), 4);
        if (p == Num.end())
           std::cout  << "Could not find 4 in the vector"  << std::endl;
        else
           std::cout  << "Have found 4 in the vector"  << std::endl;
        return 0;
}
like image 192
Burkhard Avatar answered Dec 21 '22 07:12

Burkhard