C++ boost::range::count_if














































C++ boost::range::count_if



Description:
The header 'boost/range/algorithm/count_if.hpp'   contains the algorithm count_if() in C++ boost library. This function is very similar to boost::range::count() the only difference in this version is that this function uses unary predicate which returns a value convertible to bool. The value returned indicates whether the element of the sequence has to be counted by the function.

Syntax:

template<class SinglePassRange, class UnaryPredicate>
typename range_difference<const SinglePassRange>::type count_if(const SinglePassRange& rng, UnaryPredicate pred);


Parameters:
The following parameters are accepted by the function:
1.) rng: It specifies the sequence which has to be reversed.
2.) pred: Unary predicate which accepts each elements of the sequence and returns the Boolean value where true indicates the element has to be counted.


Program:
#include <boost/range/algorithm/count_if.hpp>
#include <iostream>
#include <vector>
using namespace std;


int main()
{
   
vector<int> Arr = {222, 819, 619, 7887, 6116, 535, 42};
   
// Predicate to check palindrome numbers. Returns true if element is palindrome
   
auto is_palindrome = [](int element)
    {
       
string str1 = to_string(element), str2 = to_string(element);
        reverse(str1.begin(), str1.end());
       
return str1 == str2;
    };
   
// count_if to check total number of palindrome numbers in array.
   
int cnt = boost::range::count_if(Arr, is_palindrome);

   
cout << "There are " << cnt << " palindrome numbers in array.";
}

Output:
There are 4 palindrome numbers in array.

Complexity:
Time Complexity: The time complexity is distance(rng) number of invocations of predicate pred.

Please write on comment section if you find any mistake or for any suggestions.

More Articles of Abhishek Kumar Singh:

Name Views Likes
C++ boost::range::replace_if 1068 0
C++ boost::range::copy_backward 806 1
C++ boost::range::max_element 1283 0
C++ boost::range::inplace_merge 736 0
C++ boost::range::copy 1525 1
C++ boost::algorithm::is_partitioned() 910 1
C++ boost::algorithm::copy_if() 1137 0
C++ boost::range::for_each (version 2) 807 0
C++ boost::range::set_symmetric_difference 727 0
C++ boost::remove_copy_if 816 0
C++ boost::range::set_intersection 1126 0
C++ boost::range::find_end 752 0
C++ boost::range::remove_erase_if 1807 0
C++ boost::range::push_back 1255 0
C++ boost::range::generate 711 0
C++ boost::algorithm::any_of() 820 0
C++ boost::range::insert 827 0
C++ boost::range::remove_erase 992 0
C++ boost::range::reverse 1331 0
C++ boost::algorithm::equal() 834 1
C++ boost::range::copy_n 898 0
C++ boost::range::random_shuffle 1356 1
C++ boost::algorithm::partition_point() 697 0
C++ boost::algorithm::one_of_equal() 575 0
C++ boost::algorithm::all_of() 1013 1
C++ boost::range::merge 1144 0
C++ boost::range::reverse_copy 810 0
C++ boost::range::find 899 0
C++ boost::range::fill_n 659 0
Removing duplicate elements from std::vector (using std::unique and std::set) 2891 1
C++ boost::range::equal 780 0
C++ boost::algorithm::iota() 1162 0
C++ boost::range::is_sorted 884 0
test article 845 2
C++ boost::algorithm::is_permutation() 940 1
C++ boost::partial_sum 936 0
C++ boost::range::partial_sort 981 1
C++ boost::range::min_element 1139 0
C++ boost::range::iota 1152 0
C++ boost::range::set_union 796 0
C++ boost::algorithm::partition_copy() 1143 3
C++ boost::range::swap_ranges 688 0
C++ boost::range::for_each 988 0
C++ boost::range::upper_bound 1153 0
C++ boost::range::binary_search 1313 0
C++ boost::algorithm::all_of_equal() 595 0
C++ boost::algorithm::copy_n() 729 1
C++ boost::range::lower_bound 1110 0
C++ boost::algorithm::gather() 1658 0
C++ boost::algorithm::none_of_equal() 638 0
C++ boost::algorithm::one_of() 771 2
C++ boost::range::rotate 921 0
C++ boost::algorithm::any_of_equal() 944 0
Use of Comparator in C++ 2244 0
C++ boost::range::count 869 0
C++ boost::range::replace_copy_if 673 0
C++ boost::range::remove 891 2
C++ boost::remove_if 1577 1
C++ boost::range::nth_element 1209 1
C++ boost::range::partition 800 1
C++ boost::range::erase 721 0
C++ boost::range::fill 941 1
C++ boost::range::find_if 1474 0
C++ boost::range::lexicographical_compare 848 0
C++ boost::algorithm::none_of() 633 0
C++ boost::algorithm::hex() 4220 0
C++ boost::range::replace 820 0
C++ boost::range::replace_copy 857 1
C++ boost::range::set_difference 1095 0
C++ boost::range::overwrite 775 0
C++ boost::range::count_if 1024 0
C++ boost::range::push_front 707 0
C++ boost::range::includes 705 0
C++ boost::algorithm::is_sorted() 742 0
C++ boost::range::remove_copy 663 1
C++ boost::algorithm::minmax_element 656 1
Deletion of leaf node of a Binary Search Tree 1820 1
Nth Fibonacci Number (Recursive Solution, Dynamic Programming, Iterative Solution 4083 1
C++ boost::range::rotate_copy 699 0

Comments