gpt4 book ai didi

c++ - 如何在不使用 atof 函数的情况下将命令行添加为数字数据?

转载 作者:行者123 更新时间:2023-11-28 06:28:19 25 4
gpt4 key购买 nike

有什么方法可以避免使用 atof 函数?如何将字符串转换为 float ?

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char* argv[])
{
double x = 0;
for(int i=0; i<argc; i++)
x += atof(argv[i]);
cout<<x<<endl;
return 0;
}

最佳答案

您可以使用 stringstream

#include <iostream>
#include <sstream>
using namespace std;

int main () {
int val;
stringstream ss (stringstream::in | stringstream::out);
ss << "120";
ss >> val;
return 0;
}

关于c++ - 如何在不使用 atof 函数的情况下将命令行添加为数字数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28139940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com