如何解决如何在C ++中按特定列的值对2D字符串数组进行排序?
我有以下从外部.txt文件获取/创建的2d数组。
id
我需要根据“姓氏”列(每行的列索引2)对该数组进行排序,所以我基本上得到以下结果:
z
我将如何以编程方式执行此操作?无法使用 id sub.id.x sub.id.y sub.id.y.y
1 a 1 AA 1
2 a 1 CC 2
3 a 2 DD 3
4 a 2 AA 1
5 b 1 AA 1
6 b 1 CC 2
7 b 2 DD 3
8 b 2 AA 1
9 b 3 GG 4
10 b 3 OO 5
11 c 1 AA 1
12 c 1 CC 2
13 c 2 DD 3
14 c 2 AA 1
15 c 3 GG 4
16 c 3 OO 5
17 c 4 PP 7
18 c 4 OW 6
。我不断收到string accountsArr[5][7] = {
"bham@gnet.com","Blake","Ham","squid62","1987","U","Teacher","jdark@att.net","Jim","Dark","gymrat32","1985","A","Master","hgreen@lakes.net","Hannah","Green","flower22","2007","Apprentice","tsmith@dna.com","Tom","Smith","tuna20","2000","jarrow@pnet.com","James","Arrow","ahoy10","2005","Apprentice"
};
。
string accountsArr[5][7] = {
"jarrow@pnet.com","bham@gnet.com","Teacher"
};
更新:我需要/想知道如何在原始字符串数组上专门做到这一点(没有向量,结构等)。
解决方法
让我为您提供一种使用std::sort
首先,为您的数据创建一个适当的结构。我将简化您的输入。我们也已经为此实现了operator<
,稍后再使用。
struct Person
{
string first_name;
string last_name;
string email;
Person(const string& first_name,const string& last_name,const string& email) :
first_name(first_name),last_name(last_name),email(email) {}
bool operator<(const Person& other) const { return last_name < other.last_name; }
};
C ++旨在成为一种面向对象的语言,如果您具有任何规则的结构化数据,则它至少应该是一种结构。因为我们不需要封装,也没有办法使对象无效,所以可以将其保留为结构,否则我们将选择一个类。
原始数组通常不是存储数据的好方法。使用一个的唯一好理由是必须对速度进行超优化。因此,我们现在将您的原始数组转换为适当的容器:
vector<Person> persons;
persons.reserve(rows); // <-- reserve allows for the memory to be properly pre-allocated
for(size_t i=0; i<rows; i++)
{
persons.push_back(Person(accountsArr[i][1],accountsArr[i][2],accountsArr[i][0]));
}
现在,我们在定义operator<
的结构上有一个合适的容器,剩下要做的就是调用std::sort
:
std::sort(persons.begin(),persons.end());
完成。
我们还可以考虑创建一个处理所有这些问题的类Persons
,并拥有一个方法read_file(const string& file)
来读取数据,将其放入成员向量中,然后对其进行排序。
无论如何,在将来,如果人们告诉您使用std::sort
,那么搜索有关其用法的教程将不会有任何伤害。它提供了一些其他功能。如https://en.cppreference.com/w/cpp/algorithm/sort中所述,对其进行描述,并确保阅读并理解其中的所有内容。
在这种情况下,您可以编写幼稚的Bubblesort,但是在编程时,您希望自己做的尽可能少。 STL函数最有可能更有效(您的排序在O(N²)中,但是排序可以在O(NlogN)中进行),并一遍又一遍地进行测试(创建错误非常容易)。
此外,由于您在自定义气泡排序中自己定义了一个,因此STL具有built in std::swap
。
无论如何,请考虑将您将来的代码放在CodeReview上。
编辑:当您在问题中写了一些有关“未识别的标识符排序”的内容时,如果要使用功能,则需要包括标题。对于std::sort
,正确的斜率是#include <algorithm>
。
所以我终于通过进行“冒泡排序”解决了这个问题。这是我的实现方法。希望这可以帮助其他在查找按特定列对2d数组进行排序的示例时遇到困难的人:
int rows = 5;
int col = 7;
string accountsArr[rows][col] = {
"bham@gnet.com","Blake","Ham","squid62","1987","U","Teacher","jdark@att.net","Jim","Dark","gymrat32","1985","A","Master","hgreen@lakes.net","Hannah","Green","flower22","2007","Apprentice","tsmith@dna.com","Tom","Smith","tuna20","2000","jarrow@pnet.com","James","Arrow","ahoy10","2005","Apprentice"
};
//do a bubble sort
for(int maxElement = rows - 1; maxElement > 0; maxElement--){
for(int i = 0; i < maxElement; i++){
//if current row "last name" column is > the next row "last name" column,swap them
if(theAccounts[i][2] > theAccounts[i + 1][2]){
std::swap(theAccounts[i],theAccounts[i + 1]);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。