C++类型转换
在哪里可以找到类型转换的概述,例如字符串到整数等?
由于到目前为止的评论,我将澄清:我正在寻找一个列表/表格,上面写着: 要将字符串转换为 int,请使用: ... 对于其他数据类型(如果可能)也是如此:double 到 int,char 到 string,...
Where can I find an overview of type conversion, e.g. string to integer, etc.?
Because of the comments so far, I'll clarify: I'm looking for a list /table that says:
To convert a string to an int, use: ... And same for other data types (where possible): double to int, char to string, ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议阅读有关字符串类的参考,然后阅读有关通常转换为不同的数据类型。
I would recommend to read the reference about the string class and then something about casting to different data types generally.
如果它是与其他类型之间的字符串,则 stringstream 或 boost::lexical_cast。
对于其他类型,这将取决于类型,但也许可以查找标准演员模板? static_cast 和 dynamic_cast 应该可以完成您需要的大部分操作,或者 const_cast 和 reinterpret_cast 往往只对处理遗留系统有用。
If it's string to/from other types then stringstream or boost::lexical_cast.
For other types it will depend on the types, but maybe look up the standard cast templates? static_cast and dynamic_cast should do most things you need, or there is const_cast and reinterpret_cast which tend to only be useful for dealing with legacy systems.
流本质上是 C++ 的字符串转换运算符。
您还可以使用 C 的 sprintf 转换方法,但这种方法非常容易出错且不安全。
Streams are essentially C++'s string conversion operators.
You also have available C's conversion method of sprintf, but that is massively error-prone and unsafe.
标准库中有一些函数可以进行 string<->int 转换。你应该找一本关于 C++ 的参考书,或者在 Google 上搜索。
我更熟悉 C,但我相信 C++ 函数是相同的:atoi、strtoi 等。
There are functions in the standard library to do string<->int conversions. You should grab any reference book on C++, or search Google.
I'm more familiar with C, but I believe the C++ functions are the same: atoi, strtoi, etc.