You can use either the standard std::upper_bound algorithm using 1 as an argument, or the std::lower_bound algorithm using 0 as an argument.
For example,
#include <iostream> #include <functional> #include <iterator> #include <algorithm> int main() { int a[] = { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, }; auto it = std::upper_bound(std::begin(a), std::end(a), 1, std::greater<int>()); if (it != std::begin(a)) { std::cout << "The last 1 is at the position " << std::distance(std::begin(a), std::prev(it)) << std::endl; } else { std::cout << "There is no 1 in the array" << std::endl; } }
Output of the program to the console
The last 1 is at the position 4
Instead of expression
std::distance(std::begin(a), std::prev(it))
you can use the expression
std::distance(std::begin(a), it) - 1
And then you get the desired position if 1 is present in the array or -1 if there is none 1.