C++ Algorithm swap() Function12 Nov 2024 | 2 min read C++ Algorithm swap() function swaps or say interchanges the values of two containers under reference. SyntaxParametera: It is the first container with some value. b: It is another container with some value. Return valueThe function only swaps the values of two containers and does not return something. Example 1Output: sg contains: 14 14 14 14 14 14 Example 2Output: Value of ss before swapping: 9 Value of sg before swapping: 14 Value of ss after swapping: 14 Value of sg after swapping: 9 ComplexityFor arrays the function has N complexity as the operation of swapping is individually performed on each element. For non array the function has constant complexity. Data racesBoth the containers are undergo modification ExceptionsThe function throws an exception if any of the container elements throws one. Next TopicC++ Algorithm fill() |
algorithm replace_copy_if() function
C++ Algorithm replace_copy_if() C++ Algorithm replace_copy_if() function is used to make a copy of the range [first, last) to the range beginning at result, placing those for which pred returns true by new_value. It uses predicate pred instead of operator== to compare the elements. This function examines each...
8 min read
algorithm partition() function
C++ Algorithm partition() C++ Algorithm partition() function is used to make partition the elements on the basis of given predicate (condition) mentioned in its arguments. If the container is partitioned then this function returns true, else returns false. Syntax template <class BidirectionalIterator, class UnaryPredicate> BidirectionalIterator partition (BidirectionalIterator first, BidirectionalIterator last, UnaryPredicate...
5 min read
algorithm all_of() function
C++ Algorithm Functions all_of() C++ Algorithm all_of() function returns a true value if the value of 'pred' argument is true. The value should be true for all elements in the range [first, last]. Syntax template <class InputIterator, class UnaryPredicate> bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred); Parameter first: It specifies the...
1 min read
algorithm replace_if() function
C++ Algorithm replace_if() C++ Algorithm replace_if() function is used to assign new_value to all the elements in the range [first, last) for which pred predicate returns true. This function examines each element in a range and replaces it if it satisfies a specified predicate. Syntax template <class ForwardIterator,...
4 min read
algorithm copy_n() function
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
algorithm rotate_copy() function
C++ Algorithm rotate_copy() C++ Algorithm rotate_copy() function is used to make a rotated copy of the elements in the range [first, last). The sequence will start at the element in the middle of the source sequence and the last element will be followed by first. It appends the...
7 min read
algorithm nth_element() function
C++ Algorithm nth_element() C++ Algorithm nth_element() function is used to sort the elements between the first and nth element in ascending order and element between nth and last are not sorted. However, no element in between nth and last is smaller than an element between first and...
6 min read
algorithm count() function
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
algorithm upper_bound() function
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
algorithm copy_if() function
C++ Algorithm Function copy_if() C++ Algorithm copy_if() function is used to copy the elements of the container [first,last] into a different container starting from result for which the value of pred is true. Syntax template<class InputIterator, class OutputIterator, class UnaryPredicate> OutputIterator copy_if(InputIterator first, InputIterator last, OutputIterator result,UnaryPredicate pred); Parameter first: It is...
1 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