
1个回答
展开全部
#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;
}
//都试了一下,应该没有什么问题的。。。你看看是不是满足你要求。。。呵呵。。期待交流。。
#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;
}
//都试了一下,应该没有什么问题的。。。你看看是不是满足你要求。。。呵呵。。期待交流。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询