reverse()函數的使用


reverse()函數可以對字符串進行反轉操作,頭文件是#include<algorithm>

容器類型的要用begin()和end()來指定反轉的區域,數組類型的直接用int類型即可

下面貼出示范代碼:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    string s="hello";
    reverse(s.begin(),s.end());
    char c[]="hello";
    reverse(c,c+strlen(c));
    char x[6]={'h','e','l','l','o'};
    reverse(x,x+strlen(x));
    char str[11];
    for(int i=0;i<5;i++)cin>>str[i];
    reverse(str,str+5);
    cout<<s<<endl;
    cout<<c<<endl;
    cout<<x<<endl;
    for(int i=0;i<5;i++)cout<<str[i];
    return 0;
}

輸入"hello"后的輸出結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM