We are already familiar with swap and iter_swap. Next, let's look at the interval element exchange algorithm swap_ranges, which is used to exchange the interval elements of two iterators. Its usage prototype is as follows, the iterator range [first1, last1) elements, with the iterator range [first2, first2 + (last1-first1 )) in the iterator range element exchange, * first1 and * first2 are exchanged, * (first + 1) and * (first2 + 1) are exchanged ,... * (last1-1) and * (first2 + last1-fitst1)-1) Exchange.
Function prototype:
template<class ForwardIterator1, class ForwardIterator2> ForwardIterator2 swap_ranges ( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2 ){ while (first1!=last1) swap(*first1++, *first2++); return first2;}
Parameter description:
First1, last1
Indicates the first iterator range [first1, last1) for exchange ).
First2
It indicates the iterator position of the first element of the second iterator range to be exchanged. The number of elements in the iterator range is equal to that of the first iterator.
Program example:
/*************************************** * **************************** Copyright (c) jerry Jiang ** file name: swap_ranges.cpp * Author: Jerry Jiang * Create Time: 22:22:18 * mail: jbiaojerry@gmail.com * blog: http://blog.csdn.net/jerryjbiao ** description: A simple program interprets the C ++ STL algorithm series's 17 * variable algorithm: interval element exchange swap_ranges ************************************ * *****************************/# InCl Ude <iostream> # include <algorithm> # include <vector> using namespace STD; int main () {vector <int> first (5, 10); // first: 10 10 10 10 10 10 vector <int> second (5, 33); // Second: 33 33 33 33 vector <int >:: iterator it; swap_ranges (first. begin () + 1, first. end ()-1, second. begin (); // print out results of swap: cout <"first contains:"; for (IT = first. begin (); it! = First. end (); ++ it) cout <"<* it; cout <" \ nsecond contains: "; for (IT = second. begin (); it! = Second. End (); ++ it) cout <"" <* it; cout <Endl; return 0 ;}
**************************************** **************************************** **************************************** *******
C ++ classic bibliography index and resource download:Http://blog.csdn.net/jerryjbiao/article/details/7358796
**************************************** **************************************** **************************************** ********