-2

This question is similar to this question: What is the most elegant way to check if all values in a boolean array are true?

But for vector<bool> in c++.

How can use all_of for this purpose (for example I want to check the value between ranges, element 10 to 20) so I can not use begin and end.

Community
  • 1
  • 1
user1436187
  • 3,252
  • 3
  • 26
  • 59
  • Maybe `std::find(cbegin(vector) + 10, cbegin(vector) + 20, false) == cbegin(vector) + 20`. – nwp Aug 07 '14 at 09:58
  • You know that functions that take iterators, take any valid iterator, not only `begin()` and `end()`, right? –  Aug 07 '14 at 09:58

2 Answers2

3

If you want to use std::all_of, then just return the value in the predicate function :

#include <vector>
#include <iostream>
#include <algorithm>


int main()
{
    std::vector< bool > v( 30, true );

    const bool all = std::all_of( std::begin(v)+10, std::begin(v)+20, []( const bool v){ return v; } );

    std::cout << all << std::endl;
}
BЈовић
  • 62,405
  • 41
  • 173
  • 273
1

I'd just try std::find(begin(v)+10, begin(v)+20, false) - it returns begin(v)+20 if all values between 10 and 20 are true.

MSalters
  • 173,980
  • 10
  • 155
  • 350