2D 矢量排序算法

2D Vector Sorting Algorithm

本文关键字:算法 排序 2D      更新时间:2023-10-16

我正在尝试编写一个排序循环,该循环根据表示时间的列下的值对 2D 向量进行排序。它应按升序对条目进行排序(下面的示例数据(。如果时间列下的值为 null/"-",则应将其推到底部。

这就是我目前所拥有的;

bool hasSwapped;
do {
    hasSwapped = false;
    for (int nor = 0; nor < (rows - 1); nor++)
    {
        //nor stands for no of rows
        int t = 5;
        if (Champ[nor][t] < Champ[nor + 1][t]&& Champ[nor][t] != "-")
            continue;
        else
        {
            swap(Champ[nor], Champ[nor + 1]);
            hasSwapped = true;
        }
    }
} while (hasSwapped);

但是,当我运行该程序时,它会以某种方式卡在循环中。程序运行并对所有没有任何时间值作为"-"进行排序,但是一旦您将"-"值添加到表中,程序就不会输出任何内容。我相信当我尝试解释 if 语句中的"-"值时会出现问题。有人可以建议解决我的问题吗?

未排序数据示例(我使用"/"作为各种条目的分隔符,有问题的时间值是每行中的倒数第二个值(

1  / BrianOkoth /24-02-1999/    3/  100M/   -/  DNF
2   /HanselRack/    13-09-1988/ 2/  200M/   1.54/   Q
3   /FalloTimmy/    12-09-1973/ 1/  100M/   0.32/   -
4   /RodneyJohn/    14-08-1984/ 2/  500M/   3.92/   DQ
5   /GarryField/    02-02-2001/ 3/  100M/   0.90/   Q
6   /WallowGon/ 03-03-1977/ /1  /500M   /3.4/   Q
7   /TerryFlops/    24-04-1975/ 1/  200M/   1.3/    Q
8   /DinghyRoes/    13-06-1989/ 3/  500M/   -/  DNF
9   /WorryNot/  23-07-1956/ 2/  200M/   3.2 /Q
10  /GogoPopo/  24-04-1977/ 1/  100M/   3.0/    -

麦克维:

#include "stdafx.h"
#include<iostream>
#include <vector>
#include <string>
#include<fstream>
#include <Cstdlib>
#include <functional>
#include "stdlib.h"
#include<algorithm>
using namespace std;
void main()
{
    int rows;
    cout << "Enter number rows -> ";
    cin >> rows;
    vector < vector<string>>Champ;
    //input  your own data into the 2d vector Champ that is similar to the 
sample data given in the question.
    //sorting loop
    bool hasSwapped;
    do {
        hasSwapped = false;
        for (int nor = 0; nor < (rows - 1); nor++)
        {
            //nor stands for no of rows
            int t = 5;
            if (Champ[nor][t] < Champ[nor + 1][t] && Champ[nor][t] != "-")
                continue;
            else
            {
                swap(Champ[nor], Champ[nor + 1]);
                hasSwapped = true;
            }
        }
    } while (hasSwapped);
    //output loop
    cout << "NO     Name    DOB     Heat    D(m)    Time(s) Status" << endl;
    for (unsigned int i = 0; i < Champ.size(); i++)
    {
        int in = 0;
        for (int n = 1; n < rows + 1; n++)
        {
            std::string s = std::to_string(n);
            Champ[in][0] = s;
            in++;
        }
        for (unsigned int j = 0; j < Champ[i].size(); j++)
        {
            cout << Champ[i][j] << "t";
        }
        cout << endl;
    }
    system("pause");
}

IMO,2D矢量是错误的起点。我将从一个struct开始,其中包含文件中每个字段的成员,并为这些结构定义比较。由于您想要的时间字段无论如何都是浮点数,并且那些(在几乎所有合理的实现中(都支持infinity的表示,因此我会使用它来获得您想要的行为。

if (foo == '-')
    record.time = std::numeric_limits<double>::infinity();
else
    record.time = std::stod(foo);

如您所料,infinity排序为大于任何其他数字,因此您不必对比较执行任何特殊操作即可使它们按所需的顺序排序 - 所有存在数字的数字按该数字排序,然后是所有带有"-"表示时间字段的数字。

稍微简化一下(省略大多数不相关的字段(,代码可能如下所示:

#include <algorithm>
#include <string>
#include <iostream>
#include <iomanip>
#include <iterator>
struct item { 
    std::string name;
    double time;
    friend std::istream &operator>>(std::istream &input, item &i) { 
        std::string line;
        std::getline(input, line);
        std::istringstream is(line);
        std::getline(is, i.name, '/');
        char ch;
        is >> ch;
        if (ch == '-') {
            is.ignore(1);
            i.time = std::numeric_limits<double>::infinity();
        }
        else {
            is.unget();
            is >> i.time;
        }
        return input;
    }
    friend std::ostream &operator<<(std::ostream &os, item const &i) { 
        return os << i.name << 't' << i.time;
    }
    bool operator<(item const &other) { 
        return time < other.time;
    }
};
int main() { 
    std::istringstream input {
    R"(BrianOkoth/ -/
HanselRack/  1.54/
FalloTimmy/ 0.32/
RodneyJohn/ 3.92/
GarryField/ 0.90/
WallowGon/  3.4/
TerryFlops/ 1.3/
DinghyRoes/ -/
WorryNot/   3.2 /
GogoPopo/   3.0/)"};
    std::vector<item> items { std::istream_iterator<item>(input), {}};
    std::copy(items.begin(), items.end(), 
              std::ostream_iterator<item>(std::cout, "n"));
    std::cout << 'n';
    std::sort(items.begin(), items.end());
    std::copy(items.begin(), items.end(), 
              std::ostream_iterator<item>(std::cout, "n"));
}

输出如下所示:

BrianOkoth  inf
HanselRack  1.54
FalloTimmy  0.32
RodneyJohn  3.92
GarryField  0.9
WallowGon   3.4
TerryFlops  1.3
DinghyRoes  inf
WorryNot    3.2
GogoPopo    3
FalloTimmy  0.32
GarryField  0.9
TerryFlops  1.3
HanselRack  1.54
GogoPopo    3
WorryNot    3.2
WallowGon   3.4
RodneyJohn  3.92
BrianOkoth  inf
DinghyRoes  inf