C++ STL Functions
C++ STL Functions
list_name.erase(iterator position) -
list_name.erase(iterator fis, iterator last) – Removes a single element or a range of
elements from the list.
Parameters: This function can accepts different parameters based on whether it is used to erase
a single element or a range of element from the list container.
-position: This parameter is used when the function is used to delete a single element.
This parameter refers to an iterator which points to the element which is need to be erased from
the list container.
-first, last: These two parameters are used when the list is used to erase elements from
a range. The parameter first refers to the iterator pointing to the first element in the range and the
parameter last refers to the iterator pointing to the last element in the range which is needed to be
erased. This erases all the elements in the range including the element pointed by the
iterator first but excluding the element pointed by the iterator last.
Return Value: This function returns an iterator pointing to the element in the list container which
followed the last element erased from the list container.
list_name.remove(val) – Removes all the elements from the list, which are equal to given
element.
list_name.remove_if(predicate) in C++ STL– Used to remove all the values from the list
that correspond true to the predicate or condition given as parameter to the function.
The predicate in the form of aa function pointer or function object is passed
as the parameter.
list_name.splice() function in C++ STL– Used to transfer elements from one list to
another.
list_name.merge() function in C++ STL– Merges two sorted lists into one
Syntax:
list1_name.merge(list2_name)
Syntax:
list1_name.merge(list2_name, comparator)
Parameters: The function accepts two parameters which are described below:
list2-name – It specifies the list2 which is to be merged in list1.
comparator – It is a binary predicate which takes two values of the same type that of those
contained in the list, returns true if the first argument is considered to go before the second in the
strict weak ordering it defines, and false otherwise.
list_name.emplace(iterator point to the target position, element) function in C++ STL–
Extends list by inserting new element at a given position.
Return value: It returns a random access iterator which points to the newly inserted element.
Forward List in C++
1. assign() :- This function is used to assign values to forward list, its another variant is used to
assign repeated elements.
Syntax:
Version 1:forward_list_name.assign(iterator it1, iterator it2)
Version 2:forward_list_name.assign(int n, val)
Version 3:forward_list_name.assign(initializer_list li)
Parameters: This function accepts different parameters in different version which are
discussed below:
Iterator: The first version takes two iterators as parameters. New elements are
constructed from each element in the range [it1, it2) i.e it includes all elements
between it1 and it2 including the element dereferenced by it1 but excluding the
element pointed by it2.
n and val: In the second version n elements are created and each element is
initialized with value val.
Ininitializer_list: In the third version the new contents are created which are
initialized with copies of the values passed as initializer list, in the same order.
Return Value This function does not return any value.
2. push_front ( val ) :- This function is used to insert the element at the first position on forward
list. The value from this function is copied to the space before first element in the container. The
size of forward list increases by 1.
3. pop_front() :- This function is used to delete the first element of list.
4. emplace_front ( val ) :- This function is similar to the previous function but in this no copying
operation occurs, the element is created directly at the memory before the first element of the
forward list. Time Complexity : O(1)
4. insert_after (iterator position, vals ) This function gives us a choice to insert elements at
any position in forward list. The arguments in this function are copied at the desired position.
5. emplace_after ( iterator position, elenments ) This function also does the same operation
as above function but the elements are directly made without any copy operation. This function
returns an iterator that points to the newly inserted element.
6. erase_after() This function is used to erase elements from a particular position in the forward
list. Clear() - used to remove all the elements of the forward list container, thus making its size 0.
Syntax :
1. flistname.erase_after(position)
2. flistname.erase_after(startingposition, endingposition)
Parameters :
Position previous of the element to be removed in the form of iterator.
or the range specified using start and end iterator.
Result :
Elements are removed from the next position of the container.
7. remove ( element ) :- This function removes the particular element from the forward
list mentioned in its argument.
8. remove_if (predicate) :- This function removes according to the condition in its
argument.
9. splice_after() :- This function transfers elements from one forward list to other.
Syntax:
forwardlist1_name.splice_after(position iterator, forwardlist2_name,
first iterator, last iterator)
Parameters: The function accepts four parameters which are specified as below:
position – Specifies the position in the forward_list after which the new elements
are to be inserted.
forwardlist2_name– Specifies the list from which elements are to be inserted.
first– Specifies the iterator after which insertion is to be done.
last– Specifies the iterator till which insertion is to be done.
Return value: The function has no return value.
10. front()– This function is used to reference the first element of the forward list container.
begin()– begin() function is used to return an iterator pointing to the first element of the
forward list container.
end()– end() function is used to return an iterator pointing to the last element of the list
container.
cbegin()– Returns a constant iterator pointing to the first element of the forward_list.
cend()– Returns a constant iterator pointing to the past-the-last element of the forward_list.
before_begin()– Returns a iterator which points to the position before the first element of
the forward_list.
cbefore_begin()– Returns a constant random access iterator which points to the position
before the first element of the forward_list.
max_size()– Returns the maximum number of elements can be held by forward_list.
18. resize()– Changes the size of forward_list.
19. merge() :- This function is used to merge one forward list with other. If both the lists are
sorted then the resulted list returned is also sorted.
Syntax:
forwardlist_name1.merge(forward_list& forwardlist_name2)
or
forwardlist_name1.merge(forward_list& forwardlist_name2, Compare comp)
Parameters: The function accepts two parameters which are specified as below:
1. forwardlist_name2 – Another forward list of the same type which is to be merged
2. comp – A comparison function which should return true or false.
Return value: The function does not return anything.
20. operator “=” :- This operator copies one forward list into other. The copy made in this case
is deep copy.
Syntax: forwardlistname1 = (forwardlistname2)
21. sort() :- This function is used to sort the forward list.
Time Complexity : O(nlogn)
22. unique() :- This function deletes the multiple occurrences of a number and returns a forward
list with unique elements. The forward list should be sorted for this function to execute
successfully.
Methods of Deque:
deque insert() function in C++ STL: Returns an iterator that points to the first of the newly
inserted elements.
Syntax:
deque_name.insert (iterator position, const value_type& val)
or
deque_name.insert (iterator position, size_type n, const value_type& val)
or
deque_name.insert (iterator position, InputIterator first, InputIterator last)
Parameters: The function accepts four parameters which are specified as below:
position – Specifies the position where the element/elements are to be inserted.
val – specifies the value to be assigned to newly inserted element.
n – specifies the number of elements to insert. Each element is initialized to a copy of val.
first, last – specifies the iterators specifying a range of elements which is to be inserted. The range
includes all the elements between first and last, including the element pointed by first but not the
one pointed by last.
deque rbegin() function in C++ STL: Returns a reverse iterator which points to the last
element of the deque (i.e., its reverse beginning).
deque rend() function in C++ STL: Returns a reverse iterator which points to the position
before the beginning of the deque (which is considered its reverse end).
deque cbegin() in C++ STL: Returns a constant iterator pointing to the first element of the
container, that is, the iterator cannot be used to modify, only traverse the deque.
deque max_size() function in C++ STL: Returns the maximum number of elements that a
deque container can hold.
deque assign() function in C++ STL: Assign values to the same or different deque
container.
1. Syntax:
deque_name.assign(size, val)
Parameters: The function accepts two parameters which are described below:
size: it specifies the number of values to be assigned to the container.
val: it specifies the value to be assigned to the container.
Return Value: The function returns nothing.
2. Syntax:
deque1_name.assign(iterator1, iterator2)
Parameters: The function accepts two parameters which are described below:
iterator1: it specifies the iterator which points to the starting element of container(deque, array, …)
whose elements are to be transferred to deque1.
iterator2: it specifies the iterator which points to the last element of a container(deque, array, …)
whose elements are to be transferred to deque1
Return Value: The function returns nothing.
deque resize (n) function in C++ STL: Function which changes the size of the deque to n.
deque::push_front (val) in C++ STL: This function is used to push elements into a deque
from the front.
deque::push_back (val) in C++ STL: This function is used to push elements into a deque
from the back.
deque::pop_front() and deque::pop_back() in C++ STL: pop_front() function is used to
pop or remove elements from a deque from the front. pop_back()function is used to pop
or remove elements from a deque from the back.
deque::front() and deque::back() in C++ STL: front() function is used to reference the first
element of the deque container. back() function is used to reference the last element of
the deque container.
deque::clear() and deque::erase() in C++ STL: clear() function is used to remove all the
elements of the deque container, thus making its size 0.
dequename.erase() function is used to remove elements from a container from the
specified position or range.
Syntax :
1. dequename.erase(position)
2. dequename.erase(startingposition, endingposition)
Parameters :
Position of the element to be removed in the form of iterator.
or the range specified using start and end iterator.
deque::empty() and deque::size() in C++ STL: empty() function is used to check if the
deque container is empty or not. size() function is used to return the size of the deque
container or the number of elements in the deque container.
deque::operator= and deque::operator[] in C++ STL:
operator= operator is used to assign new contents to the container by replacing the
existing contents. dequename1 = (dequename2);
operator[] operator is used to reference the element present at position given inside the
operator. dequename[position];
deque::at() and deque::swap() in C++ STL: at() function is used reference the element
present at the position given as the parameter to the function.
dequename.at(position);
swap() function is used to swap the contents of one deque with another deque of same
type and size. dequename1.swap(dequename2);
deque::begin() and deque::end in C++ STL: begin() function is used to return an iterator
pointing to the first element of the deque container. end()function is used to return an
iterator pointing to the last element of the deque container.
deque::emplace_front (val) and deque::emplace_back (val) in C++
STL: emplace_front() function is used to insert a new element into the deque container.
The new element is added to the beginning of the deque. emplace_back() function is
used to insert a new element into the deque container. The new element is added to the
end of the deque.