Sun Nov 27 2022

11 most useful functions in C++

Programming13895 views
11 most useful functions in C++

C++ is a general-purpose, object-oriented programming language. The language can handle a lot of complex things such as memory management, hardware and many more. You also need to write a lot of code to get a working prototype if you want to building from scratch.

But, C++ standard libraries are huge. There have a number of useful functions which you can use in yourprograms by simply including more #include lines at the top. Let's take a look 11 of them -

swap()

It uses to exchange the contents of two stacks and modifies the size of the stack if necessary. Many components of the standard library call swap() in an unqualified manner to allow custom overloads for non-fundamental types to be called instead of this generic version.

find_if()

If allowed to search for the first element in a sequence that satisfies a particular condition. The sequence can be defined by iterators like start or finish, and third argument us as a condition. If no such element is found, the function returns last.

replace_if()

It works similar like find_if (), but instead of returning any value replace the found element otherwise remain unchanged.

binary_search()

Binary search is an important component. To create a binary search program you need to know sort and binary search both at the same time. But binary_search () function solved this complexity by search a list of data and return boolean true if the element is present in the range else return false.

random_shuffle()

It uses to rearrange list of elements. The function takes two iterators to the beginning and end of the container as parameters, then swaps the value of each element with that of some other randomly picked element.

priority_queues()

It is a container adaptor that provides constant time lookup of the largest element. Working with a priority_queue is similar to managing aheap with the benefit of not being able to accidentally invalidate the heap.

unordered_map()

It is an associative container that contains key-value pairs with unique keys. Internally, the elements in the unordered_map are not sorted, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values.

map()

It is a sorted associative container that contains key-value pairs with unique keys. Internally, the elements in a map are always sorted by its key and mapped values can be accessed directly by their corresponding key using the bracket operator.

fmod()

It takes a two arguments and returns a value of type double, float or long double type. And if the denominator y is zero, then returns NaN.

unique()

It used to remove all the first element from every consecutive group of equivalent elements in the range and returns a past-the-end iterator for the new logical end of the range, but not able to alter the size of the container.

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.