#include <iostream>
#include "string"
#include <algorithm>//包含STL库提供的一些算法
using namespace std;
bool compare(int a,int b)
{
//return a<b; //升序排列,如果改为return a>b,则为降序
return a>b;
}
void main()
{
int a[10]={1,2,3,4,6,6,3,8,2},i;
for(i=0;i<10;i++)
cout<<a[i]<<endl;
sort(a,a+10);//默认升序排列
for(i=0;i<10;i++)
cout<<a[i]<<endl;
sort(a,a+10,compare);
/*
c.begin() 返回一个迭代器,它指向容器c的第一个元素
c.end() 返回一个迭代器,它指向容器c的最后一个元素的下一个位置
c.rbegin() 返回一个逆序迭代器,它指向容器c的最后一个元素
c.rend() 返回一个逆序迭代器,它指向容器c的第一个元素前面的位置
*/
string str("abedhfxzy");
string s1(str.rbegin(),str.rend());//降序排列
string s2(str.begin(),str.end());//升序排列
}
C++基础3:algorithm库的基础用法
最新推荐文章于 2024-01-25 01:54:35 发布