在此程序中,我正在加载不同的数据集以测试合并,冒泡和插入排序算法。我一直在零零碎碎地工作,这是一个早期的迭代。由于某种原因,我的代码随机崩溃,因此最后10次运行更多。运行调试器后,它将带我到堆上的随机位置,但未提供有关程序问题的更多信息,我已将其精确定位到我的sort.cpp文件中,但仍然无法获取它。我不相信它会导致内存泄漏,并且我没有使用指针,所以一切都应该存在。如果您可以帮助我对程序进行故障排除,将不胜感激。
主要
#include <iostream>
#include "merge.h"
#include "bubble.h"
#include "insertion.h"
#include "createfile.h"
#include "sort.h"
using namespace std;
int main()
{
cout << "You can do it Xavier, I believe in you" << endl;
//========================================
//Sort Random Sets
//========================================
SortRandom();
//========================================
//Sort Backward Sets
//========================================
// SortBackwards();
//========================================
//Sort sets with 20% Unique
//========================================
// Sort20Percent();
//========================================
//Sort sets with 30% randomized
//========================================
// Sort30Percent();
}
sort.cpp
#include "sort.h"
#include "fstream"
#include "iostream"
#include "bubble.h"
#include "insertion.h"
#include "merge.h"
using namespace std;
void SortRandom()
{
sort random;
//Sorts 10 set
random.load("Random(10).txt");
random.execute();
random.stats();
random.save("Random(10)Stats.txt");
cout << "====================" << endl;
//Sorts 1000 set
random.load("Random(1000).txt");
random.execute();
random.stats();
random.save("Random(1000)Stats.txt");
cout << "====================" << endl;
//Sorts 10,000 set
random.load("Random(10000).txt");
random.execute();
random.stats();
random.save("Random(10000)Stats.txt");
cout << "====================" << endl;
//Sorts 100,000 set
random.load("Random(100000).txt");
random.execute();
random.stats();
random.save("Random(100000)Stats.txt");
}
sort.h
#ifndef SORT_H
#define SORT_H
#include "algorithm.h"
#include <fstream>
#include <sstream>
#include <chrono>
using namespace std;
class sort : public algorithm
{
private:
vector<int> dataset;
string file;
string time1;
string time2;
string time3;
public:
//loads file into program
void load(string filename)
{
ifstream inFile;
ofstream writefile;
inFile.open(filename);
file = filename;
int entry;
string str;
while(std::getline(inFile, str))
{
inFile >> entry;
dataset.push_back(entry);
}
}
//Print the unsorted vector
void print()
{
for(int i=0;i<dataset.size();i++)
{
cout << dataset[i] << endl;
}
}
//Sort set and time it
void execute()
{
vector<int> temp1 = dataset;
vector<int> temp2 = dataset;
vector<int> temp3 = dataset;
//Time for bubblesort
using timer = std::chrono::high_resolution_clock;
timer::time_point start_time = timer::now();
bubblesort(temp1);
timer::time_point end_time = timer::now();
cout << "Total Time for BubbleSort: " << chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count() << "ms" << endl;
//Time for MergeSort
using timer = std::chrono::high_resolution_clock;
timer::time_point start_time2 = timer::now();
MergeSort(temp2,0,temp2.size());
timer::time_point end_time2 = timer::now();
cout << "Total Time for MergeSort " << chrono::duration_cast<std::chrono::milliseconds>(end_time2 - start_time2).count() << "ms" << endl;
//Time for Insertion Sort
using timer = std::chrono::high_resolution_clock;
timer::time_point start_time3 = timer::now();
insertionsort(temp3,temp3.size());
timer::time_point end_time3 = timer::now();
cout << "Total Time for Insertion Sort " << chrono::duration_cast<std::chrono::milliseconds>(end_time3 - start_time3).count() << "ms" << endl;
//Save time variables
ostringstream x,y,z;
x << chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
time1 = x.str();
y << chrono::duration_cast<std::chrono::milliseconds>(end_time2 - start_time2).count();
time2 = y.str();
z << chrono::duration_cast<std::chrono::milliseconds>(end_time3 - start_time3).count();
time3 = z.str();
dataset = temp1;
}
//print sorted vector to screen
void display()
{
insertionsort(dataset,dataset.size());
for(int i=0;i<dataset.size();i++)
{
cout << dataset[i] << ',';
}
cout << endl;
cout << "===============================================" << endl;
}
//Print the size of the dataset,method and time it took
void stats()
{
cout << "To sort this data set of size " << dataset.size() << " using the bubblesort method took " << time1 << " ms." << endl;
cout << "To sort this data set of size " << dataset.size() << " using the mergesort method took " << time2 << " ms." << endl;
cout << "To sort this data set of size " << dataset.size() << " using the insert sort method took " << time3 << " ms." << endl;
}
//Save stats to a file
void save(string filename)
{
ofstream writefile;
writefile.open(filename);
writefile << "\n";
writefile << "===========================================================================" << "\n" << endl;
writefile << "To sort this data set of size " << dataset.size() << " using the bubblesort method took " << time1 << " ms." << endl;
writefile << "To sort this data set of size " << dataset.size() << " using the mergesort method took " << time2 << " ms." << endl;
writefile << "To sort this data set of size " << dataset.size() << " using the insert sort method took " << time3 << " ms." << endl;
writefile << "===========================================================================" << "\n" << endl;
for(int i=0;i<dataset.size();i++)
{
writefile << dataset[i];
writefile << ',';
}
dataset.clear();
}
};
void SortRandom();
void SortBackwards();
void Sort20Percent();
void Sort30Percent();
#endif // SORT_H
答案 0 :(得分:0)
函数sort :: load()可能是问题所在:调用push_back()时,将连续重新分配矢量数据集。如果数据集足够多,程序将出现内存分配失败。
一种解决方法是使用dataset.reserve(MAX_DATASET_SIZE)在程序开始时初始化其容量。