输入一个字符串,将其逆序后输出

一、学习要点:
1.反向迭代器:
string::reverse_iterator riter;
从尾巴到首部:
rbegin():返回逆向迭代器,指向字符串的最后一个元素;
rend():返回逆向迭代器,指向字符串的第一个元素前面的位置;
二、代码:

#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;
int main(){
    string s;
    cin>>s;
    //数组实现
    for(int i=s.length()-1;i>=0;i--){
        cout<<s[i];
    }
    cout<<endl;
    //逆向迭代器实现
    string::reverse_iterator iter;
    for(iter=s.rbegin();iter!=s.rend();iter++){
        cout<<*iter;
    }
    cout<<endl;
    system("pause");
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/fyf18845165207/article/details/82730208