c++ 头文件 及 sort 和 vector简单介绍

c++  sort :http://www.16kan.com/post/997260.html

http://wenku.baidu.com/view/e064166daf1ffc4ffe47ac67.html

假设自己定义了一个结构体

node  struct node

int a; 

int b; 

double c; 

}

有一个 node 类型的数组 node arr[100] �想对它进行排序�先按 a 值升序排列�如果 a 值相同�再按 b 值降序排列�如果 b 还相同�就按 c 降序排列。就可以写这样一个比较函数� 以下是代码片段�

bool cmp(node x,node y)

  { 

if(x.a!=y.a) return x.a 

if(x.b!=y.b) return x.b>y.b; 

return return x.c>y.c; 

排序时写 sort(arr,a+100,cmp);

 sort(a,a+10,greater<int>());      //从大到小排序

示例:

 1 struct node
 2 {
 3     char s[110];
 4     int num;
 5 }p[N];
 6 bool cmp(node x,node y)
 7 {
 8     if(x.num==y.num)
 9     {
10         if(strlen(x.s)==strlen(y.s))
11         return strcmp(x.s,y.s)<0;
12         return strlen(x.s)>strlen(y.s);
13     }
14     return x.num>y.num;
15 }

 sort(p+1,p+num+1,cmp);

c++头文件:

http://developer.51cto.com/art/201002/183607.htm

vector简单介绍:

 1 int main()
 2 {
 3   vector<int>v;
 4   int i, t, n;
 5   while(cin>>n)
 6   {
 7      for(i = 0; i < n; i++)
 8      {
 9         cin>>t;
10         v.push_back(t);  //vector加入元素
11      }
12      sort(v.begin(), v.end()); //vector的sort
13      
14      for(i = 0; i < n; i++)
15          cout<<v[i]<<" ";  //vector从0开始
16      cout<<endl;
17   }
18   return 0;
19 }
原文地址:https://www.cnblogs.com/bfshm/p/3164401.html