0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

可変長のパラメータ数を含む文字列入力を受け取ってパラメータの配列を返す関数

Last updated at Posted at 2023-04-08

テストコードを書く上で"1.1 0.4 2.3"などの可変長のパラメータを持った文字列入力を扱いたかったので書きました。

// 長さ10まで
double *string_to_double_array(std::string input){
  double *val = (double *)malloc(sizeof(double)*10);
  std::istringstream stream(input);
  int cnt = 0;
  for(;;){
    stream >> *val++; cnt++;
    if(stream.fail()) break;
  }
  return val - cnt; // 先頭に返す
}

int main(){

    double *params = string_to_double_array("1.1 0.4 2.3");
    // params = {1.1, 0.4, 2.3}

    return 0;
}


テストによってパラメータ数が変わるので、これのおかげでかなり便利になりました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?