函数合并两个数字列表,并从最小到最大排序

Function to merge 2 lists of numbers and order them from least to greatest?

本文关键字:排序 列表 合并 数字 两个 函数      更新时间:2023-10-16

我必须创建一个函数,该函数将接受2个输入流和一个输出流。该函数应该从两个独立的文件中读取一个整数列表,合并该列表,将数字从最小到最大排序,然后将这个合并有序的列表输出到一个新文件中。

这是函数的代码:

void merge(ifstream& inStream1, ifstream& inStream2, ofstream& outStream)
{
    int arr[100], i = 0;
    while(!inStream1.eof())
    {
        inStream1>>arr[i++];
    }
    while(!inStream2.eof())
    {
        inStream2>>arr[i++];
    }
    int indexOfNextSmallest;
    for (int index = 0; index < i - 1; index++)
    {
        indexOfNextSmallest = indexOfSmallest(arr, index, i);
        swapValues(arr[index], arr[indexOfNextSmallest]);
    }
    for (int index = 0; index < i; index++)
    {
        outStream<<arr[index]<<endl;
    }
}
int indexOfSmallest(const int arr[], int startIndex, int i)
{
    int min = arr[startIndex], indexOfMin = startIndex;
    for (int index = startIndex + 1; index < i; index++)
    {
        min = arr[index];
        indexOfMin = index;
    }
    return indexOfMin;
}
void swapValues(int& v1, int& v2)
{
    int temp;
    temp = v1;
    v1 = v2;
    v2 = temp;
}

这是一个示例输入输出:

文件1有以下编号:

4
5
8
11
18
28 

文件2有以下编号:

87
234
11
8
9
45
23

此输出到新文件:

23
4
5
8
11
18
28
1988923808
87
234
11
8
9
45

我完全迷路了。请帮我解决这个问题。谢谢

int arr[100]

为什么?你知道两个文件中的元素加起来不会超过100个吗?可能不会吧?

因此,您最好使用能够动态调整其大小的数据结构,如std::vector。但是,由于您似乎也想删除重复项,因此std::set是更好的选择。这给了你另一个好处:std::set的元素是排序的。

这样,你的问题就减少到:

void merge(std::istream & in1, std::istream & in2, std::ostream & out) {
 // The set is used to store the elements from both files.
 // Since it also sorts them, no explicit sorting is needed.
 // Duplicates will be removed, if that's not what you want, use a multiset
 std::set<int> storage;
 // The istream_iterator applies operator>> to the whole file, effectively
 // reading the whole file contents.
 storage.insert(
   std::istream_iterator<int>(in1),
   std::istream_iterator<int>());
 storage.insert(
   std::istream_iterator<int>(in2),
   std::istream_iterator<int>());
 // We copy each element of the set (they're in sorted order) into
 // the output. Using the ostream_iterator, it's like writting
 // for (auto element : storage) { out << element << " "; }
 std::copy(
   std::begin(storage), std::end(storage),
   std::ostream_iterator<int>(out, " ")); // if you want seperate lines use "n"
}

用你的输入和这个代码我得到:

4 5 8 9 11 18 23 28 45 87 234 

使用标准库提供的内容!


关于您的代码:

int indexOfSmallest(const int arr[], int startIndex, int i)
{
    int min = arr[startIndex], indexOfMin = startIndex;
    for (int index = startIndex + 1; index < i; index++)
    {
        min = arr[index]; /* HERE */
        indexOfMin = index;
    }
    return indexOfMin;
}

你看不到,当前元素(在index(是否真的小于当前最小值!

// inside that loop
if (arr[index] < min) {
 min = arr[index];
 indexOfMin = index;
}

(当然,还有std::min_element。(

哦,还有:

void merge(ifstream & //...

不要硬编码你没有的需求。这里不需要文件流,只需要流,所以istreamostream是更好的选择。

还有:

while(!inStream1.eof())
{
    inStream1>>arr[i++];
}

考虑包含文本字符串hello的文件。然后您的流将失败,但您将继续读取输入。更好的直接测试输入操作:

while (inStream1 >> arr[i++]);