How to use ranges::find to find the first element in a range equal to value with C++

2 Answers

0 votes
#include <algorithm>
#include <iostream>

int main()
{
    namespace ranges = std::ranges;
 
    const int n = 2;
    const auto vec = {5, 3, 2, 7, 9, 2, 2};
 
    if (ranges::find(vec, n) != vec.end()) {
        std::cout << "yes" << '\n';
    }
    else {
        std::cout << "no" << '\n';
    }
}


 
 
/*
run:

yes

*/

 



answered Apr 28, 2024 by avibootz
edited Apr 28, 2024 by avibootz
0 votes
#include <algorithm>
#include <iostream>

int main()
{
    namespace ranges = std::ranges;
 
    const int n = 2;
    const auto vec = {5, 3, 2, 7, 9, 2, 2};
 
    if (ranges::find(vec.begin(), vec.end(), n) != vec.end()) {
        std::cout << "yes" << '\n';
    }
    else {
        std::cout << "no" << '\n';
    }
}


 
 
/*
run:

yes

*/

 



answered Apr 28, 2024 by avibootz
edited Apr 28, 2024 by avibootz
...