gpt4 book ai didi

c++ - 用行对列进行排序

转载 作者:行者123 更新时间:2023-11-28 08:20:53 26 4
gpt4 key购买 nike

我有一组点 (x,y,z)。我想使用第一列对这些数据进行排序,第二列和第三列应根据排序的第一列重新排列。是否可以在 C++ 中执行此操作,如果可以,请帮助我。

在此附上我的实现代码,但我在第 31 行和第 32 行收到错误消息“从 'const' vod*' 到 'const int [*][3]' 的无效转换”。我尝试使用几个方法,但我的努力还没有成功。我在这里使用了“qsort”,有没有其他方法或者我可以使用“sort”来做到这一点。由于我有一个非常大的数据集,我希望使用一种快速的方法。所以我最后需要的是仅使用第一列排序的数据集,如下例所示:排序前

34  12  12
12 34 15
24 20 34
13 11 10
40 23 32

排序后

12  34 15
13 11 10
24 20 34
34 12 12
40 23 32

如果有什么好的方法可以帮助我写代码...谢谢

#include <iostream>
#include <cstdlib>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class Point
{
private:
double x;
double y;
double z;

public:
Point(){};
~Point(){};

Point(double X, double Y, double Z){
x=X;y=Y;z=Z; }

double X(){return x;}
double Y(){return y;}
double Z(){return z;}
};

int cmp ( const void *pa, const void *pb ) {
const int (*a)[3] = pa;
const int (*b)[3] = pb;
if ( (*a)[1] < (*b)[1] ) return -1;
if ( (*a)[1] > (*b)[1] ) return +1;
return 0;
}

int main ( ) {
vector<Point> points;
int input_x,input_y,input_z;
int i=0;
while(i<6){//data set,it is a example, actual data come from a file

cout<<"x: ";cin>>input_x;
cout<<"y: ";cin>>input_y;
cout<<"z: ";cin>>input_z;
Point point(input_x,input_y,input_z);
points.push_back(point);
i++;
}
for (int i=0;i<points.size();i++){//before sort
cout<<points[i].X()<<" "<<points[i].Y()<<" "<<points[i].Z()<<endl;
}

qsort( points, 6, sizeof points[0], cmp );

for (int i=0;i<points.size();i++){//after sort
cout<<points[i].X()<<" "<<points[i].Y()<<" "<<points[i].Z()<<endl;
}
system("PAUSE");
return 0;
}

最佳答案

使用 std::sort 而不是 qsort 几乎可以肯定是最简单的:

class Point { 
int x, y, z;
public:
Point(int x, int y, int z) : x(x), y(y), z(z) {}

bool operator<(Point const &other) {
return x < other.x;
}
// skipping the reading and other stuff for now...
};

int main() {
std::vector<Point> points;
// add some Points to `points` here.

// sort using order defined in Point::operator<:
std::sort(points.begin(), points.end());
return 0;
}

编辑:为了使比较与被比较的项目分开,您使用单独的函数或仿函数来进行比较,并将其传递给 std::sort。不过,在任何情况下,您的类(class)都有一些您确实想要更改的内容 - 至少,因为您的 Point::X()Point::Y() Point::Z() 不修改 Point 对象,您想让它们成为 const 成员函数。完成后,排序就相当简单了:

#include <vector>
#include <algorithm>
#include <iterator>
#include <iostream>

class Point {
double x, y, z;
public:
double X() const { return x; }
double Y() const { return y; }
double Z() const { return z; }

Point(double x=0.0, double y=0.0, double z=0.0) : x(x), y(y), z(z) {}
};

namespace std {
ostream &operator<<(ostream &os, Point const &p) {
return os << "(" << p.X() << ", " << p.Y() << ", " << p.Z() << ")";
}
}
struct byX {
bool operator()(Point const &a, Point const &b) {
return a.X() < b.X();
}
};

int main(){
std::vector<Point> points;

for (int i=0; i<10; i++)
points.push_back(Point(rand(), i, i));

std::cout << "Unsorted:\n";
std::copy(points.begin(), points.end(),
std::ostream_iterator<Point>(std::cout, "\n"));

std::sort(points.begin(), points.end(), byX());

std::cout << "\nSorted:\n";
std::copy(points.begin(), points.end(),
std::ostream_iterator<Point>(std::cout, "\n"));
return 0;
}

从技术上讲,我想我应该再添加一个小细节:如果您的任何点中的 x 值是 NaN,这将无法正常工作。 NaN 不等于任何违反 std::sort 所需的严格弱排序的事物(甚至不等于它本身)。

关于c++ - 用行对列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5887524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com