如何基于一个数组对多个数组进行排序并打印出来

编程入门 行业动态 更新时间:2024-10-12 03:27:53
本文介绍了如何基于一个数组对多个数组进行排序并打印出来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用字符串流读取了10行的输入文件,并将所有值存储在单独的数组中.ID,第一个,最后一个,城市,州和GPA.看起来像这样.

I used a string-stream to read an input file with 10 lines and stored all the values into separate arrays. ID, first, last, city, state, and GPA. It looked like this.

1密苏里州堪萨斯州内森(Nathan Humphery)3.35 2密西西比州(Sara Jonathan Lees)3.56 3 Kayla James Liberty KS 3.78 4 Kyle Davis独立KS 2.98 ... 8 Daniel Earla独立KS 3.77

1 Nathan Humphery Kansas MO 3.35 2 Sara Jonathan LeesSummit MO 3.56 3 Kayla James Liberty KS 3.78 4 Kyle Davis Independence KS 2.98 ... 8 Daniel Earla Independence KS 3.77

因此ID数组应为{1,2,3,4,...,8} 而城市数组将是{堪萨斯州,LeesSummit,自由,独立,...,独立}

So the ID array would be { 1, 2, 3, 4, ..., 8} and the city array would be {Kansas, LeesSummit, Liberty, Independence, ..., Independence}

我程序中的一个功能应该打印出按城市分类的信息.

One of the functions in my program is supposed to print out the information sorted by city.

我使用选择排序算法对城市数组进行排序,以使其按照正确的字母顺序排列.但是现在我不确定如何正确排序其他数组以匹配city数组中的信息.是否可以将某些内容添加到SelectionSort函数中,以将city数组中发生的更改与其他数组相匹配?我什至需要对每个数组进行排序,还是缺少一种更简便的方法?

I used the selection sort algorithm to sort the city array to put them in the correct alphabetical order. But now I'm not sure how to properly sort the other arrays to match the information that was in the city array. Can something be added to the SelectionSort function that would match the changes that happened in the city array to the other arrays? Do I even need to sort every array, or is there an easier way that I'm just missing?

我还不能使用诸如"#include algorithm"和"sort()"之类的东西,而且我还在使用命名空间std.

I can't use things such as "#include algorithm" and "sort()" yet, and I'm also using namespace std.

void SelectionSort(string city[], int size) { int i; int j; int indexSmallest; string temp; for (i = 0; i < size; ++i) { indexSmallest = i; for (j = i + 1; j < size; ++j) { if (city[j] < city[indexSmallest]) { indexSmallest = j; } } temp = city[i]; city[i] = city[indexSmallest]; city[indexSmallest] = temp; } }

输出应如下所示.

4凯尔·戴维斯独立队KS 2.98 8丹尼尔·厄尔拉独立队KS3.77 1密苏里州堪萨斯州内森·哈弗里(Nathan Humphery)3.35 2密苏里州密西西比州(Sara Jonathan Lees)密西根州3.56 3凯拉·詹姆斯·利伯蒂(Kayla James Liberty)KS 3.78

4 Kyle Davis Independence KS 2.98 8 Daniel Earla Independence KS 3.77 1 Nathan Humphery Kansas MO 3.35 2 Sara Jonathan LeesSummit MO 3.56 3 Kayla James Liberty KS 3.78

推荐答案

使用此答案,您可以创建一个索引数组,然后根据您感兴趣的条件对索引数组进行排序.然后以排序方式引用数据时,您使用索引数组.

Using the techniques used in this answer, you can create an array of indices, and sort the index array based on the criteria you're interested in. Then when referencing the data in a sorted manner, you use the index array.

#include <vector> //... std::vector<int> index_array; int main() { for (int i = 0; i < number_of_items; ++i) index_array.push_back(i); //... SelectionSort(city, size) } void SelectionSort(string city[], int size) { int i; int j; int indexSmallest; string temp; for (i = 0; i < size; ++i) { indexSmallest = i; for (j = i + 1; j < size; ++j) { if (city[index_array[j]] < city[index_array[indexSmallest]]) { indexSmallest = j; } } temp = index_array[i]; index_array[i] = index_array[indexSmallest]; index_array[indexSmallest] = temp; } }

然后在访问数组时,使用索引数组:

Then when accessing the arrays, use the array of indices:

for (int i = 0; i < size; ++i) std::cout << city[index_array[i]] << "\n" << names[index_array[i]] << "\n\n";

更多推荐

如何基于一个数组对多个数组进行排序并打印出来

本文发布于:2023-11-29 04:06:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645219.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   多个   打印出来

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!