-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadIris.cpp
More file actions
59 lines (48 loc) · 1.81 KB
/
Copy pathreadIris.cpp
File metadata and controls
59 lines (48 loc) · 1.81 KB
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
#include "readIris.h"
std::vector<double> line2vec(std::string line){
/**
* Convert delimited numeric string to vector<double>.
*
* Assumes delimiters are single-char and non-numeric. Agnostic to delimiter
* type. No built-in error handling.
*
* @param line: string of delimiter-separated numeric values
* @return 1-D vector of double precision values.
*/
//Initialize vars
std::string::size_type sz = 0; //Set sz to default type for std::string.size()
std::vector<double> result;
while (sz <= line.length()) {
line = line.substr(sz); //Return substring of line beginning at sz
double num = std::stod (line,&sz); //Reads line until 1st non-numeric char
//Sets sz to index of delimiter
//Assign numeric substr to num as double
sz++; //Skip delimiter for next iteration
result.push_back(num); //Append num to end of output vector
}
return result;
}
std::vector<std::vector<double>> readIris(std::string filename){
/**
* Convert csv file to 2-D vector<double>.
*
* Design specifically for iris.data file. May need modification for others.
* Assumes no headers, and that delimiters are single-char and non-numeric.
* Agnostic to delimiter type. No built-in error handling.
*
* @param filename: (string) full path to file
* @return 2-D vector of double precision values.
*/
//Initialize vars
std::vector<std::vector<double>> result;
std::vector<double> row;
std::string line;
std::ifstream myFile(filename); //Open filename
//Loop over lines in filename
while (std::getline(myFile,line)){
row = line2vec(line); //Convert each line to 1-D vector<double>
result.push_back(row); //Append row vector to end of 2-D result vector
}
myFile.close(); //Closefile
return result;
}