1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
// Example program
#include <iostream>
#include <valarray>
// Returns a vector with the indexes whose vector values are true
std::vector<unsigned int> trueIndexes( const std::vector<bool> &vector )
{
std::vector<unsigned int> indices;
auto it = std::find_if(std::begin(vector), std::end(vector), [](bool element){return element;});
while (it != std::end(vector)) {
indices.emplace_back(std::distance(std::begin(vector), it));
it = std::find_if(std::next(it), std::end(vector), [](bool element){return element;});
}
indices.shrink_to_fit();
return indices;
} // end function trueIndexes
int main()
{
std::valarray<float> a = {0,9,1,8,2,7,3,6,4,5}, b = {0,1,2,3,4,5,6,7,8,9};
std::vector<unsigned int> c;
std::valarray<bool> aux(10);
aux[a<b] = true;
std::vector<bool> aux1(std::begin(aux),std::end(aux));
c = trueIndexes(aux1);
for(const auto& s: a)
std::cout << s << ' ';
std::cout << "\n";
for(const auto& s: b)
std::cout << s << ' ';
std::cout << "\n";
for(const auto& s: c)
std::cout << s << ' ';
std::cout << "\n";
}
|