您的位置:首页 > 移动开发 > IOS开发

stl sort算法之研究[转载]

2005-03-25 11:10 477 查看
原作者: sudaobo  sudaobo@21cn.com
2004-8-4
sort模板有两种:
---------------------------------------------------------------------
template<class RanIt>
    void sort(RanIt fist, RanIt last);
template<class RanIt, class Pred>
    void sort(RanIt fist, RanIt last, Pred pr);
---------------------------------------------------------------------
第一种模板,sort重排[first,last)之间的元素,产生一个按operate<排列的序列。sort将序列中的元素以升序方式排列。
第二种模板和前一个的行为相似,不过它用pr(X,Y)代替了operate<(x,y)。
例子:
------------------------------
// tmp1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <algorithm> // Include algorithms
#include <iostream>
using namespace std;
bool pr(int s1, int s2)
{
return s1>s2;
}
int main(int argc, char* argv[])
{
vector<int> vec;
vector<int>::iterator i;
vec.push_back (10);
vec.push_back (3);
vec.push_back (7);
sort(vec.begin(), vec.end(),pr); // Sort the vector
for (i = vec.begin(); i != vec.end(); i++)
{
cout<<*i<<endl;
}
return 0;
}
 
例子2:
--------------------------------------------------------------------------------------
// tmp1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <algorithm> // Include algorithms
#include <iostream>
#include <string>
using namespace std;
class myless
{
public:
  bool operator()( const int &a, const int &b) {
    return a < b;
  }
};
int main(int argc, char* argv[])
{
 vector<int> vec;
 vector<int>::iterator i;
 
 vec.push_back (10);
 vec.push_back (3);
 vec.push_back (7);
 sort(vec.begin(), vec.end(), myless()); // Sort the vector
 for (i = vec.begin(); i != vec.end(); i++)
 {
  cout<<*i<<endl;
 }
 return 0;
}
 
例子3:
------------------------------------------------------------------------
// tmp1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <algorithm> // Include algorithms
#include <iostream>
#include <string>
using namespace std;
typedef struct
{
 string first;
 string last;
}NAME;
bool sortbyfirst(const NAME& n1, const NAME& n2)
{
 return (n1.first<n2.first);
}
bool sortbylast(const NAME& n1, const NAME& n2)
{
 return (n1.last<n2.last);
}
int main(int argc, char* argv[])
{
 vector<NAME> contacts;
 vector<NAME>::iterator j;
 
 NAME tmp;
 tmp.first = "liu";
 tmp.last = "bei";
 contacts.push_back(tmp);
 tmp.first = "zhao";
 tmp.last = "yun";
 contacts.push_back(tmp);
 tmp.first = "gun";
 tmp.last = "yu";
 contacts.push_back(tmp);
 tmp.first = "zhang";
 tmp.last = "fei";
 contacts.push_back(tmp);
cout<<"by first:"<<endl;
sort(contacts.begin(), contacts.end(), sortbyfirst);
 for(j=contacts.begin(); j!= contacts.end(); j++)
 {
  cout<<j->first<<" "<<j->last<<endl;
 }
 cout<<"by last:"<<endl;
 sort(contacts.begin(), contacts.end(), sortbylast);
 for(j=contacts.begin(); j!= contacts.end(); j++)
 {
  cout<<j->first<<" "<<j->last<<endl;
 }
 return 0;
}

////////////////////////////////////////////////
对于比较简单的结构体,保存对象比较好,不要用指针,
struct rec
{
rec(int nId)
{
id = nId;
}
bool operator>(const rec& recArg) const
{
return id < recArg.id;
}
int id;
};
list<rec> m_list;
int main()
{
m_list.push_back(rec(8));
m_list.push_back(rec(2));
m_list.push_back(rec(3));
m_list.sort(greater<rec>());
cout << m_list.begin()->id << endl;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息