Friday 30 June 2017

STL Algorithm

Algorithm Used in Vector

(i) Non-Manipulating Algorithms:
          1. sort(first_iterator,last_iterator)-To sort a given vector.
          2. reverse(first_iterator,last_iterator)-To reverse a given vector.
          3. *max_element(first_iterator,last_iterator)-To find the maximum element of a vector
          4. *min_element(first_iterator,last_iterator)-To find the minimum element of a vector 
          5. accumulate(first_iterator,last_iterator.initial value of sum)-Does the summation of vector elements.
          6. count(first_iterator,last_iterator,x)-Count the occurrence in vector.
          7. find(first_iterator,last_iterator,x)-Points the last address of the vector if the element is not found

Sample Code:
       
          8. binary_search(first_iterator,last_iterator,x)-Tests whether x exists in the sorted vector or not!
          9. lower_bound(first_iterator,last_iterator,x)- The leftmost position in which 'x' can be inserted keeping the vector sorted.
          10. upper_bound(first_iterator,last_iterator,x)- The rightmost position in which 'x' can be inserted keeping the vector sorted.

Sample Code:
       
 (ii) Manipulating Algorithms:
           1. vect.erase(position to be deleted)-This erases the selected element and shifts and resizes the vector.
           2. vect.erase(unique(vect.begin(),vect.end()),vect.end())-This erases thye duplicate occurrence in the sorted vector.
           3. next_permutation(first_iterator,last_iterator)-This modified vector to it's next permutation.
           4. prev_permutation(first_iterator,last_iterator)-This modified vector to it's previous permutation.

Sample Code:

Updating..............

No comments:

Post a Comment