Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
3 contributors

Users who have contributed to this file

@Bhupesh-V @Pranav-Khurana @sdmg15
458 lines (380 sloc) 16.4 KB

<algorithm>

View Contents
  1. all_of
  2. any_of
  3. none_of
  4. for_each
  5. for_each_n
  6. count
  7. count_if
  8. mismatch
  9. equal
  10. find
  11. find_if
  12. find_if_not
  13. find_end
  14. find_first_of
  15. adjacent_find
  16. adjacent_difference
  17. search
  18. search_n
  19. copy
  20. copy_if
  21. copy_n
  22. copy_backward
  23. move
  24. move_backward
  25. fill
  26. fill_n
  27. transform
  28. generate
  29. generate_n
  30. remove
  31. remove_if
  32. remove_copy
  33. remove_copy_if
  34. replace
  35. replace_if
  36. replace_copy
  37. replace_copy_if
  38. swap
  39. swap_ranges
  40. iter_swap
  41. reverse
  42. reverse_copy
  43. rotate
  44. rotate_copy
  45. random_shuffle
  46. shuffle
  47. unique
  48. unique_copy
  49. is_partitioned
  50. partition
  51. partition_copy
  52. stable_partition
  53. partition_point
  54. is_sorted
  55. is_sorted_until
  56. sort
  57. partial_sort
  58. partial_sort_copy
  59. stable_sort
  60. nth_element
  61. lower_bound
  62. upper_bound
  63. binary_search
  64. equal_range
  65. merge
  66. inplace_merge
  67. includes
  68. set_difference
  69. set_intersection
  70. set_symmetric_difference
  71. set_union
  72. is_heap
  73. is_heap_until
  74. make_heap
  75. push_heap
  76. pop_heap
  77. sort_heap
  78. clamp
  79. max
  80. max_element
  81. min
  82. min_element
  83. minimax
  84. minimax_element
  85. lexicographical_compare
  86. is_permutation
  87. next_permutation
  88. prev_permutation
  89. sample
  90. destroy
  91. uninitialized_copy
  92. uninitialized_fill
  93. uninitialized_move
  94. uninitialized_default_construct
  95. uninitialized_value_construct
  96. iota
  97. partial_sum
  98. inclusive_scan
  99. exclusive_scan
  100. reduce
  101. transform_reduce
  102. transform_inclusive_scan
  103. transform_exclusive_scan
  104. inner_product
  105. accumulate

for_each

Description : Applies the given Function Object f to each element of the container in range [first, last)

Example :

     class Display{
      public: 
          void operator() (int i){ 
          std::cout << i << " ";
          }
     };
     
    std::vector<int> a{3,2,3,9};
    // Applying the function object Display to each element of a 
    std::for_each(a.begin(),a.end(), Display); // Will output, 3 2 3 9
    

count

Description : : Returns the number of elements in the range [first, last) satisfying specific criteria(counts the elements that are equal to value).

Example :

    std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
 
    // determine how many integers in a std::vector match a target value.
    int target1 = 3;
    int num_items1 = count(v.begin(), v.end(), target1);
    cout << "number: " << target1 << " count: " << num_items1 << '\n';

See Sample Code

count_if

Description : Returns the number of elements in the range [first, last) satisfying specific criteria(counts the elements that are equal to value).

Example:

    vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
 
    // use a lambda expression to count elements divisible by 3.
    int num_items3 = count_if(v.begin(), v.end(), [](int i){return i % 3 == 0;});
    cout << "number divisible by three: " << num_items3 << '\n';

find

Description : Returns the first element in the range [first, last) that satisfies specific criteria(searches for an element equal to value).

Example:

    vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };

    int searchme = 4;
    if(find(std::begin(v), std::end(v), searchme) != end(v)){
        cout <<"\n v contains 3";
    }
    else
        cout<<"No match !!";

find_if

Description : Returns the first element in the range [first, last) that satisfies specific criteria(searches for an element for which predicate/condition p returns true).

