020 string与数值转换
2020-02-22 本文已影响0人
赵者也
C++ 11 引入了多个函数,可以实现数值数据与标准库 string 之间的转换。
数值类型 转换为 string
to_string(val) 定义在 头文件 <string> 中,它是一组重载函数,返回值 val 的 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 );
注意:
- 使用 std::to_string 转换浮点类型可能会产生意想不到的结果,因为返回的字符串中的有效数字可能为零,请参见下面的示例。
- 返回值可能与默认情况下 std::cout 打印的值有很大的不同,请参见下面的示例。
- std::to_string 依赖于当前语言环境进行格式化,因此来自多个线程的对 std::to_string 的并发调用可能导致调用的部分序列化。为此,c++ 17 提供 std::to_chars 作为一种与语言环境无关的高性能替代方案。
测试示例:
#include <QCoreApplication>
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
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';
return a.exec();
}
输出结果:
std::cout: 23.43
to_string: 23.430000
std::cout: 1e-09
to_string: 0.000000
std::cout: 1e+40
to_string: 10000000000000000303786028427003666890752.000000
std::cout: 1e-40
to_string: 0.000000
std::cout: 1.23457e+08
to_string: 123456789.000000
string 转换为 数值类型
int stoi( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
int stoi( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );
long stol( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
long stol( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );
long long stoll( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
long long stoll( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );
unsigned long stoul( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
unsigned long stoul( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );
unsigned long long stoull( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
unsigned long long stoull( const std::wstring& str, std::size_t* pos = nullptr, int base = 10 );
float stof( const std::string& str, std::size_t* pos = nullptr );
float stof( const std::wstring& str, std::size_t* pos = nullptr );
double stod( const std::string& str, std::size_t* pos = nullptr );
double stod( const std::wstring& str, std::size_t* pos = nullptr );
long double stold( const std::string& str, std::size_t* pos = nullptr );
long double stold( const std::wstring& str, std::size_t* pos = nullptr );
使用示例:
#include <QCoreApplication>
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::string str1 = "45";
std::string str2 = "3.14159";
std::string str3 = "31337 with words";
std::string str4 = "words and 2";
int myint1 = std::stoi(str1);
int myint2 = std::stoi(str2);
int myint3 = std::stoi(str3);
// error: 'std::invalid_argument'
// int myint4 = std::stoi(str4);
std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';
std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';
std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n';
//std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n';
return a.exec();
}
输出结果:
std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337