函数合并两个数字列表,并从最小到最大排序
Function to merge 2 lists of numbers and order them from least to greatest?
我必须创建一个函数,该函数将接受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 & //...
不要硬编码你没有的需求。这里不需要文件流,只需要流,所以istream
和ostream
是更好的选择。
还有:
while(!inStream1.eof())
{
inStream1>>arr[i++];
}
考虑包含文本字符串hello
的文件。然后您的流将失败,但您将继续读取输入。更好的直接测试输入操作:
while (inStream1 >> arr[i++]);
相关文章:
- C 中的排序列表
- 使用霍尔分区方案的快速排序算法返回原始未排序列表
- 带有额外参数的排序列表以进行比较功能
- 算法,用于查找获取排序列表所需的最小移动元素到结束步骤
- 迭代排序列表并计算不同的数字
- 维护多集中数字的排序列表和另一个集合中的累积总和
- 超过合并 k 排序列表的时间限制(leetcode)
- 正在检查排序列表中是否有一个范围内的数字
- O(N) 查找,但在排序列表上使用 upper_bound 时进行 O(log(N)) 比较
- 如何在只更改一个元素的情况下在排序列表中进行快速排序
- 返回指向排序列表的指针;C++中的链表
- 除了显示平均,最高和最低温度之外,有没有办法打印出整个排序列表
- 整数排序列表..TMP
- c++排序:具有相同数组值的排序列表
- 复制C++中未排序列表的构造函数
- 如何将元素添加到排序列表中..?(C++)
- 三维对象的Z排序列表
- 在我的排序列表中插入项目时出现问题
- 将节点插入到不带头指针的排序列表中
- 使用"pop_front"从排序列表中删除元素会导致崩溃