Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,945 questions

51,887 answers

573 users

How to use ranges::find_if_not to find the first element in the range that satisfies a criteria with C++

2 Answers

0 votes
#include <algorithm>
#include <iostream>
 
int main()
{
    namespace ranges = std::ranges;
  
    const auto vec = {20, 44, 100, 5, 8, 3, 9, 2, 1};
  
    auto is_even = [](int x) { return x % 2 == 0; };
  
    auto result = ranges::find_if_not(vec, is_even);
     
    if ( result != vec.end()) {
        std::cout << "First odd element in vector: " << *result << '\n';
    }
    else {
        std::cout << "No odd elements in vector\n";
    }
}
 
 
  
  
/*
run:
 
First odd element in vector: 5

 
*/

 



answered Apr 28, 2024 by avibootz
0 votes
#include <algorithm>
#include <iostream>
 
int main()
{
    namespace ranges = std::ranges;
  
    const auto vec = {100, 400, 5, 3, 1, 9, 2, 8};
 
    auto divides_4 = [](int x) { return x % 4 == 0; };
     
    auto result = ranges::find_if_not(vec.begin(), vec.end(), divides_4);
  
    if (result != vec.end()) {
        std::cout << "First element not divisible by 4 in vector: " << *result << '\n';
    }
    else {
        std::cout << "No elements in vector are not divisible by 4\n";
    }
}
 
 
  
  
/*
run:
 
First element not divisible by 4 in vector: 5
 
*/

 



answered Apr 28, 2024 by avibootz
...