Don't understand why this is happening. These errors only occur in visual studio...I just compiled via xcode on mac.
This is my actual code (although really only the beginning is significant to this issue...(because everyone wants all the code!):
#include "stdafx.h"
#include <fstream>
#include <sstream>
#include <algorithm>
#include <ctime>
#include "Student.h"
#include <iostream>
using namespace std;
Student* readStudentsFromFile(string filename, int num) {
ifstream studentsStream;
studentsStream.open(filename.c_str());
if (!studentsStream.is_open()) {
cerr << "Couldn't open the file " << filename << endl;
return NULL;
}
// create a new array of students with size 'num'
Student* students = new Student[num];
string name, school, sid;
int id;
// read student records from file
for (int i = 0; i < num; i++) {
getline(studentsStream, name, ',');
getline(studentsStream, sid, ',');
getline(studentsStream, school);
istringstream idConv(sid);
idConv >> id;
// create a student object from the record and store it in the array
students[i] = Student(id, name, school);
}
studentsStream.close();
return students;
}
void writeStudentsToFile(Student students[], int num, string filename) {
ofstream output(filename.c_str());
output.open(filename.c_str());
for (int i = 0; i < num; i++)
{
output << students[i].toString() << endl;
}
}
Student* findCommonStudents1(Student group1[], int len1, Student group2[],
int len2, int numCommon) {
// YOUR CODE HERE
// Hint: Two nested for loops
int tempv = 0;
Student* commonstu = new Student[numCommon];
for (int j = 0; j < len1; j++)
{
for (int k = 0; k < len2; k++)
{
if (group1[j] == group2[k])
{
commonstu[tempv] = group1[j];
tempv = tempv + 1;
}
}
}
return commonstu;
}
Student* findCommonStudents2(Student group1[], int len1, Student group2[],
int len2, int numCommon) {
bool inboth = false;
int tempv = 0;
Student* commonstu = new Student[numCommon];
sort(group2, group2 + len2);
for (int m = 0; m < len1; m++)
{
inboth = binary_search(group2, group2 + len2, group1[m]);
if (inboth == true)
{
commonstu[tempv] = group1[m];
}
}
return 0;
}
int main() {
const int UC_SIZE = 10;
const int SMC_SIZE = 5;
const int SMC_UC_GRADS_SIZE = 2;
Student* uc = readStudentsFromFile("sample_uc_students.txt", UC_SIZE);
Student* smc = readStudentsFromFile("sample_smc_grads.txt", SMC_SIZE);*/
const int UC_SIZE = 350000;
const int SMC_SIZE = 75000;
const int SMC_UC_GRADS_SIZE = 25000;
Student* uc =
readStudentsFromFile("uc_students.txt",
UC_SIZE);
Student* smc =
readStudentsFromFile("smc_grads.txt",
SMC_SIZE);
// Rough timing
time_t start, end;
time(&start);
Student* common1 = findCommonStudents1(uc, UC_SIZE, smc, SMC_SIZE,
SMC_UC_GRADS_SIZE);
time(&end);
cout << "Using linear search it took " << difftime(end, start) << "
seconds."
<< endl;
sort(common1, common1 + SMC_UC_GRADS_SIZE);
writeStudentsToFile(common1, SMC_UC_GRADS_SIZE, "smc_grads_at_uc_1.txt");
time(&start);
Student* common2 = findCommonStudents2(uc, UC_SIZE, smc, SMC_SIZE,
SMC_UC_GRADS_SIZE);
time(&end);
cout << "Using binary search it took " << difftime(end, start)
<< " seconds." << endl;
sort(common2, common2 + SMC_UC_GRADS_SIZE);
writeStudentsToFile(common2, SMC_UC_GRADS_SIZE, "smc_grads_at_uc_2.txt");
delete[] smc;
delete[] uc;
delete[] common1;
delete[] common2;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire