所以我应该从input.txt文件中获取1000000个数字并将它们存储到一个大小为10000的术语数组中,并将每个术语大小为10000的另一个数组存储起来。对于现在,我只是想尝试排序一系列7个数字:
0
1
2
0
3
2
0
我应该得到输出:
0 3
2 2
1 1
3 1
但相反。我正在
0 3
1 2
1 1
3 1
由于某种原因,它不能将两者置于所需的位置。我假设Insert()
函数存在问题。但我无法找到它。
另外,作为旁注。我尝试使用1,000,000个数字运行程序,但它只是说SearchAndSort.exe失败并需要关闭。
请帮帮我!我打赌那里只是一些小的语法错误。 这是我的main.cpp文件
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "TermTable.h"
using namespace std;
int main()
{
TermTable termTableObj;
ifstream fin("input.txt");
if (fin.is_open())
{
cout << "Open" << endl;
int num;
while (fin >> num)
{
termTableObj.Insert(num);
}
termTableObj.Sort();
termTableObj.Display();
}
return 0;
}
这是我的TermTable.cpp实现文件。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include "TermTable.h"
using namespace std;
int TermTable::BinarySearch(int searchValue)
{
int first, last, middle, position;
bool found;
first = 0;
last = currentAmount - 1;
found = false;
position = -1;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (termArray[middle] == searchValue)
{
found = true;
position = middle;
}
else if (termArray[middle] > searchValue)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
void TermTable::Sort()
{
int temp;
bool swapOccurred;
do {
swapOccurred = false;
for (int count = 0; count < (currentAmount - 1); count++)
{
if (frequencyArray[count] < frequencyArray[count + 1])
{
temp = frequencyArray[count];
frequencyArray[count] = frequencyArray[count + 1];
frequencyArray[count + 1] = temp;
temp = termArray[count];
termArray[count] = frequencyArray[count + 1];
termArray[count + 1] = temp;
swapOccurred = true;
}
}
} while (swapOccurred);
}
void TermTable::Insert(int value)
{
int position = BinarySearch(value);
if (position != -1)
{
frequencyArray[position] += 1;
}
else
{
int temp;
termArray[currentAmount] = value;
frequencyArray[currentAmount] = 1;
currentAmount++;
for (int i = currentAmount + 1; i >= 0; i--)
{
if (termArray[i] < termArray[i - 1])
{
temp = termArray[i];
termArray[i] = termArray[i - 1];
termArray[i - 1] = temp;
temp = frequencyArray[i];
frequencyArray[i] = frequencyArray[i - 1];
frequencyArray[i - 1] = temp;
}
else
{
break;
}
}
}
}
void TermTable::Display()
{
ofstream fout("output.txt");
for (int j = 0; j < 10000; j++)
{
fout << termArray[j] << " " << frequencyArray[j] << endl;
}
}
这是我的班级定义:
#include <cstdlib>
#include <iostream>
using namespace std;
// class specification
class TermTable
{
public:
// constructor
TermTable()
{
currentAmount = 0;
for (int i = 0; i < 10000; i++)
{
termArray[i] = 0;
}
for (int i = 0; i < 10000; i++)
{
frequencyArray[i] = 0;
}
};
// member functions
int BinarySearch(int searchValue);
void Insert(int value);
void Sort();
void Display();
// destructor
~TermTable()
{
return;
}
private:
// data
int currentAmount;
int termArray[10000];
int frequencyArray[10000];
};
答案 0 :(得分:2)
看起来您只需要输入值的标准直方图,并按频率递减的顺序列出。
所以让我们通常插入地图(按值作为键),然后按频率降序对对进行排序。
<强> Live On Coliru 强>
#include <iostream> // cin/cout
#include <algorithm> // for_each
#include <iterator> // istream_iterator
#include <map>
#include <vector>
#include <functional> // mem_fn
using namespace std;
using Histo = map<int, size_t>;
int main() {
Histo histo;
std::for_each(std::istream_iterator<int>(std::cin), {}, [&](int i) { histo[i]++; });
vector<pair<int, size_t>> v(begin(histo), end(histo));
sort(begin(v), end(v), [](Histo::value_type a, Histo::value_type b) { return a.second > b.second; });
for(auto& p : v)
std::cout << p.first << " " << p.second << "\n";
}
打印
0 3
2 2
1 1
3 1