c++中逆序reverse和sort的使用

reverse可以进行逆序,包含于中#include 。
sort可以进行升序排列

#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
int main ()
{
    
    string key;
    cin>>key;
    reverse(key.begin(),key.end());
    //翻转的是起点和终点这段区间 和sort用法类似;[st,ed)
    cout<<key<<endl;
    
       
    return 0;
}
#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
int main ()
{
		char str[50];
        scanf("%s",str);
        int len=strlen(str);
        reverse(str,str+len);
        /**
        对于定义的char型的话呢,就是填充的是翻转的位置,从哪里到哪里;类型和sort一样;
        */
        cout<<str<<endl;
    
       
    return 0;
}

#include<cstdio>
#include<string.h>
using namespace std;
int main ()
{
    char str[100];
    cin>>str;
	strrev(str);
    cout<<str<<endl;
 
    return 0;
}

1.strrev函数只对字符数组有效,对string类型是无效的。

2.reverse函数是反转容器中的内容,对字符数组无效。

3此外对结构体中的元素使用sort,需要另外构造一个比较函数
对结构体使用sort如下

#include<iostream>
#include<algorithm>
using namespace std;
struct data{
	int n;
	int m;
};
bool comparison(data x,data y){//传入对应结构体元素 
	return x.n<y.n;//需要比较的参数域 
}//使用<为升序;使用>为降序 
int main(){
	int n;
	cin>>n;
	data a[n];	
	for(int i=0;i<n;i++){//输入 
		cin>>a[i].n>>a[i].m;
	}
	
	sort(a,a+3,comparison);
	
	for(int i=0;i<n;i++){	//输出 
		cout<<a[i].n<<' '<<a[i].m<<' '<<endl;
	}
	return 0; 
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值