teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

10

先程の編集でコードが動かなくなったので編集

2020/07/16 02:54

投稿

退会済みユーザー
answer CHANGED
@@ -53,10 +53,11 @@
53
53
  using XY = std::vector<TwoDimVec>;//xyの動的配列
54
54
 
55
55
  //
56
- // std::ifstreamからXY情報取得
56
+ // std::ifstreamからxy型への変換
57
57
  //
58
58
  XY ifstream2xy(std::ifstream& ifs, unsigned int start_line)noexcept
59
59
  {
60
+
60
61
  std::string one_line;
61
62
  std::vector<std::string> text;
62
63
 
@@ -79,7 +80,7 @@
79
80
  }
80
81
 
81
82
  //
82
- // XY型のオブジェクトからstd::stringへの変換
83
+ // XY型からstd::stringへの変換
83
84
  //
84
85
  std::string xy2str(const XY& xy, unsigned int start_line)noexcept{
85
86
  std::stringstream result;
@@ -100,7 +101,7 @@
100
101
  //
101
102
  std::ifstream ifs(input_path);//入力ストリーム
102
103
  std::ofstream ofs(output_path);//出力ストリーム
103
- auto xy = ifstream2xy(ifs, start_line);//ifsからXY情報取得
104
+ auto xy = ifstream2xy(ifs, start_line);//ifsからxy型への変換
104
105
 
105
106
  //
106
107
  // ここからxyの加工を行う
@@ -112,7 +113,7 @@
112
113
  // ここからxyを9th.outへ書き込むための処理
113
114
  //
114
115
  auto str = xy2str(xy, start_line);//xyからstd::stringへ変換
115
- //std::cout << str << std::endl;//strを標準出力
116
+ std::cout << str << std::endl;//strを標準出力
116
117
 
117
118
  ofs << str;//ファイル出力
118
119
 

9

単純化を狙いsscanfのエラーチェック除外

2020/07/16 02:53

投稿

退会済みユーザー
answer CHANGED
@@ -69,9 +69,8 @@
69
69
  unsigned int ct = 0;
70
70
  for(const auto& line:text){
71
71
  if(ct>=start_line){
72
- if(sscanf(line.c_str(), "%d,%d", &x, &y)!=EOF){
72
+ sscanf(line.c_str(), "%d,%d", &x, &y);
73
- result.push_back({x,y});
73
+ result.push_back({x,y});
74
- }
75
74
  }
76
75
  ++ct;
77
76
  }

8

不備を修正

2020/07/16 02:52

投稿

退会済みユーザー
answer CHANGED
@@ -25,7 +25,7 @@
25
25
  という二次元ベクトルの複数記載されたテキストファイルがあり
26
26
  それを加工して9th.outというテキストファイルに保存
27
27
  したい...
28
- というのであればsscanfを使っているの
28
+ というのであればsscanfを使っていたりエラー処理省いていたり
29
29
  あまりいいコードとは言えませんが
30
30
  下記のコードを参考にしていただいたらどうでしょうか。
31
31
 

7

sscanfが危険な関数であることを記載

2020/07/16 02:48

投稿

退会済みユーザー
answer CHANGED
@@ -25,7 +25,8 @@
25
25
  という二次元ベクトルの複数記載されたテキストファイルがあり
26
26
  それを加工して9th.outというテキストファイルに保存
27
27
  したい...
28
- というのであればエラー処理など省いていますが
28
+ というのであればsscanfを使っているので
29
+ あまりいいコードとは言えませんが
29
30
  下記のコードを参考にしていただいたらどうでしょうか。
30
31
 
31
32
 

6

不要な部分削除

2020/07/16 01:41

投稿

退会済みユーザー
answer CHANGED
@@ -70,8 +70,6 @@
70
70
  if(ct>=start_line){
71
71
  if(sscanf(line.c_str(), "%d,%d", &x, &y)!=EOF){
72
72
  result.push_back({x,y});
73
- }else{
74
- //エラー処理
75
73
  }
76
74
  }
77
75
  ++ct;

5

誤解を招きそうな部分修正

2020/07/16 00:54

投稿

退会済みユーザー
answer CHANGED
@@ -81,7 +81,7 @@
81
81
  }
82
82
 
83
83
  //
84
- // XY型からstd::stringへの変換
84
+ // XY型のオブジェクトからstd::stringへの変換
85
85
  //
86
86
  std::string xy2str(const XY& xy, unsigned int start_line)noexcept{
87
87
  std::stringstream result;

4

無駄な部分修正

2020/07/16 00:35

投稿

退会済みユーザー
answer CHANGED
@@ -56,10 +56,8 @@
56
56
  //
57
57
  XY ifstream2xy(std::ifstream& ifs, unsigned int start_line)noexcept
58
58
  {
59
- using Text = std::vector<std::string>;
60
-
61
59
  std::string one_line;
62
- Text text;
60
+ std::vector<std::string> text;
63
61
 
64
62
  while(getline(ifs, one_line))text.push_back(one_line);
65
63
 

3

誤記修正

2020/07/16 00:34

投稿

退会済みユーザー
answer CHANGED
@@ -110,7 +110,7 @@
110
110
  // ここからxyの加工を行う
111
111
  //
112
112
  //xy[2][1] = 12345;//二行目のyを12345に書き換える
113
- for(auto&& line:xy)for(auto&& axis:line)axis += 10;//全要素に10を足す
113
+ for(auto& line:xy)for(auto& axis:line)axis += 10;//全要素に10を足す
114
114
 
115
115
  //
116
116
  // ここからxyを9th.outへ書き込むための処理

2

誤表現修正

2020/07/16 00:30

投稿

退会済みユーザー
answer CHANGED
@@ -52,7 +52,7 @@
52
52
  using XY = std::vector<TwoDimVec>;//xyの動的配列
53
53
 
54
54
  //
55
- // std::ifstreamからXY型への取得
55
+ // std::ifstreamからXY情報取得
56
56
  //
57
57
  XY ifstream2xy(std::ifstream& ifs, unsigned int start_line)noexcept
58
58
  {
@@ -104,7 +104,7 @@
104
104
  //
105
105
  std::ifstream ifs(input_path);//入力ストリーム
106
106
  std::ofstream ofs(output_path);//出力ストリーム
107
- auto xy = ifstream2xy(ifs, start_line);//ifsからXYオブジェクト取得
107
+ auto xy = ifstream2xy(ifs, start_line);//ifsからXY情報取得
108
108
 
109
109
  //
110
110
  // ここからxyの加工を行う

1

誤解を招きそうな部分修正

2020/07/16 00:25

投稿

退会済みユーザー
answer CHANGED
@@ -52,7 +52,7 @@
52
52
  using XY = std::vector<TwoDimVec>;//xyの動的配列
53
53
 
54
54
  //
55
- // std::ifstreamからxy型への変換
55
+ // std::ifstreamからXY型への取得
56
56
  //
57
57
  XY ifstream2xy(std::ifstream& ifs, unsigned int start_line)noexcept
58
58
  {
@@ -104,7 +104,7 @@
104
104
  //
105
105
  std::ifstream ifs(input_path);//入力ストリーム
106
106
  std::ofstream ofs(output_path);//出力ストリーム
107
- auto xy = ifstream2xy(ifs, start_line);//ifsからxy型への変換
107
+ auto xy = ifstream2xy(ifs, start_line);//ifsからXYオブジェクト取得
108
108
 
109
109
  //
110
110
  // ここからxyの加工を行う