Example:

    bool IsOdd (int i) {
    return ((i%2)==1);
    }

    int main(){
        vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
        if(find_if(std::begin(v), std::end(v),IsOdd) != end(v)){
            cout <<"\n Odd Value Found";
        }
        else
            cout<<"No match !!";
        return 0;
    }

find_if_not

Description : Returns the first element in the range [first, last) that satisfies specific criteria(searches for an element for which predicate q returns false).

Example:

    bool IsOdd (int i) {
        return ((i%2)==1);
    }

    int main(){
        vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
        if(find_if_not(std::begin(v), std::end(v),IsOdd) != end(v)){
            cout <<"\n First Even Value";
        }
        else
            cout<<"No match !!";
        return 0;
    }

find_first_of

Description : Return iterator to the first element in the range [first, last) that is equal to an element from the range [s_first; s_last). If no such element is found, last is returned.(Searches the range [first, last) for any of the elements in the range [s_first, s_last) ).

Example:

    std::vector<int> v{0, 2, 3, 25, 5};
    std::vector<int> t{3, 19, 10, 2};
 
    auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end());
 
    if (result == v.end()) {
        std::cout << "no elements of v were equal to 3, 19, 10 or 2\n";
    } else {
        std::cout << "found a match at "
                  << std::distance(v.begin(), result) << "\n";
    }

generate

Description : Used to generate numbers based upon a generator function, and then, it assigns those values to the elements in the container in the range [first, last). The generator function has to be defined by the user, and it is called successively for assigning the numbers.

Example:

    int gen() { 
        static int i = 0; 
        return ++i; 
    } 
  
    using namespace std; 
    int main() { 
        int i; 
        vector<int> v1(10); 
  
        std::generate(v1.begin(), v1.end(), gen); 
  
        vector<int>::iterator i1; 
        for (i1 = v1.begin(); i1 != v1.end(); ++i1) { 
            cout << *i1 << " "; 
        } 
        return 0; 
    } 

adjacent_find

Description : Binary function which returns first adjacent element pairs based on certain condition (as third argument). Default condition checks equality.

Example :

    std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };
 
    // Binary function which returns first adjacent element pairs based on certain condition (as third argument) . 
    // Default condition checks equality.
    auto i  = std::adjacent_find (v.begin(), v.end());
    cout <<"The first pair of repeated elements are:"<< *i <<'\n';

binary_search

Description : Binary search is a widely used searching algorithm that requires the array to be sorted before search is applied. The main idea behind this algorithm is to keep dividing the array in half (divide and conquer) until the element is found, or all the elements are exhausted. The prototype for binary search is :

binary_search(startaddress, endaddress, valuetofind)

Example :

    void show(int a[], int arraysize) { 
        for (int i = 0; i < arraysize; ++i) 
            cout << a[i] << " "; 
    } 
  
