C++11的字符串与数值间的类型转换:to_string() stoi stol stoul stoll stof stod stold

本文详细介绍了C++中使用string进行数值转换的方法,包括使用`std::to_string`将整型、浮点型转换为字符串,以及如何通过`stoll`和`stold`将字符串转换回相应的数值类型。
部署运行你感兴趣的模型镜像
C++类型转换 
1 再也不用搞C的那一套了,再也不用什么庞大的stringstream了
string的数值转换函数
2 其中,sto*()提供了针对string和wstring的重载
代码:
#include <string>
#include <iostream>
using namespace std;

template<typename T>
void p(const T& t)
{
	cout<<t<<endl;
}
int main()
{
	long long ll = 1000;
	string str;

	str = std::to_string(ll);
	p(str);

	ll = stoll(str);
	p(ll);

	long double d = 3.141592;
	str = to_string(d);
	p(str);

	d = stold(str);
	p(d);
};
1000
1000
3.14159
3.14159
请按任意键继续. . .

全部函数如下:http://www.cplusplus.com/reference/string/


boost::lexical_cast
由于效率的问题,不建议使用,参考 boost::lexical_cast
old版本(不建议使用,自己造轮子)

代码:

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
using namespace std;

template<typename Result,typename Para>
Result lexical_cast(Para para)
{
    stringstream ss;
    ss<<para;
    Result result;
    ss>>result;
    return result;
}
//int ACE_TMAIN(int argc, ACE_TCHAR *argv[])
int main(int argc, char *argv[])
{

    double arr[10] = {0.1,1.2,2.3,3.4,4.5,5.6,6.7,7.8,8.9,9.0};
    vector<string> str_arr;
    for (size_t i =0 ; i< sizeof(arr)/sizeof(double) ; ++i)
    {
        str_arr.push_back(lexical_cast<string>(arr[i]));
    }
    ostream_iterator<string> out(cout," ");
    copy(str_arr.begin(),str_arr.end(),out);
    
    return 0;
}



您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

### C++ 中将字符串转换为数值的方法 在 C++ 编程语言中,存在多种方法可以实现从字符串数值类型的转换。以下是几种常见的技术及其特点: #### 使用 `std::stoi` 和其变体 C++11 引入了一系列函数用于处理字符串数值的操作,其中包括 `std::stoi`, `std::stol`, `std::stoll`, `std::stoul`, `std::stoull`, `std::stof`, `std::stod`, 和 `std::stold`。这些函数可以直接将字符串解析为对应的整型或浮点型数据。 ```cpp #include <iostream> #include <string> int main() { std::string str = "123"; int num = std::stoi(str); std::cout << "The integer value is: " << num << std::endl; } ``` 上述代码展示了如何通过 `std::stoi` 将字符串 `"123"` 转换为整数类型[^1]。 #### 利用流操作符 (`std::istringstream`) 另一种常用的方式是借助输入字符串流对象来完成转换工作。这种方法更加灵活,允许逐步提取多个值或者处理复杂的格式化需求。 ```cpp #include <sstream> #include <string> int main(){ std::string s="4567"; std::istringstream iss(s); double d=0; iss >> d; if(!iss){ // Handle error case here. } else{ std::cout<<d<<"\n"; } } ``` 这里我们创建了一个基于目标字符串的 `std::istringstream` 对象,并利用右移运算符将其内容赋给变量 `d`。 #### 运用自定义逻辑标准库函数组合 除了内置支持外,还可以手动编写辅助功能以满足特定场景下的要求。比如当面对包含非数字字符的情况时,可先清理掉无关部分再调用前述任一机制执行最终映射过程;又或者是针对不同基数(base)设定专门处理器等等。 值得注意的是,在实际开发过程中应当考虑异常情况的发生概率以及相应的应对策略,确保程序健壮性和用户体验良好度得到保障。 对于类似 Python 的 `convert.toInt()` 或者 Java 的 `Integer.parseInt(String)` 方法,则可以通过上面提到的标准模板库(STL)成员函数达成同等效果[^3]。 ```cpp // Example demonstrating conversion using stoi and handling exceptions. try { size_t pos; long lval = std::stol("-42", &pos); } catch (const std::invalid_argument& ia) { cerr << "Invalid argument: " << ia.what(); } catch (const std::out_of_range& oor) { cerr << "Out of Range error: " << oor.what(); } ``` 以上片段说明了即使遇到非法输入也能妥善处置的情形下进行安全可靠的转型动作.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C++程序员Carea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值