c++ string和其他类型互转
来源:互联网 发布:mp3剪切合并软件 编辑:程序博客网 时间:2024/05/29 08:20
c++中string和其他基本数据类型的转化总是需要自己实现,但是c++11给广大的c++程序员带来了福音,c++基础类型的相互转化变得非常简单。
- 其他类型 -> string
std::string to_string( int value );std::string to_string( long value );std::string to_string( long long value );std::string to_string( unsigned value );std::string to_string( unsigned long value );std::string to_string( unsigned long long value );std::string to_string( float value );std::string to_string( double value );std::string to_string( long double value );
例子:#include <iostream>#include <string> int main() {double f = 23.43;double f2 = 1e-9;double f3 = 1e40;double f4 = 1e-40;double f5 = 123456789;std::string f_str = std::to_string(f);std::string f_str2 = std::to_string(f2); // Note: returns "0.000000"std::string f_str3 = std::to_string(f3); // Note: Does not return "1e+40".std::string f_str4 = std::to_string(f4); // Note: returns "0.000000"std::string f_str5 = std::to_string(f5);std::cout << "std::cout: " << f << '\n'<< "to_string: " << f_str << "\n\n"<< "std::cout: " << f2 << '\n'<< "to_string: " << f_str2 << "\n\n"<< "std::cout: " << f3 << '\n'<< "to_string: " << f_str3 << "\n\n"<< "std::cout: " << f4 << '\n'<< "to_string: " << f_str4 << "\n\n"<< "std::cout: " << f5 << '\n'<< "to_string: " << f_str5 << '\n';}
- string -> int
int stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );int stoi( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
- string -> long
long stol( const std::string& str, std::size_t* pos = 0, int base = 10 );long stol( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
- string -> long long
long long stoll( const std::string& str, std::size_t* pos = 0, int base = 10 );long long stoll( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
- string -> unsigned long
unsigned long stoul( const std::string& str, std::size_t* pos = 0, int base = 10 );unsigned long stoul( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
- string -> unsigned long long
unsigned long long stoull( const std::string& str, std::size_t* pos = 0, int base = 10 );unsigned long long stoull( const std::wstring& str, std::size_t* pos = 0, int base = 10 );
- string -> float
float stof( const std::string& str, std::size_t* pos = 0 );float stof( const std::wstring& str, std::size_t* pos = 0 );
- string -> double
double stod( const std::string& str, std::size_t* pos = 0 );double stod( const std::wstring& str, std::size_t* pos = 0 );
- string -> long double
long double stold( const std::string& str, std::size_t* pos = 0 );long double stold( const std::wstring& str, std::size_t* pos = 0 );
阅读全文
0 0
- c++ string和其他类型互转
- String类型和Date类型的互转,以及其他类型
- c/c++string 转化其他类型
- c/c++ string和double类型互转
- char类型和string类型(C++,C#)
- char类型和string类型(C++,C#)
- js string 转其他类型收集
- String类型和Date类型互转
- String类型和Date类型互转
- 使用stringstream 来进行string和其他类型的转换
- QT中string和其他类型之间的转换
- java里面String字符串和其他类型转换
- Java中String类型和其他数据类型之间的转换
- C++中string 和其他类型之间的转换
- String和InputStream类型互转
- Cstring 和其他类型间的互转
- UE4 字符和其他类型互转,字符串拼接
- C#string类型的特性和方法
- oracle initialization or shutdown in progress解决方法
- centos7.3下二进制安装mysql5.7.19记录
- BZOJ1022: [SHOI2008]小约翰的游戏John
- Python基础小笔记《2017-10-18》
- java面试题-java虚拟机(JVM)
- c++ string和其他类型互转
- tomcat对https配置
- [BZOJ1076]奖励关-状压+期望
- 全面理解Android中的Px,DPI,DIP,Density,Sp等概念
- 行人属性“Person Attribute Recognition with a Jointly-trained Holistic CNN Model”
- hbase集群搭建
- ubuntu16.04(64位)兼容32位程序
- 表
- js 解决 select option 过多 ,筛选过滤功能