我有点纠结于排序字符串Table[X][Y]。作为标记,我使用C++,必须使用标准库并为所有C++ (不仅仅是C++ 11)设置标准库。
Table的大小是固定的(我得到X读取文件有多少行,Y是固定的,因为这是不同的“属性”有每一行)。
创建表时,表的每一部分都是从以前从文件中读取并存储在字符串中的内容中获得的Table[X][Y] = stringX.data();。我在第一列中有数字(其中一列我将用作排序标准)、名称、地址等等。
创建表的部分是:
Table[i][0] = string1.data();
Table[i][1] = string2.data();
Table[i][2] = string3.data();
Table[i][3] = string4.data();
Table[i][4] = string5.data();其中,"i“是while(fgets)的当前”迭代“,它一次从文件中读取一行,在这些字符串中执行一些操作,并将行读取的每个部分的”最终值“存储在字符串中。
我必须使用第一列作为递减顺序的标准对该表进行排序。
让我们想象一下表是这样的:Table[4][3]
20 | Jhon | 14th July
2 | Mary | 9th June
44 | Mark | 10th December
1 | Chris | 4th Feb我需要输出如下:
44 | Mark | 10th December
20 | Jhon | 14th July
2 | Mary | 9th June
1 | Chris | 4th Feb我已经阅读了几个问题和页面,它们对int/chars数组进行排序,或者将数组转换为向量,然后使用它们。我试图排序字符串表,我没有转换任何东西(如果可能的话,不知道)。
我不知道我是否成功地解释了这个问题和我有足够清楚的情况。Im没有把我所有的代码都放进去,因为除了字符串表的声明和表中作为string.data放置的字符串之外,其余的代码与表和排序过程无关。代码打开文件,逐行读取,过滤一些分隔符和特殊字符所需的信息,并将每个“排名标准”放置到一个字符串中,然后在评估每个标准并给出一个总分(然后存储在“string1”中)之后,指定一个“排名”。完成所有这些工作后,我创建字符串Table[x][y],并将筛选和处理的信息一次放在该表的一行中(因为我在读取文件中的每一行时对此进行了修改)。
剩下的唯一的事情就是将表从得分最高到最后的排序,然后创建一个前10名的文件。
我感谢并感谢您提前花时间阅读了这篇文章,以及您可以提供的任何提示、信息、代码或源代码。
发布于 2021-06-02 07:46:31
首先,正如注释中提到的那样,在C++中使用std::vector实现了可变长度数组。您使用的当前语法
std::string Table[X][Y]
如果X或Y都是运行时变量,则C++是不合法的。考虑到您的示例,标准的C++声明如下:
std::vector<std::array<std::string, 3>> Table;
所以让我们假设这是您将要使用的。
下一步是根据第一列对数据进行排序。这可以通过使用std:分类算法函数以及适当的谓词来实现,该谓词指示您使用第一列作为排序标准。
下面是一个使用数据的简短示例,说明这一切是如何完成的:
#include <vector>
#include <array>
#include <iostream>
#include <algorithm>
#include <string>
int main()
{
std::vector<std::array<std::string, 3>> Table;
// Set up the test data
Table.push_back({"20", "Jhon", "14th July"});
Table.push_back({"2", "Mary", "9th June"});
Table.push_back({"44", "Mark", "10th December"});
Table.push_back({"1", "Chris", "4th Feb"});
std::cout << "Before sort:\n\n";
for (auto& s : Table)
std::cout << s[0] << " | " << s[1] << " | " << s[2] << "\n";
std::cout << "\n\nAfter sort:\n\n";
// Sort the data using the first column of each `std::array` as the criteria
std::sort(Table.begin(), Table.end(), [&](auto& a1, auto& a2)
{ return std::stoi(a1[0]) > std::stoi(a2[0]); });
// Output the results:
for (auto& s : Table)
std::cout << s[0] << " | " << s[1] << " | " << s[2] << "\n";
}以下是最后的输出:
Before sort:
20 | Jhon | 14th July
2 | Mary | 9th June
44 | Mark | 10th December
1 | Chris | 4th Feb
After sort:
44 | Mark | 10th December
20 | Jhon | 14th July
2 | Mary | 9th June
1 | Chris | 4th Feb输出需要一点格式化,但这并不重要。
请记住,数据的来源并不重要,无论是来自文件还是上面示例所示的硬编码。无论您如何填充Table,这都取决于您。目标是向您展示一旦填充,如何排序的数据。
我们做的第一件事是创建这个表并用测试数据填充它。注意,向量有一个push_back函数来向向量添加条目。
然后调用std::sort有一个谓词函数( lambda),其中谓词被赋予两个项,在这种情况下,它将是两个std::array的引用。然后目标是返回如果第一个std::array (在这个例子中,a1)应该放在第二个std::array (a2)之前。
注意,我们只关心第一列,所以我们只需要考虑每个数组的数组,并对它们进行比较。
还请注意,由于数组是一个std::string,所以我们不能按字典顺序比较它--我们需要将字符串转换为int并比较int值。这就是std::stoi调用转换为整数的原因。
关于排序谓词的最后一件事是,我们希望有一个降序排序。因此,要使用的比较运算符是>,而不是“传统的”< (该<按升序方式排序)。
希望这能解释代码所做的事情。
编辑:
由于您正试图让这段代码在C++98中工作,所以最简单的方法是
std::vector<std::vector<std::string>>而不是std::vector<std::array<std::string, 3>>有鉴于此,下面是C++98的代码:
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
bool SortFirstColumn(const std::vector<std::string>& a1,
const std::vector<std::string>& a2)
{
return atoi(a1[0].c_str()) > atoi(a2[0].c_str());
}
int main()
{
std::vector<std::vector<std::string>> Table;
// Set up the test data
std::vector<std::string> vect(3);
vect[0] = "20";
vect[1] = "Jhon";
vect[2] = "14th July";
Table.push_back(vect);
vect[0] = "2";
vect[1] = "Mary";
vect[2] = "9th June";
Table.push_back(vect);
vect[0] = "44";
vect[1] = "Mark";
vect[2] = "10th December";
Table.push_back(vect);
vect[0] = "1";
vect[1] = "Chris";
vect[2] = "10th December";
Table.push_back(vect);
std::cout << "Before sort:\n\n";
for (size_t i = 0; i < Table.size(); ++i)
std::cout << Table[i][0] << " | " << Table[i][1] << " | " << Table[i][2] << "\n";
std::cout << "\n\nAfter sort:\n\n";
// Sort the data using the first column of each `std::vector<std::string>` as the criteria
std::sort(Table.begin(), Table.end(), SortFirstColumn);
// Output the results:
for (size_t i = 0; i < Table.size(); ++i)
std::cout << Table[i][0] << " | " << Table[i][1] << " | " << Table[i][2] << "\n";
}https://stackoverflow.com/questions/67798217
复制相似问题