• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

reverse函数(C++)

武飞扬头像
psudd
帮助1

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 */
}

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgekfbb
系列文章
更多 icon
同类精品
更多 icon
继续加载