一、自定义结构体中的变量是 int 、usigned int 等类型时
有如下三种方法
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
//for_each 的谓词,为了输出不同的对象, 这里利用了函数对象
template<class T>
class Display: public binary_function<T, T,bool>
{
public:
void operator()(const T& a)
{
cout<<a<<endl;
}
};
class A //待排序的类
{
public:
A(int i): m_i(i){}
int Get()const{return m_i;}
friend ostream& operator<<(ostream& out, const A& a)
{
out<<a.Get();
return out;
}
private:
int m_i;
};
//方法一,使用谓词, 升序排序
bool LessThan(const A& a, const A& b)
{
return a.Get() < b.Get();
}
//方法二, 重载 <,升序排序
inline bool operator< (const A& a, const A& b)
{
return a.Get() < b.Get();
}
// 方法三, 重载>, 降序排序, 配合下面的Greater类
inline bool operator> (const A& a, const A& b)
{
return a.Get() > b.Get();
}
template<class T>
class Greater: public binary_function<T, T,bool>
{
public:
bool operator()(const T& a, const T& b)
{
return a > b;
}
};
int main()
{
vector<A> Avec;
Avec.push_back(A(5));
Avec.push_back(A(10));
Avec.push_back(A(2));
Avec.push_back(A(2));
sort(Avec.begin(), Avec.end(), LessThan);//谓词函数
for_each(Avec.begin(), Avec.end(), Display<A>()); //升序排序
sort(Avec.begin(), Avec.end()); //默认升序排列类对象对象容器
for_each(Avec.begin(), Avec.end(), Display<A>());//输出升序排列对象容器
sort(Avec.begin(), Avec.end(),Greater<A>());//降序排列对象
for_each(Avec.begin(), Avec.end(), Display<A>());//输出降序排列对象容器
return 0;
}
另一种参考例子
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std;
class myclass {
public:
myclass(){};
myclass(int a, int b):first(a), second(b){}
void set_value(int a,int b)
{
first = a;
second = b;
}
bool operator < (const myclass &m)const {
return first < m.first;
}
public:
int first;
int second;
};
bool less_second(const myclass & m1, const myclass & m2) {
return m1.second < m2.second;
}
int main() {
int i = 0;
vector< myclass > vect;
myclass my;
for(i = 0 ; i < 10 ; i ++){
//myclass my(10-i, i*3);
my.set_value(10-i,i*3);
vect.push_back(my);
}
for(i = 0 ; i < vect.size(); i ++)
cout<<"("<<vect[i].first<<","<<vect[i].second<<")\n";
sort(vect.begin(), vect.end());
cout<<"\nafter sorted by first:"<<endl;
for(i = 0 ; i < vect.size(); i ++)
cout<<"("<<vect[i].first<<","<<vect[i].second<<")\n";
cout<<"\nafter sorted by second:"<<endl;
sort(vect.begin(), vect.end(), less_second);
for(i = 0 ; i < vect.size(); i ++)
cout<<"("<<vect[i].first<<","<<vect[i].second<<")\n";
return 0 ;
}
二 、自定义结构体中的变量是 string char 等类型时
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Lable_word_Node{
public:
void Fill_String_to_Lable_and_Word(char s[]);//用字符串对 对象中的Lable_String、Old_String赋值
void Char_of_String_Increase_sort(char s[]);//对字符串 s[] 中的字母以升序排序
bool operator < (const Lable_word_Node &m) const{//重载 < 符号,以便于 sort 函数以升序排序
int n = Lable_String.compare(m.Lable_String);//待比较的字符串与当前的字符串比较,小于等于返回ture,大于返回false
if(n == 0 || n == 1)
return false;
else
return true;
}
public:
string Lable_String;//用于存储单词的标识
string Old_String;//用于存储单词
};
void Lable_word_Node::Fill_String_to_Lable_and_Word(char s[])//用字符串对 对象中的Lable_String、Old_String赋值
{
Old_String.append(s);
char *temp = new char[strlen(s)+1];
strcpy(temp, s);
Char_of_String_Increase_sort(temp);
Lable_String.append(temp);
delete []temp;
}
void Lable_word_Node::Char_of_String_Increase_sort(char dest[])//对字符串 s[] 中的字母以升序排序
{
sort(&dest[0], &dest[strlen(dest)]);
}
void Print_String_Member(vector<Lable_word_Node> vect)//打印向量中存放的标识和单词
{
int i = 0;
for(i = 0;i < vect.size();i++)
{
cout << vect[i].Lable_String.begin() << " " << vect[i].Old_String.begin() <<endl;
}
}
void Unique_sort_Node(vector<Lable_word_Node> *vect1, vector<Lable_word_Node> *vect2)//对向量中的重复标志进行归类,以标识来分类,相同标识的放于
//Old_String 追加到相同标识的string中
{
int i = 0;
int n;
int Node_count = 0;//计数,归整重复的结点后,一共多少个结点
string temp1,temp2,temp3;
(*vect2).push_back((*vect1)[0]);//将vect1[0]的结点加入向量中
temp2 = (*vect1)[0].Lable_String;//获得vect1[0]中的Lable_String
for(i = 1; i <(*vect1).size(); i++)
{
temp1 = (*vect1)[i].Lable_String;//取vect1[i]中的Lable_String
n = temp1.compare(temp2);//与上一次的temp1比较
if(n == 0)//有相同标识,则加入到相同标识的Old_String 中
{
temp3 = " ";//与便于与下一个单词分开,前面加个空格
temp1 = (*vect1)[i].Old_String;//提取(*vect1)[i]的Old_String,以便于将其压入到相同标识的Old_String 中
temp3 += temp1;//压入前先给其前面加一个空格
(*vect2)[Node_count].Old_String.append(temp3);
}
else//不同标识,则新建标识
{
Node_count++;
(*vect2).push_back((*vect1)[i]);//压入vect向量中
temp2 = (*vect2)[Node_count].Lable_String;//这次的Lable_String变为下一次要比较的参考标准
}
}
}
//用二分查找快速查找
int My_Binary_Search(vector<Lable_word_Node> vect, string s)
{
int low = 0;
int high = vect.size()-1;
int mid;
int n;
while(low <= high)
{
mid = (low + high) / 2;
n = s.compare(vect[mid].Lable_String);
if(0 == n)
return mid;
else if (-1 == n)
high = mid - 1;
else
low = mid +1;
}
return -1;
}
int main(void)
{
int i = 0;
char s[23] = {'\0'};
unsigned int count = 0;
int pos;
vector<Lable_word_Node> vect1;
vector<Lable_word_Node> vect2;
Lable_word_Node A;
Lable_word_Node Find_Node;
for(i = 0;i<6; i++)
{
gets(s);
A.Fill_String_to_Lable_and_Word(s);
vect1.push_back(A);
A.Lable_String.erase(A.Lable_String.begin(),A.Lable_String.end());
A.Old_String.erase(A.Old_String.begin(),A.Old_String.end());
}
printf("\n创建标识后:\n");
printf("---------------------------------------------\n");
Print_String_Member(vect1);
printf("\n排序后:\n");
printf("---------------------------------------------\n");
stable_sort(&vect1[0], &vect1[vect1.size()]);
Print_String_Member(vect1);
printf("\n整理重复的标号后:\n");
printf("---------------------------------------------\n");
Unique_sort_Node(&vect1, &vect2);
Print_String_Member(vect2);
printf("\n找到了:\n");
printf("---------------------------------------------\n");
Find_Node.Fill_String_to_Lable_and_Word("stop");//假设待查的单词为 "stop"
pos = My_Binary_Search(vect2, Find_Node.Lable_String);
if(pos != -1)
{
cout << vect2[pos].Old_String.begin() <<endl;
}
else
{
cout <<" This dict is not change position dict!" << endl;
}
return 0;
}