STL_알고리즘교환 (swap ranges)

C + + Primer 학습 중...  나의 학습 과정 을 간단하게 기록 하 다. (코드 위주) 모든 용기 에 swap 적용ranges(b,e,b2)  //장점: 부분 교환 이 가능 하고 서로 다른 유형의 용기 간 에 주 의 를 교환 할 수 있 습 니 다. 다음 두 가지 방법 도 교환 알고리즘 입 니 다.    1. 용기 의 swap () 멤버 함수    2. 할당 작업
/**------http://blog.csdn.net/u010579068------**/
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
using namespace std;

/*****************************************
//      
swap_ranges(b,e,b2)  //       +            

  :            
    1、   swap()    
    2、    
*****************************************/
/**----------------------------------------------------------------------------------

----------------------------------------------------------------------------------**/
/*************************************************************************************
std::swap_ranges                                                        algorithm
--------------------------------------------------------------------------------------
template < class ForwardIterator1, class ForwardIterator2 >
  ForwardIterator2 swap_ranges ( ForwardIterator1 first1, ForwardIterator1 last1,
                                 ForwardIterator2 first2 );

//eg:
template<class ForwardIterator1, class ForwardIterator2>
  ForwardIterator2 swap_ranges ( ForwardIterator1 first1, ForwardIterator1 last1,
                                 ForwardIterator2 first2 )
{
  while (first1!=last1) swap(*first1++, *first2++);
  return first2;
}
*************************************************************************************/


int main()
{
    vector<int> first (5,10);        //  first: 10 10 10 10 10
    vector<int> second (7,33);       // second: 33 33 33 33 33 5

    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 << "
second contains:"; for (it=second.begin(); it!=second.end(); ++it) cout << " " << *it; cout << endl; cout << endl; /**-------------------------------------------------------------------**/ //swap_ranges , , int a[]={1,2,3,4,5,6,7}; int b[]={9,10,11,12}; vector<int> third (a,a+7); deque<int> fourth(b,b+4); deque<int>::iterator iter; auto riter=swap_ranges(third.begin()+2, third.end()-3, fourth.rbegin()+1); cout << "third contains:"; for (it=third.begin(); it!=third.end(); ++it) cout << " " << *it; cout << "
fourth contains:"; for (iter=fourth.begin(); iter!=fourth.end(); ++iter) cout << " " << *iter; cout << endl; cout<<" : "<<*riter<<endl; return 0; } /******* Output: first contains: 10 33 33 33 10 second contains: 10 10 10 33 33 33 33 third contains: 1 2 11 10 5 6 7 fourth contains: 9 4 3 12 : 9 **/

좋은 웹페이지 즐겨찾기