C++ STL(14):排列算法

29 篇文章 0 订阅
29 篇文章 1 订阅
#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群:

328243383(1群)



点击群号或者扫描二维码即可加入QQ群:

180479701(2群)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值