请问C++有没有给结构中的一个参数排序,然后该结构的其他参数也随之改变的函数?

lftlovecjy 2014-04-24 09:53:56
比如说:
typedef struct student{
int ge;
int gi;
int first;
int second;
int third;
int four;
int five;
int num;
int final;
}student;
student s[100];
按照ge排序,然后其他参数一块改变
...全文
117 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lftlovecjy 2014-05-03
  • 打赏
  • 举报
回复
九度是一个ACM网站,我做的就是上面的题 这个怎么解决呢?
mujiok2003 2014-05-03
  • 打赏
  • 举报
回复
引用 9 楼 lftlovecjy 的回复:
[quote=引用 8 楼 mujiok2003 的回复:] [quote=引用 4 楼 lftlovecjy 的回复:] [quote=引用 3 楼 u012061345 的回复:] 去看一下STL的算法,特别是sort()算法的使用。
我看了,说实在的看不懂啊,我试过sort(s[0].ge,s[n-1].ge);语法上没有错,但是一运行就出问题,也是看不懂[/quote] std::sort(s, s +n, [](student const& s1, student const& s2){ return s1.ge < s2.ge;}); [/quote] 请问为什么九度显示Main.cc: In function ‘int main()’: Main.cc:84: error: expected primary-expression before ‘[’ token Main.cc:84: error: expected primary-expression before ‘]’ token Main.cc:84: error: expected primary-expression before ‘const’ Main.cc:84: error: expected primary-expression before ‘const’ 这个怎么解决呢?[/quote] []是c++ 11的lamda语法。九度是啥玩意?
lftlovecjy 2014-05-03
  • 打赏
  • 举报
回复
引用 8 楼 mujiok2003 的回复:
[quote=引用 4 楼 lftlovecjy 的回复:] [quote=引用 3 楼 u012061345 的回复:] 去看一下STL的算法,特别是sort()算法的使用。
我看了,说实在的看不懂啊,我试过sort(s[0].ge,s[n-1].ge);语法上没有错,但是一运行就出问题,也是看不懂[/quote] std::sort(s, s +n, [](student const& s1, student const& s2){ return s1.ge < s2.ge;}); [/quote] 请问为什么九度显示Main.cc: In function ‘int main()’: Main.cc:84: error: expected primary-expression before ‘[’ token Main.cc:84: error: expected primary-expression before ‘]’ token Main.cc:84: error: expected primary-expression before ‘const’ Main.cc:84: error: expected primary-expression before ‘const’ 这个怎么解决呢?
mujiok2003 2014-04-24
  • 打赏
  • 举报
回复
引用 4 楼 lftlovecjy 的回复:
[quote=引用 3 楼 u012061345 的回复:] 去看一下STL的算法,特别是sort()算法的使用。
我看了,说实在的看不懂啊,我试过sort(s[0].ge,s[n-1].ge);语法上没有错,但是一运行就出问题,也是看不懂[/quote] std::sort(s, s +n, [](student const& s1, student const& s2){ return s1.ge < s2.ge;});
赵4老师 2014-04-24
  • 打赏
  • 举报
回复
struct student t;
for (i=0;i<100-1;i++) {
 for (j=i+1;j<100;j++) {
  if (s[i].ge<s[j].ge) {t=s[i];s[i]=s[j];s[j]=t;}
 }
}
  • 打赏
  • 举报
回复
引用 4 楼 lftlovecjy 的回复:
[quote=引用 3 楼 u012061345 的回复:] 去看一下STL的算法,特别是sort()算法的使用。
我看了,说实在的看不懂啊,我试过sort(s[0].ge,s[n-1].ge);语法上没有错,但是一运行就出问题,也是看不懂[/quote]
typedef struct student{
int ge;
int gi;
int first;
int second;
int third;
int four;
int five;
int num;
int final;
}student;
student s[100];


bool operator<( const student& l, const student& r)
{
	return l.ge<r.ge;
}
int main( )
{
	std::sort( s,s+100 );

}
赵4老师 2014-04-24
  • 打赏
  • 举报
回复
使用qsort函数。
lftlovecjy 2014-04-24
  • 打赏
  • 举报
回复
引用 3 楼 u012061345 的回复:
去看一下STL的算法,特别是sort()算法的使用。
我看了,说实在的看不懂啊,我试过sort(s[0].ge,s[n-1].ge);语法上没有错,但是一运行就出问题,也是看不懂
罗博士 2014-04-24
  • 打赏
  • 举报
回复
去看一下STL的算法,特别是sort()算法的使用。
lftlovecjy 2014-04-24
  • 打赏
  • 举报
回复
引用 1 楼 akirya 的回复:
其他参数一块改变 是啥意思?
引用 1 楼 akirya 的回复:
其他参数一块改变 是啥意思?
比如说 50 20 1 2 3 4 5 6 7 30 40 2 3 4 5 6 7 8 排序之后: 30 40 2 3 4 5 6 7 8 50 20 1 2 3 4 5 6 7
  • 打赏
  • 举报
回复
其他参数一块改变 是啥意思?

65,208

社区成员

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

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