#include <iostream>template <typename T>constexpr T how_many_nagatives(T seq[], int n, int i = 0) { return i == n || seq[i] >= 0 ? i : how_many_nagatives(seq, n, i+1);}int main() { constexpr int sequence[] = {-4, -6, 8, 10}; constexpr int n = sizeof(sequence) / sizeof(*sequence); constexpr int k = how_many_nagatives(sequence, n); std::cout << k << std::endl; return 0;}
Использован тернарный оператор
Код:
#include <iostream>template <typename T>constexpr T how_many_nagatives(T seq[], int n, int i = 0) { return i == n || seq[i] >= 0 ? i : how_many_nagatives(seq, n, i+1);}int main() { constexpr int sequence[] = {-4, -6, 8, 10}; constexpr int n = sizeof(sequence) / sizeof(*sequence); constexpr int k = how_many_nagatives(sequence, n); std::cout << k << std::endl; return 0;}