Simple Program interpretation of C ++ STL algorithm series 17: swap_ranges

Source: Internet
Author: User

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

**************************************** **************************************** **************************************** ********

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.