C/C++中字符串与数值相互转换
2017-10-25 20:06
525 查看
第一种方法:
数字转换成字符串:
字符串转换成数字:
上面方法缺点是处理大量数据转换速度较慢。
******************************************************************************************************************************
第二种方法:
以下内容来源于C++标准库
自C++11开始,C++标准库提供了一些便捷函数,用来将string转换成数值或者反向转换,然而请注意,这些转换只可用于类型 string 和 wstring ,不适用于u16string和 u32string。
对于上面所有“将string转换为数值”的函数,以下都适用:
它们会跳出前导的任何空白字符。
它们允许返回“被处理之最末字符”后的第一个字符的索引。
如果转换无法发生,它们会抛出std::invalid_argument,如果被转换值超出返回类型的可表达范围,它们会抛出std::out_of_rang。
对于整数,你可以(也可以不)传递基数(base)。
对于所有将数值转换为 string 或 wstring 的函数,val 可以是以下任何类型:int、unsigned int、long、unsigned long、unsigned long long、long long、float、double 或者 long double。
举个例子,考虑一下程序:
输出如下:
注意std::stoi("-0x77")会导致0,因为它只解析-0,然后就把x解释为它所找到的数值终点;std::stol("789",&idx,8)只解析string中第一个字符,因为8在八进制中不是个有效字符。
数字转换成字符串:
#include <iostream> #include <sstream> #include <string> using namespace std; string num2str(double i) { stringstream ss; ss << i; return ss.str(); } int main() { double x = 965.3; string s = num2str(x); cout << s << endl; return 0; }
字符串转换成数字:
#include <iostream> #include <sstream> #include <string> using namespace std; int str2num(string str) { int num; stringstream ss(str); ss >> num; return num; } int main() { string str = "123456"; int x = str2num(str); cout << x << endl; return 0; }
上面方法缺点是处理大量数据转换速度较慢。
******************************************************************************************************************************
第二种方法:
以下内容来源于C++标准库
自C++11开始,C++标准库提供了一些便捷函数,用来将string转换成数值或者反向转换,然而请注意,这些转换只可用于类型 string 和 wstring ,不适用于u16string和 u32string。
对于上面所有“将string转换为数值”的函数,以下都适用:
它们会跳出前导的任何空白字符。
它们允许返回“被处理之最末字符”后的第一个字符的索引。
如果转换无法发生,它们会抛出std::invalid_argument,如果被转换值超出返回类型的可表达范围,它们会抛出std::out_of_rang。
对于整数,你可以(也可以不)传递基数(base)。
对于所有将数值转换为 string 或 wstring 的函数,val 可以是以下任何类型:int、unsigned int、long、unsigned long、unsigned long long、long long、float、double 或者 long double。
举个例子,考虑一下程序:
#include <iostream> #include <sstream> #include <string> #include <limits> #include <exception> int main() { try { //convert to numeric type std::cout << std::stoi(" 77") << std::endl; std::cout << std::stod(" 77.7") << std::endl; std::cout << std::stoi("-0x77") << std::endl; //use index of characters not processed std::size_t idx; std::cout << std::stoi(" 42 is the truth", &idx) << std::endl; std::cout << " idx of first unprocessed char: " << idx << std::endl; //use bases 16 and 8 std::cout << std::stoi(" 42", nullptr, 16) << std::endl; std::cout << std::stoi("789", &idx, 8) << std::endl; std::cout << " idx of first unprocessed char: " << idx << std::endl; //convert numerix value to string long long ll = std::numeric_limits<long long>::max(); std::string s = std::to_string(ll);//converts maximum long long to string std::cout << s << std::endl; //try to convert back std::cout << std::stoi(s) << std::endl; //throws out_of_range } catch (const std::exception& e) { std::cout << e.what() << std::endl; } }
输出如下:
注意std::stoi("-0x77")会导致0,因为它只解析-0,然后就把x解释为它所找到的数值终点;std::stol("789",&idx,8)只解析string中第一个字符,因为8在八进制中不是个有效字符。
相关文章推荐
- [Tip: _itow]数值和字符串相互转换(C++ 数据类型转换技巧)
- C++数值与字符串相互转换
- c++中字符串与数值的相互转换
- C++数值与字符串相互转换的那些事(一)字符串转数值(转载请注明)
- 数值和字符串相互转换(C++ 数据类型转换技巧)
- C++数值类型与string的相互转换
- C/C++中int/long/float/double数值类型与字符串互相转换[总结]
- postgreSQL数据类型字符串和数值相互转换
- C/C++中字符串与数字相互转换
- C++字节流与二进制字符串相互转换(一个简单的明文加解密程序)
- (转载)C++中枚举与字符串相互转换
- C++抽象编程——字符串与整数之间的相互转换详解
- 二进制值和十六进制字符串相互转换的C++代码
- C++ 整型和字符串相互转换(转载)
- C/C++中字符串与数字相互转换
- C/C++ 字符串与数字相互转换
- C++数值类型与string的相互转换
- C++中时间与字符串之间的相互转换
- C++中数字与字符串的相互转换
- c++ 将16进制字符串转换成为int数值