int main() { 
    int a[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; 
    int asize = sizeof(a) / sizeof(a[0]); 
    cout << "\n The array is : "; 
    show(a, asize); 
  
    cout << "\n\nLet's say we want to search for 2 in the array"; 
    cout << "\n So, we first sort the array"; 
    sort(a, a + asize); 
    cout << "\n\n The array after sorting is : "; 
    show(a, asize); 
    cout << "\n\nNow, we do the binary search"; 
    if (binary_search(a, a + 10, 2)) 
        cout << "\nElement found in the array"; 
    else
        cout << "\nElement not found in the array"; 
  
    cout << "\n\nNow, say we want to search for 10"; 
    if (binary_search(a, a + 10, 10)) 
    cout << "\nElement found in the array"; 
    else
        cout << "\nElement not found in the array"; 
  
    return 0; 
} 

reverse

Description : It reverses the order of the elements in the range [first, last) of any container.

Example :

 int main() { 
          int i; 
          vector<int> v1{1,2,,3,4};
          
          reverse(v1.begin(),v1.end());

          vector<int>::iterator i1; 
          for (i1 = v1.begin(); i1 != v1.end(); ++i1) { 
              cout << *i1 << " "; 
          } 
          return 0; 
      } 

sort

Description : This function is implemented as Quick-sort. The complexity of it is O(N*log(N)).

Example :

    void show(int a[]) { 
        for(int i = 0; i < 10; ++i) 
            cout << a[i] << " "; 
    } 
  
    int main() {

        int a[10]= {1, 5, 8, 9, 6, 7, 3, 4, 2, 0}; 
        cout << "\n The array before sorting is : "; 
        show(a); 
  
        sort(a, a+10); 
  
        cout << "\n\n The array after sorting is : "; 
        show(a); 
  
        return 0; 
} 

all_of

Description : This function operates on whole range of array elements and checks for a given property on every element and returns true when each element in range satisfies specified property, else returns false.

Example :

int main() { 

	std::vector<int> v{10, 2, 4, 6}; 
	
	if (std::all_of(v.begin(), v.end(), [](int i){ return i % 2 == 0; })) { 
		std::cout << "All numbers are even\n"; 
	} 
    else{
        std::cout << "All numbers are not even\n"; 
    }
}

Run Code

any_of

Description : This function operates on whole range of array elements and checks for a given property on every element and returns true when atleast one element in range satisfies specified property, else returns false.

Example :

int main() {
    
    std::vector<int> v{1, 3, 5, 7, 2};
	
	if( std::any_of(v.begin(), v.end(), [](int i){ return i % 2 == 0; }))	{ 
		std::cout << "A number is even\n"; 
	}
    else{
        std::cout << "No number is even\n";
    }
} 

Run Code

none_of

Description : This function operates on whole range of array elements and checks for a given property on every element and returns true when no element in range satisfies specified property, else returns false.

Example :

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


int main() {
    
    std::vector<int> v{1, 3, 5, 7};
	
    if( std::none_of(v.begin(), v.end(), [](int i){ return i % 2 == 0; })) { 
	std::cout << "No number is  even\n"; 
    }
    else {
        std::cout << "A number is even\n";
    }
} 

Run Code

mismatch

Description :

  1. mismatch( start_iter1, end_iter1, start_iter2 ) : This function returns the 1st mismatch pair pointer, first element pointing to position of first mismatch element of 1st container, second element pointing to position of first mismatch element of 2nd container. If no mismatch is found, 1st element points to position after last element of 1st container and 2nd points to corresponding position in 2nd container.
  2. mismatch( start_iter1, end_iter1, start_iter2, comparator) : This function is almost similar to the working as the above mentioned version, but it offers to find not only equality mismatches, but also other user-defined and desired mismatches via user- defined comparator function that is sent as 4th argument and returns a boolean true or false.

Example :

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

bool check(int i,int j){
    return ((i+10)==j);
}

int main () {
    
    std::vector<int> v1{10, 20, 30, 40, 50, 70, 80, 90};
    std::vector<int> v2{10, 20, 30, 40, 50, 60, 70, 80};
    std::vector<int> v3{20, 30, 40, 50, 70, 80, 90};
     
    //PAIR P TO STORE THE PAIR RETURNED BY MISMATCH
    std::pair<std::vector<int>::iterator,std::vector<int>::iterator> p;

    //MISMATCH FUNCTION CALL 1: returns the first element where two arrays differ
    p = std::mismatch (v1.begin(), v1.end(), v2.begin());
    std::cout << "Mismatching elements case 1: " << *p.first << " and " << *p.second << '\n';
    
    //MISMATCH FUNCTION CALL 2: returns the first element where two arrays differ as per predicate 
    p = std::mismatch (v1.begin(), v1.end(), v3.begin(), check);
    std::cout << "Mismatching elements case 2: " << *p.first << " and " << *p.second << '\n';
    
    return 0;
}

Run Code

You can’t perform that action at this time.