STL algorithm inplace_merge (24), stl algorithm
Inplace_merge prototype:
Std: inplace_merge
Default (1) |
template <class BidirectionalIterator> void inplace_merge (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last); |
Custom (2) |
template <class BidirectionalIterator, class Compare> void inplace_merge (BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last, Compare comp); |
This function combines the ordered sequences [first, middle) and [middle, last) into an ordered sequence.
The two cells must be sorted and all use the same sorting rule.
Note that first, middle, and last are all in the same container, but they are separated into two intervals.
A simple example:
#include <iostream>#include <vector>#include <array>#include <algorithm>using namespace std;void inplacemerge(){ vector<int> vi{1,5,8,19,1,8,56}; cout<<"at first,vi="; for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";}); cout<<endl; inplace_merge(vi.begin(),vi.begin()+4,vi.end()); cout<<"after inplace_merge(vi.begin(),vi.begin()+4,vi.end());,vi="<<endl;; for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";}); cout<<endl;}
Run:
------------------------------------------------------------------
// For more instructions on writing errors or poor information, you can leave a message below or click the email address in the upper left corner to send an email to me, pointing out my errors and deficiencies, so that I can modify them, thank you for sharing it.
Reprinted please indicate the source: http://blog.csdn.net/qq844352155
Author: unparalleled
Email: coderguang@gmail.com
Yu GDUT
------------------------------------------------------------------
Why does algorithm in STL use global functions instead of container-class member functions?
Because the algorithms in algorithm are cross-container and generic.
It is not limited to a container, but even any data types that implement interfaces.
The basic type of int char can be used, and the custom type of MyType can also be used. Container can also be used
The third parameter question about for_each () of STL algorithm
......, Print <int> );