C++ Algorithm iter_swap()30 Aug 2024 | 5 min read C++ Algorithm iter_swap() exchanges the elements pointed to by two iterators a and b. SyntaxParametera: One of the forward iterator whose value is to be exchanged. b: Second of the forward iterator whose value is to be exchanged. Return valueNone ComplexityConstant Data racesThe objects pointed to by both iterators are changed. Exception safetyThrows an exception if swap() function has an exception. Please note that invalid parameters cause an undefined behavior. Example 1Let's see the simple example to demonstrate the use of iter_swap(): Output: v1: 1, 6, 3, v2: 4, 5, 2, Example 2Let's see another simple example: Output: 9 1 2 3 4 5 6 7 8 0 In the above example, we have swapped elements from v1 with the help of two iterators, one of them pointing at the beginning of v1 and the other one pointing at the end of v1. Example 3Let's see another simple example to swap the odd places of vector to odd paces of array: Output: Vector : 0 0 0 0 0 0 0 0 0 0 Array : 1 2 3 4 5 6 7 8 9 10 Swapping odd places of vector with odd places of array Vector : 0 2 0 4 0 6 0 8 0 10 Array : 1 0 3 0 5 0 7 0 9 0 Example 4Let's see another simple example: Output: Here are the contents of v1: 1 2 3 4 5 Here are the contents of v2: 2 4 6 8 10 First we swap the end values in v1. Here are the contents of the revised v1: 5 2 3 4 1 Then we swap the middle values in v1 and v2. Here are the contents of the revised v1: 5 2 6 4 1 Here are the contents of the revised v2: 2 4 3 8 10 The above example illustrates the use of the iter_swap() algorithm to swap integer values that are pointed to by two different iterators that may point into the same vector of integers, or into two different vectors of integers. Example 5Let's see another example to illustrate the use of iter_swap(): Output: Before calling iter_swap Numbers { 1 1 2 3 5 8 13 21 } After calling iter_swap Numbers { 21 1 2 3 5 8 13 1 } Next TopicC++ Algorithm |
C++ Algorithm random_shuffle() C++ Algorithm random_shuffle() reorders the elements of a range by putting them at random places. The first version uses an internal random number generator and the second version uses a random number generator which is a special kind of function object that is explicitly...
4 min read
C++ Algorithm Function adjacent_find() C++ Algorithm adjacent_find() function performs a search operation on the range [first, last] for the very first occurrence of two consecutive matching elements. If such elements are found then an iterator to the first element of the two is returned. Otherwise, the last...
1 min read
C++ Algorithm Function find_if_not() C++ Algorithm find_if_not()function returns the value of the first element in the range for which the pred value is false otherwise the last element of the range is given. Syntax template <class InputIterator, class UnaryPredicate> InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred); Parameter first: It specifies the...
2 min read
C++ Algorithm swap_ranges() C++ Algorithm swap_ranges() exchanges the elements in the range [first1, last2) with the elements present in the range starting from first2. In short we can say that, swap_ranges() swaps the elements of two sequence, that is each element at a position in the first sequence...
5 min read
C++ Algorithm stable_sort() C++ Algorithm stable_sort() function is used to sort the elements in the range [first, last) into ascending order like sort but keeps the order of equivalent elements. The elements are compared using operator < for the first version, and comp for the second version. Syntax template <class...
6 min read
C++ Algorithm upper_bound() C++ Algorithm upper_bound() function is the version of binary search. This function is used to return an iterator pointing to the first element in the range [first, last) that is greater than to the specified value val. The first version uses operator < to compare...
5 min read
C++ Algorithm Function move () C++ Algorithm move()function is used for moving the elements. It accepts three arguments and then moves the elements belonging to the range [first,last) into a range that starts with 'result'. Syntax template<class InputIterator, class OutputIterator> OutputIterator move(InputIterator first, InputIterator last, OutputIterator result); Parameter first: It is...
2 min read
C++ Algorithm remove() C++ Algorithm remove() function is used to eliminate all the elements that are equal to val from a given range [first, last) without disturbing the order of the remaining elements. This function cannot alter the size of the container. It returns an iterator to the...
5 min read
C++ Algorithm Functions is_permutation() C++ Algorithm is_permutation() function compares the elements in both the containers and returns a true value if all the elements in both the containers are found to be matching even if in different order. The first range is from [first1, last1) and the...
1 min read
C++ Algorithm Functions copy_backward() C++ Algorithm copy_backward() function is used for copying of elements in the backward order, it accepts three arguments and then copies the elements belonging to the range [first,last]. The copying of elements begins in the reverse order with termination point at 'result'. Syntax template<class BidirectionalIterator1,...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India