回答編集履歴
5
修正
answer
CHANGED
@@ -63,6 +63,7 @@
|
|
63
63
|
mat = parse_csv("sample.csv");
|
64
64
|
} catch (std::exception &e) {
|
65
65
|
std::cout << "Error: " << e.what() << std::endl;
|
66
|
+
return 1;
|
66
67
|
}
|
67
68
|
|
68
69
|
// 出力する。
|
4
修正
answer
CHANGED
@@ -38,9 +38,14 @@
|
|
38
38
|
*/
|
39
39
|
Mat parse_csv(const std::string &path)
|
40
40
|
{
|
41
|
+
Mat mat;
|
42
|
+
|
41
43
|
std::ifstream ifs(path);
|
44
|
+
if (!ifs) {
|
45
|
+
std::cout << "Failed to open csv path" << std::endl;
|
46
|
+
return mat;
|
47
|
+
}
|
42
48
|
std::string line;
|
43
|
-
Mat mat;
|
44
49
|
|
45
50
|
while (std::getline(ifs, line)) {
|
46
51
|
// 1行分解析する。
|
@@ -71,5 +76,4 @@
|
|
71
76
|
}
|
72
77
|
}
|
73
78
|
}
|
74
|
-
|
75
79
|
```
|
3
修正
answer
CHANGED
@@ -53,7 +53,12 @@
|
|
53
53
|
|
54
54
|
int main(int, char **)
|
55
55
|
{
|
56
|
+
Mat mat;
|
57
|
+
try {
|
56
|
-
|
58
|
+
mat = parse_csv("sample.csv");
|
59
|
+
} catch (std::exception &e) {
|
60
|
+
std::cout << "Error: " << e.what() << std::endl;
|
61
|
+
}
|
57
62
|
|
58
63
|
// 出力する。
|
59
64
|
if (!mat.empty()) {
|
@@ -66,4 +71,5 @@
|
|
66
71
|
}
|
67
72
|
}
|
68
73
|
}
|
74
|
+
|
69
75
|
```
|
2
修正
answer
CHANGED
@@ -9,6 +9,8 @@
|
|
9
9
|
#include <string>
|
10
10
|
#include <vector>
|
11
11
|
|
12
|
+
using Mat = std::vector<std::vector<double>>;
|
13
|
+
|
12
14
|
/**
|
13
15
|
* @brief 1行分パースする。
|
14
16
|
*
|
@@ -28,11 +30,17 @@
|
|
28
30
|
return values;
|
29
31
|
}
|
30
32
|
|
33
|
+
/**
|
34
|
+
* @brief CSV を解析する。
|
35
|
+
*
|
36
|
+
* @param path CSV ファイルのパス
|
31
|
-
|
37
|
+
* @return Mat 行列
|
38
|
+
*/
|
39
|
+
Mat parse_csv(const std::string &path)
|
32
40
|
{
|
33
|
-
std::ifstream ifs(
|
41
|
+
std::ifstream ifs(path);
|
34
42
|
std::string line;
|
35
|
-
|
43
|
+
Mat mat;
|
36
44
|
|
37
45
|
while (std::getline(ifs, line)) {
|
38
46
|
// 1行分解析する。
|
@@ -40,6 +48,13 @@
|
|
40
48
|
mat.push_back(values);
|
41
49
|
}
|
42
50
|
|
51
|
+
return mat;
|
52
|
+
}
|
53
|
+
|
54
|
+
int main(int, char **)
|
55
|
+
{
|
56
|
+
Mat mat = parse_csv("sample.csv");
|
57
|
+
|
43
58
|
// 出力する。
|
44
59
|
if (!mat.empty()) {
|
45
60
|
std::cout << "matrix size: " << mat.size() << "x" << mat.front().size() << std::endl;
|
1
修正
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
`std::getline()` のオーバーロード関数に区切り文字
|
1
|
+
`std::getline()` のオーバーロード関数に区切り文字まで読み込めるバージョンがあるので、それを使ったほうがカンマ区切りの文字列を解析するのが楽なのではないでしょうか。
|
2
2
|
|
3
3
|
[getline - cpprefjp C++日本語リファレンス](https://cpprefjp.github.io/reference/string/basic_string/getline.html)
|
4
4
|
|
@@ -9,12 +9,19 @@
|
|
9
9
|
#include <string>
|
10
10
|
#include <vector>
|
11
11
|
|
12
|
+
/**
|
13
|
+
* @brief 1行分パースする。
|
14
|
+
*
|
15
|
+
* @param line 行
|
16
|
+
* @param delim 区切り文字
|
17
|
+
* @return std::vector<double> 値の一覧
|
18
|
+
*/
|
12
19
|
std::vector<double> parse_line(const std::string &line, char delim = ',')
|
13
20
|
{
|
14
21
|
std::vector<double> values;
|
22
|
+
|
15
23
|
std::istringstream ss(line);
|
16
24
|
std::string token;
|
17
|
-
|
18
25
|
while (std::getline(ss, token, delim))
|
19
26
|
values.push_back(std::stod(token));
|
20
27
|
|
@@ -43,8 +50,5 @@
|
|
43
50
|
std::cout << std::endl;
|
44
51
|
}
|
45
52
|
}
|
46
|
-
|
47
|
-
return 0;
|
48
53
|
}
|
49
|
-
|
50
54
|
```
|