Move prototype:
STD: Move
template <class InputIterator, class OutputIterator> OutputIterator move (InputIterator first, InputIterator last, OutputIterator result);
This function moves the elements in the specified range to the starting position from result.
After moving, the specific implementation of the elements in the range of [first, last) is determined by the compiler.
The result cannot be in the range of [first, last.
The returned value is the iterator of the last element to be overwritten in the result.
The behavior is similar:
3456789
|
template<class InputIterator, class OutputIterator> OutputIterator move (InputIterator first, InputIterator last, OutputIterator result){ while (first!=last) { *result = std::move(*first); ++result; ++first; } return result;}
|
A simple example:
#include <iostream>#include <algorithm>#include <vector>using namespace std;void mmove(){ vector<int> vi{3,5,1,1}; vector<int> v2{3,5,5,1}; vector<int> result{1,2,3,4,5,6,7}; cout<<"vi="; for(int i:vi) cout<<i<<" "; cout<<endl; cout<<"result="; for(int i:result) cout<<i<<" "; cout<<endl; auto it=move(vi.begin(),vi.end(),result.begin()); cout<<"after auto it=move(vi.begin(),vi.end(),result.begin())"<<endl; cout<<"vi="; for(int i:vi) cout<<i<<" "; cout<<endl; cout<<"result="; for(int i:result) cout<<i<<" "; cout<<endl; cout<<"it="<<*it<<endl; cout<<"v2="; for(int i:v2) cout<<i<<" "; cout<<endl; auto it2=move(v2.begin()+1,v2.end(),v2.begin()); cout<<"after auto it2=move(v2.begin()+1,v2.end(),v2.begin());"<<endl; cout<<"v2="; for(int i:v2) cout<<i<<" "; cout<<endl; if(it2==v2.end()) cout<<"it2==v2.end()"<<endl; else cout<<"it2="<<*it2<<endl;}
Run:
As you can see, if the result is within the range of [first, last), the original element will be rewritten.
In particular, it is possible that the elements you move are the elements you have modified, resulting in unwanted behavior.
Move_backward prototype:
STD: move_backward
template <class BidirectionalIterator1, class BidirectionalIterator2> BidirectionalIterator2 move_backward (BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result);
This function moves the elements in the range [first, last) from the back to the result position, and the result overwrites the order in reverse order.
This function returns the result of the target range. The first element to be overwritten is displayed in sequence (that is, the first element to be overwritten rather than the first element to be overwritten) (See the following example ).
The behavior is similar:
template<class BidirectionalIterator1, class BidirectionalIterator2> BidirectionalIterator2 move_backward ( BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result ){ while (last!=first) *(--result) = std::move(*(--last)); return result;}
A simple example:
#include <iostream>#include <algorithm>#include <vector>using namespace std;void mmovebackward(){ vector<int> vi{99,5,1,1}; vector<int> result{1,2,88,4,5,6,7}; cout<<"vi="; for(int i:vi) cout<<i<<" "; cout<<endl; cout<<"result="; for(int i:result) cout<<i<<" "; cout<<endl; auto it=move_backward(vi.begin(),vi.end(),result.end()); cout<<"after auto it=move_backward(vi.begin(),vi.end(),result.end())"<<endl; cout<<"vi="; for(int i:vi) cout<<i<<" "; cout<<endl; cout<<"result="; for(int i:result) cout<<i<<" "; cout<<endl; cout<<"it="<<*it<<endl;}
Run:
Note that the returned result is an iterator pointing to the 99 element in the result, instead of the first element in the last range!
------------------------------------------------------------------
// 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: [email protected]
2014-9-19
Yu gdut
---
STL algorithm mov, move_backward (38)