1.reverse函数介绍
1.1 函数功能介绍
将容器[first, last )范围内的元素颠倒顺序放置
1.2 函数参数介绍
first_iterator, last_iterator 为函数两个参数,分别输入容器或者数组初始位置和结束位置的迭代器位置
1.3 函数细节注意
a. 头文件 “algorithm”
b. 使用该函数的容器必须有内置的迭代器函数或者有指针指向,例如queue容器和stack容器没有内置的迭代器函数就没有对应的参数输入是无法使用的
2.reverse函数代码运行
2.1 一般数组转置举例
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int a[3] = {
0, 1, 2};
int main(){
for(int i = 0; i < 3; i ++ ) cout << a[i] << ' ';
cout << endl;
reverse(a, a + 3);
for(int i = 0; i < 3; i ++ ) cout << a[i] << ' ';
cout << endl;
/*
0 1 2
2 1 0
*/
}
2.2 常用STL容器举例,string,vector等
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
string a = "123";
reverse(a.begin(), a.end());
cout << a << endl;
/* 输出: 321 */
}
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
vector<int> v = {
1, 2, 3};
for(int i = 0; i < 3; i ++ ) cout << v[i] << ' ';
cout << '\n';
reverse(v.begin(), v.end());
for(int i = 0; i < 3; i ++ ) cout << v[i] << ' ';
/* 输出:
1 2 3
3 2 1 */
}