C++ 如何将字符串转换为复数<;浮动>;在C++;?
如何轻松地将包含两个由逗号分隔的浮点数的字符串转换为复数 例如:C++ 如何将字符串转换为复数<;浮动>;在C++;?,c++,string,complex-numbers,C++,String,Complex Numbers,如何轻松地将包含两个由逗号分隔的浮点数的字符串转换为复数 例如: string s = "123,5.3";//input complex<float> c(123,5.3);//output/what I need string s=“123,5.3”//输入 复合物c(123,5.3)//输出/我需要的 有没有比拆分字符串、读取两个值并返回复杂值更简单/更快的方法?只需添加括号,默认的运算符>即可: #include <iostream> #include <
string s = "123,5.3";//input
complex<float> c(123,5.3);//output/what I need
string s=“123,5.3”//输入
复合物c(123,5.3)//输出/我需要的
有没有比拆分字符串、读取两个值并返回复杂值更简单/更快的方法?只需添加括号,默认的
运算符>
即可:
#include <iostream>
#include <string>
#include <complex>
#include <sstream>
int main()
{
std::string s = "123,5.3";//input
std::istringstream is('(' + s + ')');
std::complex<float> c;
is >> c;
std::cout << "the number is " << c << "\n";
}
#包括
#包括
#包括
#包括
int main()
{
std::string s=“123,5.3”;//输入
std::istringstream是(“(“+s+”)”);
std::复合物c;
is>>c;
std::cout您可以在字符串周围添加括号,然后类将为您读入数字
std::complex<float> c = boost::lexical_cast<std::complex<float> >('('+s+')');