swap_ranges |
Swap the elements in the range [ first1, last1 ) with the elements in a range of the same size starting at first2 . Return an iterator positioned immediately after the last element in the second range.
#include <algorithm>
template< class ForwardIterator1, class ForwardIterator2 >
ForwardIterator2 swap_ranges
(
ForwardIterator1 first1,
ForwardIterator1 last1,
ForwardIterator2 first2
);
Time complexity is linear. Space complexity is constant.
#include <iostream>
#include <string.h>
#include <algorithm>
void
main()
{
char word1[ 6 ]; ::strcpy( word1, "Fight" );
char word2[ 6 ]; ::strcpy( word2, "Texas" );
cout << word1 << " " << word2 << "\n";
swap_ranges( word1, word1 + ::strlen( word1 ), word2 );
cout << word1 << " " << word2 << "\n";
}
World Hello
Hello World
Copyright©1994-2026 Recursion Software LLC
All Rights Reserved - For use by licensed users only.