首页 > 其他 > 详细

recerse()函数

时间:2020-09-03 14:02:04      阅读:75      评论:0      收藏:0      [点我收藏+]

recerse() 函数

作用:

  逆序(反转)无论是在C或是C++中用的都特别多,常用于数组,字符串,容器等,其本身的函数参数也不复杂。

  标准C中是没有recerse()函数的,这是C++的一个新增函数,使用需要包含头文件

 

头文件:

#include <algorithm>

 左闭右开区间: reverse函数用于反转在[first,last)范围内的顺序(包括first指向的元素,不包括last指向的元素),reverse函数没有返回值   

template <class BidirectionalIterator>
void reverse (BidirectionalIterator first,BidirectionalIterator last);

 例如,交换vector容器中元素的顺序

    vector<int> v = {5,4,3,2,1};
    reverse(v.begin(),v.end());//v的值为1,2,3,4,5

    还有string类的字符串

    string str="www.mathor.top";
    reverse(str.begin(),str.end());//str结果为pot.rohtam.wwww

 最后给出函数原型,该函数等价于通过调用iter_swap来交换元素位置

template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
    while ((first!=last)&&(first!=--last))
    {
        std::iter_swap (first,last);
        ++first;
    }
}

 

总结:recerse

recerse()函数是C++新定义的一个数组逆序的函数,用指针的交换实现

容器类型的逆序可以用反向迭代器实现。

 

recerse()函数

原文:https://www.cnblogs.com/xiongxinxzy/p/13606825.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!