0
点赞
收藏
分享

微信扫一扫

sort函数和swap函数

古得曼_63b6 2022-01-31 阅读 124
c++

1.sort函数

sort函数用于C++中,对给定区间所有元素进行排序,默认为升序,也可进行降序排序。sort函数进行排序的时间复杂度为n*log2n,比冒泡之类的排序算法效率要高。

#include<bits/stdc++.h>
using namespace std;
int a[4];
int main()
{
    cin>>a[1]>>a[2]>>a[3];
    sort(a+1,a+4);
    cout<<a[3]<<' '<<a[2]<<' '<<a[1];
    return 0;
}

2.swap函数

在c++中,swap函数可用来进行数字,数组,字符串的交换。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a=1;
    int b=2;
    swap(a,b);
    cout<<a<<endl;
    cout<<b<<endl;
    return 0;
}

举报

相关推荐

0 条评论