C++ Algorithm swap_ranges()30 Aug 2024 | 4 min read 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 is replaced by the element at the same position in the second sequence and vice-versa is also possible. SyntaxParameterfirst1: A forward iterator pointing to the first position of the first range whose elements is to be swapped. last1: A forward iterator pointing to one past the final position of the first range whose elements are to be swapped. first2: A forward iterator pointing to the first position of the second range whose elements is to be swapped. Return valueswap_ranges() returns an iterator pointing to the end of the second range whose elements are to be exchanged. ComplexityThe complexity is linear between first and last. Perform a swap operation for each element in the range. Data racesThe objects in both ranges are modified. Exception safetyException throws if either an element assignment or an operation on iterators throws. Example 1Let's see the simple example to swap the element of two vectors with the given range: Output: v1: 4, 5, 6, v2: 1, 2, 3, 7, 8, In the above example, elements of vector v1 is swapped by vector v2 from range begin to end of v1. Example 2Let's see a simple example to exchange the contents of two vectors: Output: 1 2 3 4 5 5 6 7 8 9 0 1 2 3 4 6 7 8 9 10 In the above example, swap_range() function swaps the first five element of vector v by the elements of vector c. Example 3Let's see a simple example to swap the contents of vector and deque: Output: Vector v1 is: ( 0 1 2 3 4 5 ). Deque d1 is: ( 6 6 6 6 6 6 ). After the swap_range vector v1 is: ( 6 6 6 6 6 6 ). After the swap_range deque d1 is: ( 0 1 2 3 4 5 ). In the above example, elements of vector and deque are exchanged to each other. Example 4Let's see another simple example: Output: s1 = Betty Botter bought some butter s2 = But she said the butter was bitter s3 = So she got some better butter s4 = to make the bitter butter better Jumble them up!!! s1 = But she said thught some butter s2 = Betty Botter boe butter was bitter s3 = to make the bit better butter s4 = So she got someter butter better Next TopicC++ Algorithm |
C++ Algorithm Function find_first_of() C++ Algorithm find_first_of() function compares the values stored in two containers i.e [first1, last1) and [first2, last2). If an element similar to the one in the range [first2, last2) is found in [first1, last1) then an iterator to that element is returned by...
2 min read
C++ Algorithm Function count() C++ Algorithm count()function accepts 'val' as argument and compares for the occurrence of element 'val' in the range. The number of occurrence of that element is returned. Syntax template <class InputIterator, class T> typename iterator_traits<InputIterator>::difference_type count (InputIterator first, InputIterator last, const T& val); Parameter first: It is...
1 min read
C++ Algorithm Function count_if () C++ Algorithm count_if() function has a 'pred' value and returns the count of the elements in the range [first,last) for which the value of pred is true. Syntax template <class InputIterator, class UnaryPredicate> typename iterator_traits<InputIterator>::difference_type count_if(InputIterator first, InputIterator last,UnaryPredicate pred); Parameter first: It is an input iterator...
1 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 remove_copy() C++ Algorithm remove_copy() function is used to copy all elements which are not equal to val from the range [first, last) to provide result without disturbing the order of the remaining elements. This function cannot alter the size of the container. It returns an iterator...
4 min read
C++ Algorithm transform() C++ Algorithm transform() function is used in two different ways: 1.unary operation:- This method performs unary operation op on the elements in range [first1, last1] and stores the result in range starting from result. This transform() applies a function to each element of a range: 2.Binary...
5 min read
C++ Algorithm Functions copy_n() C++ Algorithm copy_n() function specifies the number of elements to be copied into the new container. The function is used to copy n elements of the container [first,last) into a different container starting from result. Syntax template<class InputIterator, class Size, class OutputIterator> OutputIterator copy_n(InputIterator first, Size...
1 min read
C++ Algorithm replace() C++ Algorithm replace() function is used to replace all value equal to old_value by the value new_value in the range [first, last). This function examines each element in the range and replaces it if it matches a specified value. Syntax template <class ForwardIterator, class T> void...
4 min read
C++ Algorithm Function find_if() C++ Algorithm find_if() function returns the value of the first element in the range for which the pred value is true otherwise the last element of the range is given. Syntax template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred); Parameter first: It specifies...
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
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