2019-03-29 20:15:12 qq_37131037 阅读数 768

reverse 在 库中，用来翻转 [a,b) 之间的内容

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

``````int a[] = {1,2,3 ,4,5,6,7};
reverse(&a[0],&a[2]);
for (int i = 0; i < 7; i++) {
cout << a[i] << endl;
}
``````

``````2
1
3
4
5
6
7
``````

c++ reverse()函数 相关内容

2019-05-28 12:56:27 xiangxianghehe 阅读数 473

``````#include <algorithm>
``````

reverse函数用于反转在[first,last)范围内的顺序（包括first指向的元素，不包括last指向的元素），reverse函数没有返回值

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

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

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

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

``````#include <iostream>
#include <algorithm>
#include <vector>

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

int main()
{
int myints[] = {1,2,3,1};
int len = sizeof(myints)/sizeof(int);
vector<int> vec(myints, myints + len);
my_reverse(vec.begin(), vec.end());
for(int x: vec)
cout<<x<<",";

return 0;
}

``````

c++ reverse()函数 相关内容

2018-11-26 10:57:16 PiscesDAI 阅读数 3631

标准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;
}
}``````

c++ reverse()函数 相关内容

2019-04-14 09:29:25 m0_43382549 阅读数 257

erase函数的原型：
iterator erase ( iterator position );

string& erase ( size_t pos = 0, size_t n = npos );

iterator erase ( iterator first, iterator last );

erase(position);删除position处的一个字符(position是个string类型的迭代器)

erase(pos,n); 删除从pos开始的n个字符，比如erase(0,1)就是删除第一个字符

erase(first,last);删除从first到last之间的字符（first和last都是迭代器）

`````` #include <iostream>
#include <string>
using namespace std;

int main ()
{
string str ("I Love YX who are very fat!!!");
string::iterator it;

// 用法(1)
it=str.begin()+6;
str.erase (it);
cout << str << endl;

// 用法(2)
str.erase (13,8);
cout << str << endl;

// 用法(3)
str.erase (str.begin()+3, str.end()-4);
cout << str << endl;
return 0;
}
``````

output

``````I LoveYX whoy fat!!!
I Love YX whoy fat!!!
I Lt!!!
``````

reverse（）函数将左闭右开区间内的元素全部逆序；
reverse_copy()会将(sourceBeg,sourceEnd,destBeg)中的源区间[sourceBeg,sourceEnd)内的元素复制到"以destBeg起始的目标区间"，并在复制过程中颠倒顺序；

``````
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a[50];
int b[50];
for(int i=0;i<10;i++)
{
a[i]=i;
cout<<a[i]<<" ";
}
cout<<endl;

reverse(a,a+10);         //第二个参数是数组最后一个元素的下一个地址
for(int i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;

reverse(a,a+5);
for(int i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;

reverse_copy(a,a+10,b); //倒序放入b数组中
for(int i=0;i<10;i++)
cout<<b[i]<<" ";
return 0;
}

``````

c++ reverse()函数 相关内容

2017-09-23 19:16:03 gl486546 阅读数 13750

C++ < algorithm > 中定义的reverse函数用于反转在[first,last)范围内的顺序

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

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

``````string str="C++REVERSE";
reverse(str.begin(),str.end());//str结果为ESREVER++C``````

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

c++ reverse()函数 相关内容