我有一个项目,告诉我导入一个csv文件,然后对其进行排序(该文件包含数百万个数字)。我用谷歌搜索了一个合并排序的源代码,发现了这个,我尝试了一下,排序工作了。 (我通过Virtual Box用g ++在ubuntu中编译了此代码)。
#include<iostream>
#include<vector>
using namespace std;
void print(vector<int> v)
{
for(int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}
vector<int> merge(vector<int> left, vector<int> right)
{
vector<int> result;
while ((int)left.size() > 0 || (int)right.size() > 0) {
if ((int)left.size() > 0 && (int)right.size() > 0) {
if ((int)left.front() <= (int)right.front()) {
result.push_back((int)left.front());
left.erase(left.begin());
}
else {
result.push_back((int)right.front());
right.erase(right.begin());
}
} else if ((int)left.size() > 0) {
for (int i = 0; i < (int)left.size(); i++)
result.push_back(left[i]);
break;
} else if ((int)right.size() > 0) {
for (int i = 0; i < (int)right.size(); i++)
result.push_back(right[i]);
break;
}
}
return result;
}
vector<int> mergeSort(vector<int> m)
{
if (m.size() <= 1)
return m;
vector<int> left, right, result;
int middle = ((int)m.size()+ 1) / 2;
for (int i = 0; i < middle; i++) {
left.push_back(m[i]);
}
for (int i = middle; i < (int)m.size(); i++) {
right.push_back(m[i]);
}
left = mergeSort(left);
right = mergeSort(right);
result = merge(left, right);
return result;
}
int main()
{
vector<int> v;
v.push_back(38);
v.push_back(27);
v.push_back(43);
v.push_back(3);
v.push_back(9);
v.push_back(82);
v.push_back(10);
print(v);
cout << "------------------" << endl;
v = mergeSort(v);
print(v);
}
但是当我插入代码以导入csv文件时,它仅读取该文件并且排序未运行,我将其放置错了吗?这是我的代码:
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
using namespace std;
const int jumlahData = 999999;
double data[jumlahData] = {0};
void loadData(const char* namaFile) {
string line;
ifstream myfile (namaFile);
if (myfile.is_open()) {
int counter = 0;
while (getline(myfile, line, ',')) {
//cout << line << '\n';
data[counter] = stod(line);
counter++;
}
myfile.close();
}
}
void print(vector<int> v)
{
for(int i = 0; i < v.size(); i++) cout << v[i] << " ";
cout << endl;
}
vector<int> merge(vector<int> left, vector<int> right)
{
vector<int> result;
while ((int)left.size() > 0 || (int)right.size() > 0) {
if ((int)left.size() > 0 && (int)right.size() > 0) {
if ((int)left.front() <= (int)right.front()) {
result.push_back((int)left.front());
left.erase(left.begin());
}
else {
result.push_back((int)right.front());
right.erase(right.begin());
}
} else if ((int)left.size() > 0) {
for (int i = 0; i < (int)left.size(); i++)
result.push_back(left[i]);
break;
} else if ((int)right.size() > 0) {
for (int i = 0; i < (int)right.size(); i++)
result.push_back(right[i]);
break;
}
}
return result;
}
vector<int> mergeSort(vector<int> m)
{
if (m.size() <= 1)
return m;
vector<int> left, right, result;
int middle = ((int)m.size()+ 1) / 2;
for (int i = 0; i < middle; i++) {
left.push_back(m[i]);
}
for (int i = middle; i < (int)m.size(); i++) {
right.push_back(m[i]);
}
left = mergeSort(left);
right = mergeSort(right);
result = merge(left, right);
return result;
}
int main()
{
vector<int> v;
loadData("dataDemo.csv");
print(v);
cout << "------------------" << endl;
v = mergeSort(v);
print(v);
}
答案 0 :(得分:1)
您的loadData("dataDemo.csv");
调用不会写入先前声明的本地vector<int> v;
。您的向量v
没有元素。您需要编写一种基于v
的内容填充向量double data[jumlahData]
的方法
即..
int main()
{
vector<int> v;
loadData("dataDemo.csv");
populateVectorWithData(v, data); //TODO implement me
print(v);
cout << "------------------" << endl;
v = mergeSort(v);
print(v);
}
答案 1 :(得分:0)
正如其他人指出的那样,您没有对读入的实际数据进行排序。这可以通过返回读入的数据点数来轻松解决。
一种更好的方法(我怀疑您正在尝试)是将向量的引用从main发送到loadData
,并push_back
将向量上的数据发送出去。此外,建议尽可能使用实现提供的库。换句话说,不要重新发明轮子,而只使用<algorithm>
而不是您自己或其他人的排序算法。它有助于防止令人讨厌的错误。
int main()
{
//return the count of integers read into data[] in loadData()
//also change the vector & array types to be consistent, please!
std::vector<int> v (data, loadData("dataDemo.csv");
print(v); //print the loaded vector
cout << "------------------" << endl;
std::sort(v.begin(), v.end()); // using <algorithm> (̶i̶t̶'̶s̶ ̶q̶u̶i̶c̶k̶s̶o̶r̶t̶)̶
print(v);
}