二维数组排序 sort
2016-07-15 17:50
302 查看
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;
bool cmp(int *p, int *q)
{
if (p[0] == q[0])
{
if (p[1] == q[1])
{
return p[2]<q[2];
}
else return p[1]<q[1];
}
else return p[0]<q[0];
}
int main()
{
srand(time(0));
int b[10] = {3, 2, 1, 4, 5, 5, 7, 8, 9, 10};
int c[10] = {3,10,9, 4, 88, 6, 7, 8, 9, 11 };
int i;
int **a = new int*[10];
for (i = 0; i<10; ++i)
{
a[i] = new int[2];
a[i][0] = b[i];
a[i][1] = c[i];
//a[i][2] = rand() % 100;
//printf("%d\\t%d\\t%d",a[i][0],a[i][1],a[i][2]);
}
sort(a, a + 10, cmp);
//sort(a, a + 10);
cout<<"After sort"<<endl;
for(i=0;i<10;++i)
{
printf("%d %d\n",a[i][0],a[i][1]);
}
system("pause");
return 0;
}
#include <algorithm>
#include <ctime>
using namespace std;
bool cmp(int *p, int *q)
{
if (p[0] == q[0])
{
if (p[1] == q[1])
{
return p[2]<q[2];
}
else return p[1]<q[1];
}
else return p[0]<q[0];
}
int main()
{
srand(time(0));
int b[10] = {3, 2, 1, 4, 5, 5, 7, 8, 9, 10};
int c[10] = {3,10,9, 4, 88, 6, 7, 8, 9, 11 };
int i;
int **a = new int*[10];
for (i = 0; i<10; ++i)
{
a[i] = new int[2];
a[i][0] = b[i];
a[i][1] = c[i];
//a[i][2] = rand() % 100;
//printf("%d\\t%d\\t%d",a[i][0],a[i][1],a[i][2]);
}
sort(a, a + 10, cmp);
//sort(a, a + 10);
cout<<"After sort"<<endl;
for(i=0;i<10;++i)
{
printf("%d %d\n",a[i][0],a[i][1]);
}
system("pause");
return 0;
}
相关文章推荐
- iOS 获取网络图片的大小
- 我的wine中文字体配置笔记
- Multiply Strings
- Everything使用攻略和技巧
- 前端开发杂记
- 在讨论全景视频的未来之前,我们先搞清楚全景视频是如何实现的[转]
- HttpSessionAttributeListener
- Linux下利用NFS和Samba在source insight上快速开发应用程序
- 屏幕坐标系获取两个矩形面积及重叠面积,重叠面积比率的java代码
- 设计模式之Builder模式
- ListView 嵌套 EditText 显示错位
- oracle死锁,从查询,记录,到杀掉
- pthread问题
- Linux运维基础命令笔试题
- iOS10本地通知的探索
- java_log_01
- [直播一揽子]x264参数的解释
- .net中的序列化
- Oracle tns 协议
- CentOS 7修改MySQL 5.6字符集为UTF-8