2017-03-22 11 views
-3

初心者の質問に文字列を変換するが、私は3つの数字を取得した文字列を持っている:など、複数のint型

144.3 432.3 532.3

今、私は

float x; 
float y; 
float z; 
で3台の山車を定義します

どのようにすべての値を内部に入れることができますか?ここで、

x = 144.3; 
y = 432.3; 
z = 532.3; 
+0

[?あなたはfloatの配列に文字列を変換するにはどうすればよい]の可能な重複(http://stackoverflow.com/questions/9986091/how-あなたは文字列を浮動小数点に変換する) – Cecilia

答えて

2

あなたはstd::stringstream使用することができます。

std::stringstream ss("144.3 432.3 532.3"); 
float x, y, z; 
ss >> x >> y >> z; 
+0

完璧に動作します、ありがとう! – PurpleReaver

1

stof標準ライブラリ関数を試してください。

std::string orbits ("686.97 365.24"); 
std::string::size_type sz;  // alias of size_t 

float mars = std::stof (orbits,&sz); 
float earth = std::stof (orbits.substr(sz)); 
関連する問題