reverse函数(C++)
时间:2024-03-29 20:50:24 来源:网络cs 作者:淼淼 栏目:培训机构 阅读:
文章目录
1.reverse函数介绍2.reverse函数代码运行
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 */}
本文链接:https://www.kjpai.cn/news/2024-03-29/150571.html,文章来源:网络cs,作者:淼淼,版权归作者所有,如需转载请注明来源和作者,否则将追究法律责任!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。