6 Important STL Functions Every Programmer Should Know.
Manas Sinha
Developer | Designer

By Manas | 2 September 2020 | 2 mins read

6 Important STL Functions Every Programmer Should Know

The C++ STL (Standard Template Library) is a powerful set of C++ template classes to provide general-purpose functions with templates that implement many popular and commonly used algorithms

0. std::sort()

Sorts the array in ascending order. Takes two pointers as parameters, start pointer and the end pointer to the array to be sort.

sort code

1. std::reverse()

Reverses the array. Takes two pointers as parameters, Start pointer and the end pointer to the array to be sorted.

2. std::count()

Counts the number of occurences of a given element in an array. Takes three parameters, Start pointer and the end pointer of the array to be searched and the element to be counted.

count code

3. std::nth_element()

Rearranges the elements in the range [first,last), in such a way that the element at the nth position is the element that would be in that position in a sorted sequence. It takes three parameters, the start pointer, pointer to the nth position, and the end pointer. It also rearranges the array in such a way that the values before n are smaller than arr[n] and values after n are greater than arr[n] .

nth element code

4. std::advance()

Advances the iterator it to to n distance forward if n>0 or backward if n<0. Takes two parameters, the iterator and the value n .

advance code

4. std::copy()

Copies the contents of one array to another. Takes three parameter, start and the end pointer of the array to be copied and the start pointer of the output array.

Before you go.

The functions mentioned above can not only be applied to the entire container but also a part of it by carefully changing the start pointer and end pointer. for example :

Also, I have explained each functions using array as an example, but that can be applied to other containers, but do use your common sense while doing so.

LEAVE A COMMENT

If you like the post leave a comment and share it.

This Post Has 2 Comments

Leave a Reply