64,643
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <algorithm>
using namespace std;
bool comp(float a[], float b[]) {
return a[3]<b[3];
}
float m[1000][4];
int main( int argc , char* argv[] )
{
sort(m, m+1000, comp);
return 0;
}
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef struct {
float v[4];
}Tm;
bool comp(Tm a, Tm b) {
return a.v[3] < b.v[3];
}
Tm m[1000];
int main( int argc , char* argv[] )
{
sort(m, m+1000, comp);
return 0;
}
#include <iostream>
#include<functional>
#include<algorithm>
using namespace std;
struct Temp
{
float x[4];
bool operator<( const Temp& r )
{
return x[3] < r.x[3];
}
};
int main(int argc, char* argv[])
{
float m[5][4]={{1.112,1.112,1.112,6.112},{1.112,1.112,1.112,1.112},{1.112,1.112,1.112,3.112},{1.112,1.112,1.112,5.112},{1.112,1.112,1.112,2.112}};
Temp* p = (Temp*) &m[0][0];
sort( p , p +5 );
for(int i=0;i<5;i++){
cout<<m[i][0]<<" "<<m[i][1]<<" "<<m[i][2]<<" "<<m[i][3]<<" "<<endl;
}
return 0;
}
vector<int>m(4000);
for(i = 0; i < 1000; i++)
for(j = 0; j < 4; j++)
m.push_back(m[i][j]);
sort(m.begin(), m.end());
#include<iostream>
#include <algorithm>
using namespace std;
bool issmall(const float &a,const float &b)
{
return a<b;
}
int main()
{
float num[2][3]={{1.3,7.9,3.2},{4.0,5.7,4.6}};
float *pt=&num[0][0],*ptr=&num[1][2];
++ptr;
for(size_t i=0;i<6;++i)
cout<<*pt++<<endl;
pt=&num[0][0];
cout<<endl;
sort(pt,ptr,issmall);
pt=&num[0][0];
for(size_t i=0;i<6;++i)
cout<<*pt++<<endl;
return 0;
}
//参考一下,多谢Inhibitory的提醒。
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef float (PA)[4];
const int N = 3;
PA* indexes[N];
float m[N][4];
bool comp(const PA* a, const PA* b) {
return (*a)[3] < (*b)[3];
}
int main( int argc , char* argv[] )
{
m[0][3] = 1.0f;
m[1][3] = 6.0f;
m[2][3] = 2.0f;
for (int i = 0; i < N; ++i)
indexes[i] = &m[i];
printf("before sort\n");
for (int i = 0; i < N; ++i)
printf("%f : %f\n", m[i][3], (*indexes[i])[3]);
sort(indexes, indexes+N, comp);
printf("\nafter sort\n");
for (int i = 0; i < N; ++i)
printf("%f : %f\n", m[i][3], (*indexes[i])[3]);
return 0;
}