int cmp( const int &a, const int &b)
{
if( a > b )
return 1;
else
return 0;
}
int main()
{
int a[]={3,4,12,1,23,2,1,4};
sort(a,a8); //是对数组a正序排序
for(int i=0;i<8;i)
{
cout<<a[i]<<" ";
}
cout<<endl;
sort(a,a8,cmp); //是对数组a降序排序
for(int i=0;i<8;i)
{
cout<<a[i]<<" ";
}
return 0;
}
输出结果:
1 1 2 3 4 4 12 23
23 12 4 4 3 2 1 1
bool cmp(int a,int b)
{
return a>b;
}
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int a[] = {2,1,3};
while (next_permutation(a, a3))
{
cout << a[0] << " " << a[1] << "" << a[2] << endl;
}
return 0;
}
输出结果:2 1 3
2 3 1
3 1 2
3 2 1
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
string str;
cin >> str;
sort(str.begin(), str.end());
do
{
cout << str <<" ";
}while (next_permutation(str.begin(), str.end()));
return 0;
}
输入:abc
输出结果:abc acbbac bca cab cba
与next_permutation相同
查找两个相邻的等价元素
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool myfunction (int i, int j)
{
return (i==j);
}
int main ()
{
int myints[] = {10,20,30,30,20,10,10,20};
vector<int> myvector (myints,myints8);
vector<int>::iterator it;
it = adjacent_find (myvector.begin(), myvector.end());
if (it!=myvector.end())
cout << "the first consecutive repeated elements are: "<< *it << endl;
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-34960-18.html
强行驱离
正愁没理由进驻军事部署的
高蛋白质呀
行贿最多的就是浙商