【问题标题】:Converting a string into multiple ints将一个字符串转换为多个整数
【发布时间】:2017-03-22 20:36:57
【问题描述】:

新手问题,但我有一个字符串可以得到 3 个数字,例如:

144.3 432.3 532.3

现在我用

定义了 3 个浮点数
float x;
float y;
float z;

如何将所有值放入其中?哪里,

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

【问题讨论】:

标签: c++ string int


【解决方案1】:

你可以使用std::stringstream:

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

【讨论】:

    【解决方案2】:

    试试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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-06
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多