#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
//排列算法
int main()
{
/************************************************************************/
//reverse
/************************************************************************/
//reverse:将范围[first,last)进行就地反转
/*
template<class BidirectionalIterator>
void reverse(
BidirectionalIterator _First,
BidirectionalIterator _Last
);
*/
std::vector<int> iv;
iv.push_back(0);
iv.push_back(1);
iv.push_back(2);
iv.push_back(3);
std::cout << "反转之前iv: ";
//0 1 2 3
std::copy(iv.begin(), iv.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
std::reverse(iv.begin(), iv.end());
std::cout << "反转之前iv: ";
//3 2 1 0
std::copy(iv.begin(), iv.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
/************************************************************************/
//reverse_copy
/************************************************************************/
//reverse_copy:将范围[first,last)复制到另一个范围上,并反转
/*
template<class BidirectionalIterator, class OutputIterator>
OutputIterator reverse_copy(
BidirectionalIterator _First,
BidirectionalIterator _Last,
OutputIterator _Result
);
*/
std::vector<int> iv2;
std::reverse_copy(iv.begin(), iv.end(), std::back_inserter(iv2));
std::cout << "iv: ";
//3 2 1 0
std::copy(iv.begin(), iv.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
std::cout << "iv2: ";
//0 1 2 3
std::copy(iv2.begin(), iv2.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
/************************************************************************/
//rotate
/************************************************************************/
//rotate: 将[first,middle)和[middle,last)互换
/*
template<class ForwardIterator>
void rotate(
ForwardIterator _First,
ForwardIterator _Middle,
ForwardIterator _Last
);
*/
int A[] = { 1, 2, 3, 4, 5, 6, 7 };
const int n = sizeof A / sizeof A[0];
int *pFirst = std::begin(A);
int *pLast = std::end(A);
//1 2 3 4 5 6 7
std::copy(pFirst, pLast, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
int *pMid = pFirst;
std::advance(pMid, 1);
std::rotate(pFirst, pMid, pLast);
//2 3 4 5 6 7 1
std::copy(pFirst, pLast, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
pMid = pLast;
std::advance(pMid, -1);
std::rotate(pFirst, pMid, pLast);
//1 2 3 4 5 6 7
std::copy(pFirst, pLast, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
pMid = pFirst;
std::advance(pMid, 3);
std::rotate(pFirst, pMid, pLast);
//4 5 6 7 1 2 3
std::copy(pFirst, pLast, std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
/************************************************************************/
//rotate_copy
/************************************************************************/
//rotate_copy:在rotate之前先copy后再进行rotate,原范围数据顺序不变
/*
template<class ForwardIterator, class OutputIterator>
OutputIterator rotate_copy(
ForwardIterator _First,
ForwardIterator _Middle,
ForwardIterator _Last,
OutputIterator _Result
);
*/
int B[n] = { 1, 1, 1, 2, 2, 2, 2 };
int C[n];
pMid = std::begin(B);
std::advance(pMid, 3);
std::rotate_copy(std::begin(B), pMid, std::end(B), std::begin(C));
//2 2 2 2 1 1 1
std::copy(std::begin(C), std::end(C), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
//下面直接写入到标准输出流上
/*
std::rotate_copy(std::begin(B), pMid, std::end(B),std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
*/
return 0;
}
====================打个广告,欢迎关注====================
QQ: | 412425870 |
csdn博客: | http://blog.csdn.net/caychen |
码云: | https://gitee.com/caychen/ |
github: | https://github.com/caychen |
点击群号或者扫描二维码即可加入QQ群: | |