c++ - 一次对两个 vector 进行排序?

标签 c++ vector c++14 bucket-sort

我当前的项目涉及根据点与 x-y 坐标平面原点的距离对点进行排序。它必须找到每个点到原点的距离,对距离进行排序,并输出创建距离的点,而不是距离。它只能使用 bucketsort 和 vector 。我的排序工作正常,但我很难弄清楚如何确保距离 vector 和点 vector 保持对齐。 (B[0] 的距离是从 c[0] 处的点创建的,即使该点最初不在 C[0] 处。)到目前为止,这是我的代码。

以标准用户输入为例:

0.2 0.38
0.6516 -0.1
-0.3 0.41
-0.38 0.2

首先,在 main 中,获取用户的输入。点的每一半最初作为它自己的 double 值放入 vector A,然后与它的另一半作为一对组合在 vector C 中。

int main(){
double number; int t = 0; pair<double,double> x;
while (cin >> number){ 
    A.push_back(number);    }
while (t < A.size()){
    x = make_pair(A[t], A[t+1]); C.push_back(x); t += 2; }
int q = 0; 
while (q < (C.size() - 1)){
    findDistance(C[q].first, C[q].second);
    q++;
}

然后,使用距离函数找到所有距离,结果存储在 vector B 中。这是函数。

void findDistance(double x = 0, double y = 0) {
double x2 = pow(x, 2);
double y2 = pow(y, 2);
double z = x2 + y2;
double final = sqrt(z);
B.push_back(final);
}

Bucketsort 组织所有的距离,将结果存储在 B 中。

void bucketSort(vector<double> &arr)
{
int n = B.size();
vector<double> b[n];

for (int i=0; i<n; i++)
{
   int bi = n*arr[i];
   b[bi].push_back(arr[i]);
}

for (int i=0; i<n; i++)
   sort(b[i].begin(), b[i].end());

int index = 0;
for (int i = 0; i < n; i++){
    for (int j = 0; j < b[i].size(); j++){
        arr[index++] = b[i][j]; }
    }
}

这就是问题所在。 B 已排序,但 C 保持其原始顺序。查看和比较表之间的值以找到正确的顺序的成本太高。 (其中一个参数是将运行时间保持为 O(n)。我有什么办法可以解决这个问题吗?

这是我的完整代码:

vector<double> A;
vector<double> B;
vector<pair<double,double>> C;

void findDistance(double x = 0, double y = 0) {
double x2 = pow(x, 2);
double y2 = pow(y, 2);
double z = x2 + y2;
double final = sqrt(z);
B.push_back(final);
}

void bucketSort(vector<double> &arr)
{
int n = B.size();
vector<double> b[n];

for (int i=0; i<n; i++)
{
   int bi = n*arr[i];
   b[bi].push_back(arr[i]);
}

for (int i=0; i<n; i++)
   sort(b[i].begin(), b[i].end());

int index = 0;
for (int i = 0; i < n; i++){
    for (int j = 0; j < b[i].size(); j++){
        arr[index++] = b[i][j]; }
    }
}

int main(){
double number; int t = 0; pair<double,double> x;
while (cin >> number){ 
    A.push_back(number);    }
while (t < A.size()){
    x = make_pair(A[t], A[t+1]); C.push_back(x); t += 2; }
cout << setprecision(5); cout << fixed; 
int q = 0; double r = 0; double d = 0; 
while (q < (C.size() - 1)){
    findDistance(C[q].first, C[q].second);
    q++;
}
bucketSort(B);
cout << showpos; cout << fixed;
//more cout here to show results
}

这是一个示例输入和输出: 输入:

0.2 0.38
0.6516 -0.1
-0.3 0.41
-0.38 0.2

输出:

-0.380000 +0.200000
+0.200000 +0.380000
-0.300000 +0.410000
+0.651600 -0.100000

如有任何帮助,我们将不胜感激,谢谢。

最佳答案

首先,我认为您应该决定是真的需要知道距离,还是只需要它们来对点 vector 进行排序。 如果您不需要它们,则无需单独存放它们。 我要做的是:

  1. 创建一个 Point 类(其成员为 x 和 y)。
  2. 为其定义比较运算符(以便能够排序)。
  3. 从用户输入中获取分数(几乎与您现在使用的方式相同)。
  4. 调用标准排序算法。

实现成员函数也可能有用,如 double get_distance() .另外如果打算多次入手成员(member)double distance可能会有用。

作为替代方案,您可以为 pair<double,double> 定义比较函数元素并在排序期间将其用作比较函数。

希望我写的有意义。

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

相关文章:

c++ - 在 ctor 中使用 const vector 初始化的 const vector 成员

C++ 行到 vector

c++ - 函数模板参数的模板返回类型

C++ 模板 : do something inside a function, 取决于模板参数

vector - 如何在 Rust 中提取字符串向量的元素?

c++ - 函数尝试 block 和 noexcept

c++ - “memcpy”未在此范围内声明

c++ - CMAKE错误: "Cannot open source file" : 'CMakeCCompilerId.c'

c++ - 让 svn diff 在提交期间显示 C++ 函数

php - 从 PHP Web 应用程序调用 C++ 库 : system() vs SWIG PHP extension?