How to get the first odd number in a vector with C++

1 Answer

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

bool IsOdd (int i) {
    return ((i % 2) == 1);
}

int main(void)
{
    std::vector<int> vec = {4, 6, 8, 2, 5, 10, 3, 1, 13};
     
    std::vector<int>::iterator it = std::find_if(vec.begin(), vec.end(), IsOdd);
    
    std::cout << "The first odd value is: " << *it;
}
 
 
 
 
/*
run:
 
The first odd value is: 5
 
*/

 



answered Dec 8, 2023 by avibootz

Related questions

1 answer 110 views
1 answer 99 views
1 answer 182 views
2 answers 200 views
...