1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
// type conversion prototype
double toNumber(const std::string & str);
int main() {
ifstream theFile("~/Dropbox/Master/Cpp/Data/Edgedata.csv");
// vectors used to store data from CSV file
vector<string> FromNodes, ToNodes, AllNodes;
vector<double> time;
// Temporary variables to store elements in string stream
double iTempTime;
string tempFrom, tempTo, tempTime, line;
while( getline(theFile, line)){ // read current line
istringstream iss(line); // construct string stream from line
// read line until comma saving value to temporary strings
getline(iss, tempFrom, ',');
getline(iss, tempTo, ',');
getline(iss, tempTime, ',');
// converting temporary edge weight string to temporary double value
iTempTime = toNumber(tempTime);
// csv file is sorted after nodes of origin but has many duplicates, I remove duplicates here, saving values to vectors for later use.
if (tempTo != ToNodes[ToNodes.size()-1] || iTempTime != time[time.size()-1]) {
FromNodes.push_back(tempFrom);
ToNodes.push_back(tempTo);
time.push_back(iTempTime);
}
// saving every new node in AllNodes vector to count number of nodes for use in Adjacency_list length
if (tempFrom != AllNodes[AllNodes.size()-1]) {
AllNodes.push_back(tempFrom);
}
}
//Test to check if reading the CSV file and storing the data works properly
cout << AllNodes.size() << endl;
cout << FromNodes.size() << endl;
return 0;
}
double toNumber(const string & str)
{
istringstream iss(str);
double num = 0;
iss >> num;
return num;
|