请问怎么用sort对二维数组进行排序

laserss 2012-11-02 02:12:35
请问怎么用sort对二维数组进行排序

这个代码哪里出了问题??
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

bool cmp(int *p,int *q){
return p[1]>q[1];
}
int main()
{
int i;
int m[3][2];

m[0][0]=5; m[0][1]=10;
m[1][0]=2; m[1][1]=5;
m[2][0]=3; m[2][1]=6;
//printf("@\n");
sort( (int**)m, (int**)(m+3), cmp); //printf("!\n");

for(i=0;i<3;i++) printf("%d %d\n",m[i][0],m[i][1]);

return 0;
}
...全文
3531 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
olderma 2012-11-03
  • 打赏
  • 举报
回复
引用 8 楼 fugeleigao528 的回复:
要实现二维数组行按照升序排序,第一列按照降序排序,首先对第一列进行降序排序,然后再对行排序。 C/C++ code1234567891011121314151617181920212223242526272829303132333435363738394041#include<iostream>#include<cstdio>#include<algorithm>usi……
冒泡排序第一个for循环起始变量改为for(i=a-1;i>=0;--i)
laserss 2012-11-03
  • 打赏
  • 举报
回复
引用 11 楼 Prairial_0 的回复:
排个版。。。 C/C++ code123456789101112131415161718192021222324252627#include <cstdio>#include <cstring>#include <algorithm>using namespace std; template<int N>struct element{ int _emt[N]; ……
呵呵 貌似只有你看懂了我想干啥。。 本来是个acm的题目 正常的作法也是结构体封装后sort排序 但是总觉得这样做有些多余 可是又不懂怎么使用sort直接对二维数组(或者多维)进行排序 。 还是谢谢大家了!特别是lansong4
Prairial_0 2012-11-03
  • 打赏
  • 举报
回复
排个版。。。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

template<int N>
struct element{
  int _emt[N];
  friend bool operator < (const element& a, const element &b){
    return a._emt[1] < b._emt[1];
  }
};

int main(){
  int m[3][2] = {
    1,10,2,5,3,6
  };
  element<2>* p = (element<2>*)m;
  sort(p,p+3);

  for (int i = 0; i < 3; ++i)
    printf("%d %d\n", m[i][0], m[i][1]);

  return 0;
}

Prairial_0 2012-11-03
  • 打赏
  • 举报
回复
我这里用vs2012提示编译错误是 error C2106: “=”: 左操作数必须为左值 也就是说sort会重写m内的元素,而m内的元素全是右值。比如你写m[2] = m[1]就会出这种错。 (之前还以为sort只用了swap,看来是我图样了:sort是快速排序、插入排序、堆排序的混合体,难怪) 那么你要做的就是对m包装一下,比如可以这么写: [code=c++] #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct element{ int a, b; friend bool operator < (const element& a, const element &b){ return a.b < b.b; } }; int main(){ int m[3][2] = { 1,10,2,5,3,6 }; element* p = (element*)m; sort(p,p+3); for (int i = 0; i < 3; ++i) printf("%d %d\n", m[i][0], m[i][1]); return 0; } [/code]
SKATE11 2012-11-03
  • 打赏
  • 举报
回复
二维数组不也是连续存储吗 排序上跟一维没多大区别吧
olderma 2012-11-02
  • 打赏
  • 举报
回复
要实现二维数组行按照升序排序,第一列按照降序排序,首先对第一列进行降序排序,然后再对行排序。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int a=3,b=2;

bool cmp(int p,int q)
{
return (p<q);
}
int main()
{
int i;
int m[a][b];
m[0][0]=5; m[0][1]=10;
m[1][0]=2; m[1][1]=5;
m[2][0]=3; m[2][1]=6;
for(i=0;i<a;++i)//对第一列采用冒泡法进行排序
{
int tem[b];
for(int j=i;j<a;++j)
{
if(m[j][0]<m[j+1][0])
{
for(int k=0;k<b;++k)//如果a[i][0]<a[i+1][0],对这两行的元素进行对换
{
tem[k]=m[j][k];
m[j][k]=m[j+1][k];
m[j+1][k]=tem[k];
}
}
}
}

for(i=0;i<a;++i)
{
sort((int*)m[i], (int*)m[i], cmp);
printf("%d %d\n",m[i][0],m[i][1]);
}
return 0;
}
Kuovane 2012-11-02
  • 打赏
  • 举报
回复
换种思考方式:
m[i][0],m[i][1] 是否等价与 m[0][i],m[1][i];
绑在一起也是人定的。换种思考,将问题变的简单。

仔细看下怎么配数据的,0后面存的是左边的,1后面的存的是右边,分别对应左右两边排序。


laserss 2012-11-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

引用 4 楼 的回复:

[Quote=引用 3 楼 的回复:]


希望得到的结果是
5 10
3 6
2 5

百度知道
跟这位兄弟提的问题一样 不过我没有按答案中那样的动态的创建数组


数据换种组织,
一样的效果

C/C++ code

#include<iostream>
#include<cstdio>
#include<algorithm>……
[/Quote]

不能这样呀,,m[i][0]和m[i][1]是绑定在一起的 排序选择的关键值后面的那个m[i][1],也就是按m[i][1]对各个m[i]进行排序
sort( (int*)m, (int*)m+3, cmp);
sort( (int*)m+3, (int*)m+6, cmp);
这样分别排序就打乱结构了
对于下面这种数据答案就错啦 ^-^
30 20
10 30
20 10
Kuovane 2012-11-02
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

[Quote=引用 3 楼 的回复:]


希望得到的结果是
5 10
3 6
2 5

百度知道
跟这位兄弟提的问题一样 不过我没有按答案中那样的动态的创建数组
[/Quote]

数据换种组织,
一样的效果


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

bool cmp(int p,int q){
return p>q;
}
int main()
{
int i;
int m[2][3];

m[0][0] = 5; m[1][0] = 10;
m[0][1] = 2; m[1][1] = 5;
m[0][2] = 3; m[1][2] = 6;


sort( (int*)m, (int*)m+3, cmp);
sort( (int*)m+3, (int*)m+6, cmp);

for(i=0;i<3;i++)
{
printf("%d %d\n",m[0][i],m[1][i]);
}

return 0;
}
laserss 2012-11-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]


希望得到的结果是
5 10
3 6
2 5

百度知道
跟这位兄弟提的问题一样 不过我没有按答案中那样的动态的创建数组
dpdp_2012 2012-11-02
  • 打赏
  • 举报
回复

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

bool cmp(int p,int q){
return p>q;
}
int main()
{
int i;
int m[3][2];

m[0][0]=5; m[0][1]=10;
m[1][0]=2; m[1][1]=5;
m[2][0]=3; m[2][1]=6;
//printf("@\n");
// sort( (int**)m, (int**)(m+3), cmp); //printf("!\n");
int *p=(int*)m;
sort(p,p+6,cmp);

for(i=0;i<3;i++) printf("%d %d\n",m[i][0],m[i][1]);

return 0;
}

laserss 2012-11-02
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
请问怎么用sort对二维数组进行排序

这个代码哪里出了问题??
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

bool cmp(int *p,int *q){
return p[1]>q[1];
}
int main()
{
int i;
int m[3]……
[/Quote]

抱歉哈 我再描述详细一些
以[][1]为关键字 用sort对二维数组进行排序
希望得到的结果是
5 10
3 6
2 5

跟这位兄弟提的问题一样 不过我没有动态的创建数组
Kuovane 2012-11-02
  • 打赏
  • 举报
回复
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

bool cmp(int p,int q){
return p>q;
}
int main()
{
int i;
int m[3][2];

m[0][0]=5; m[0][1]=10;
m[1][0]=2; m[1][1]=5;
m[2][0]=3; m[2][1]=6;

sort( (int*)m, (int*)m+6, cmp);

for(i=0;i<3;i++)
{
printf("%d %d\n",m[i][0],m[i][1]);
}

return 0;
}

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