STL variable Algorithm (i.)

Source: Internet
Author: User

The C + + STL algorithm is a set of template functions that can modify the data of container elements, which can be used for copying, exchanging, replacing, filling, removing, rotating, etc. of sequence containers. These algorithms have a high demand for iterators, with specific iterator types depending on the algorithm, or forward iterators, or bidirectional iterators, or random iterators, to provide the iterator operations required by the algorithm. When applying a variable algorithm, check that the container's iterators meet the requirements and prevent compilation errors.

Element Replication Copy

C++STL provides a copy algorithm for the element copy between containers, copying the elements of the iteration interval [first,last] to the interval [result,result+ (Last-first)) given by the copy target iterator result, as follows:

template <classclass OutputIterator>  OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);

The specific code is as follows:

template<classclass OutputIterator>  OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result){  while (first!=last) {    *result = *first;    ++result;     ++first;  }  return result;}  
//Instance#include <algorithm>#include <vector>#include <list>#include <iostream>using namespace STD;voidPrintintx) {cout<< x <<"  ";}intMainvoid){//initialization vector v     vector<int>V V.push_back (1); V.push_back (3); V.push_back (5);//Initialize bidirectional linked list L     list<int>L L.push_back (2); L.push_back (4); L.push_back (6); L.push_back (8); L.push_back (Ten);//Copy V to lCopy (V.begin (), V.end (), L.begin ());//List L Print L 3 5 8For_each (L.begin (), L.end (), print);cout<<endl;return 0;}

Reverse Replication Copy_backward

Similar to the copy algorithm, the Copy_backward algorithm also copies an iterator interval element to another iterator interval, except that the copy is copied from the last element until the first element is copied.
It uses the following prototype, which copies the elements of the iterator interval [first,last] to the interval [result-(Last-first)) where result is the ending position, and the order is * (last-1) copied to * (result-1), * (last-2) Copy to * (Result-2), * (last-3) to * (result-3) 、......

//copy_backward算法函数的代码template<classclass BidirectionalIterator2>  BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,                                         BidirectionalIterator1 last,                                         BidirectionalIterator2 result ){  while (last!=first) *(--result) = *(--last);  return result;}
//Test Cases#include <algorithm>#include <vector>#include <iostream>using namespace STD;voidPrintintx) {cout<< x <<"';}intMainvoid){ vector<int>VTen); for(unsigned intI=0; I<v.size (); i++) v[i]=i+1; Copy_backward (V.begin (), V.begin () +3, V.end ()); For_each (V.begin (), V.end (), print);cout<< Endl; System"Pause");return 0;}

Element Swap Swap

Although the SWAP function is provided internally in most containers, C++STL provides a more general swap algorithm for exchanging two elements in a more general form of iterators.

//swap算法函数的代码template <classvoid swap ( T& a, T& b ){  T c(a); a=b; b=c;}
//Instance#include <algorithm>#include <iostream>using namespace STD;intMainvoid){intA =5;intb = -;cout<<"Before Exchange"<<"a ="<< a <<"b ="<< b << Endl; Swap (A, b);cout<<"After Exchange"<<"a ="<< a <<"b ="<< b << Endl;return 0;}

Iterator Interchange Iter_swap

The ITER_SWAP algorithm is an iterative form of the swap algorithm, making the switching algorithm easier to use in general containers.

template <classclass ForwardIterator2>  void iter_swap (ForwardIterator1 a, ForwardIterator2 b){  swap (*a, *b);}
//Example#include <algorithm>#include <iostream>using namespace STD;intMainvoid){intA =5;intb = -;cout<<"Before Exchange"<<"a ="<< a <<"b ="<< b << Endl; Iter_swap (&a, &b);cout<<"After Exchange"<<"a ="<< a <<"b ="<< b << Endl; System"Pause");return 0;}

Interval element Exchange Swap_ranges

Intuitively, the swap_ranges algorithm is used to exchange two iterator interval elements. It is used as a prototype to exchange elements of the [first1,last1] iterator interval with the [first2,first2+ (last1-first1)) iterator interval elements, where *first1 and *first2 are exchanged, * (first1+1) and * ( FIRST2+1) Exchange 、......、 * (LAST1-1) and * (first2+ (LAST1-FIRST1)-1).

template<classclass ForwardIterator2>  ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1,                                ForwardIterator2 first2){  while (first1!=last1) {    swap (*first1, *first2);    ++first1; ++first2;  }  return first2;}
//Instance#include <algorithm>#include <vector>#include <iostream>using namespace STD;voidPrintintx) {cout<< x <<" ";}intMainvoid){ vector<int>V1, v2; V1.push_back (1); V1.push_back (3); V1.push_back (5); V2.push_back (2); V2.push_back (4); V2.push_back (6);//Print V1, v2    cout<<"Before the exchange, v1="; For_each (V1.begin (), V1.end (), print);cout<<"v2="; For_each (V2.begin (), V2.end (), print);cout<< Endl;//Exchange V1, v2Swap_ranges (V1.begin (), V1.end (), V2.begin ());//Print V1, v2    cout<<"After the exchange, v1="; For_each (V1.begin (), V1.end (), print);cout<<"v2="; For_each (V2.begin (), V2.end (), print);cout<< Endl;return 0;}

To be Continued ...

Reprint Please specify source: http://blog.csdn.net/lsh_2013/article/details/46854397

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

STL variable Algorithm (i.)

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.