声明一个整形数组,用c++STL库中的查找算法进行数据查找,然后应用排序算法对数据进行升序和降序排序。

查找算法find()排序算法sort()... 查找算法find()
排序算法sort()
展开
 我来答
sx_liang
2011-05-31 · TA获得超过214个赞
知道小有建树答主
回答量:233
采纳率:0%
帮助的人:200万
展开全部
#pragma warning(disable: 4786)

#include <algorithm>
#include <iostream>
#include <iterator>
#include <fstream>
#include <functional>
using namespace std;
/* 这是MSDN中的接口:
template<class InputIterator, class T> inline
InputIterator find(InputIterator first, InputIterator last, const T& value)
*/
/*
int main()
{
const int ARRAY_SIZE = 8 ;
int IntArray[ARRAY_SIZE] = { 1, 2, 3, 4, 4, 5, 6, 7 } ;

int *location ;
int i ;
int value = 4 ;

cout << "IntArray { " ;
for(i = 0; i < ARRAY_SIZE; i++)
cout << IntArray[i] << ", " ;
cout << " }" << endl ;

location = find(IntArray, IntArray + ARRAY_SIZE, value) ;

if (location != IntArray + ARRAY_SIZE)
cout << "First element that matches " << value
<< " is at location " << location - IntArray << endl;
else
cout << "The sequence does not contain any elements"
<< " with value " << value << endl ;
return 0;
}
*/

/* MSDN中两种 sort 接口,下边的那个需要一个function object ....
template<class RanIt>
void sort(RanIt first, RanIt last);
template<class RanIt, class Pred>
void sort(RanIt first, RanIt last, Pred pr);
*/

bool cmp (int a,int b) //从大到小排序,其实就是重载 sort 默认的 < 符号;sort 默认的是从小到大。。。
{
return a>b;
}

int main()
{
const int ARRAY_SIZE = 8 ;
int IntArray[ARRAY_SIZE] = { 3, 2, 1, 4, 4, 7, 6, 5 } ;

sort(IntArray,IntArray + ARRAY_SIZE, cmp);

ostream_iterator<int> os (cout," ");
copy (IntArray,IntArray + ARRAY_SIZE, os);

cout<<endl<<endl;

sort(IntArray,IntArray + ARRAY_SIZE);
copy (IntArray,IntArray + ARRAY_SIZE, os);
return 0;
}
//都试了一下,应该没有什么问题的。。。你看看是不是满足你要求。。。呵呵。。期待交流。。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式