当前位置:网站首页>Conversion between numeric types and strings
Conversion between numeric types and strings
2022-06-22 16:16:00 【Ordinary people who like playing basketball】
1. Convert numeric values to strings
Use to_string() Method can easily convert various numeric types to string types , This is an overloaded function , Function declaration in header file in ,
- The function prototype is as follows :
// The header file <string>
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);
- The use of functions is very simple , The sample code is as follows :
#include <iostream>
#include <string>
using namespace std;
int main()
{
string pi = "pi is " + to_string(3.1415926);
string love = "love is " + to_string(5.20 + 13.14);
cout << pi << endl;
cout << love << endl;
return 0;
}
- test :

2. Convert a string to a numeric value
because C++ Numeric types in include integer and floating point types , Therefore, different functions are provided for different types , By calling these functions, you can convert the string type to the corresponding numeric type .
// Defined in header file <string>
int stoi( const std::string& str, std::size_t* pos = 0, int base = 10 );
long stol( const std::string& str, std::size_t* pos = 0, int base = 10 );
long long stoll( const std::string& str, std::size_t* pos = 0, int base = 10 );
unsigned long stoul( const std::string& str, std::size_t* pos = 0, int base = 10 );
unsigned long long stoull( const std::string& str, std::size_t* pos = 0, int base = 10 );
float stof( const std::string& str, std::size_t* pos = 0 );
double stod( const std::string& str, std::size_t* pos = 0 );
long double stold( const std::string& str, std::size_t* pos = 0 );
- str: String to convert
- pos: Out parameter , The record cannot continue to be parsed from which character , such as : 123abc, The outgoing location is 3
- base: if base by 0 , Then the value is automatically detected : If the prefix is 0 , It's octal , If the prefix is 0x or 0X, Is hexadecimal , Otherwise it is decimal .
Although these functions have multiple parameters , But except for the first parameter, everything else has a default value , Generally, using default values can meet the requirements .
- eg:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str1 = "45";
string str2 = "3.14159";
string str3 = "9527 with words";
string str4 = "words and 2";
int myint1 = std::stoi(str1);
float myint2 = std::stof(str2);
int myint3 = std::stoi(str3);
// error : 'std::invalid_argument'
// int myint4 = std::stoi(str4);
cout << "std::stoi(\"" << str1 << "\") is " << myint1 << endl;
cout << "std::stof(\"" << str2 << "\") is " << myint2 << endl;
cout << "std::stoi(\"" << str3 << "\") is " << myint3 << endl;
// cout << "std::stoi(\"" << str4 << "\") is " << myint4 << endl;
}
test :

From the above test procedure, it can be concluded that , stay C++11 These conversion functions are provided to convert strings to numeric values :If all characters in the string are numeric , The entire string will be converted to the corresponding numeric value , And return... Through the return value
If the first half of the string is of numeric type , The second half is not , Then the first half will be converted to the corresponding value , And return... Through the return value
If the first character of a character is not a numeric value, the conversion fails
Reference resources : Conversion between numeric type and string
边栏推荐
- Pymssql Module User Guide
- 信创研究:国产数据库聚焦信创市场,华为Gauss有望成为最强
- Alibaba cloud middleware's open source past
- 10款超牛Vim插件,爱不释手了
- 19、 Xv6 context switching (implementation of context switching; encapsulation and recovery of state machine)
- How safe is the new bond
- 华为云HCDEZ专场暨分布式技术峰会:华为云分布式云原生技术与实践之路
- Navicat Premium 连接Oracle 数据库(图文教程)
- SAP ABAP 对话框编程教程:中的模块池-09
- Application of mongodb in Tencent retail premium code
猜你喜欢

在JFlash中添加未知类型的单片机

Quickly play ci/cd graphical choreography

Program substitution function

SAP ABAP 内部表:创建、读取、填充、复制和删除-06

数睿数据荣获第二届ISIG中国产业智能大会两项年度大奖
New design of databend SQL planner

各位学弟学妹,别再看教材了,时间复杂度看这篇就好了

odoo系统对原有模型单独开发的视图设置优先级

mysql - sql执行过程

Promoting compatibility and adaptation, enabling coordinated development of gbase may adaptation Express
随机推荐
Scala language learning-05-a comparison of the efficiency of recursion and tail recursion
【山大会议】多人视频通话 WebRTC 工具类搭建
各位学弟学妹,别再看教材了,时间复杂度看这篇就好了
[Shanda conference] project initialization
Merge sort of sorting
SAP 中的 ABAP 查询教程:SQ01、SQ02、SQ03-017
【山大会议】应用设置模块
Rosbag使用命令
Discourse 新用户可插入媒体的数量
[Shangshui Shuo series] day three - VIDEO
解决mysql远程登录报权限问题
【单片机】【让蜂鸣器发声】认识蜂鸣器,让蜂鸣器发出你想要的声音
A simple understanding of hill ordering
Linux安装mysql
Pymssql Module User Guide
校企联合在路上!华为云GaussDB又来高校啦
High precision calculation
What is the relationship between CSC securities and qiniu school? Is it safe to open a securities account
【华为云至简致远】征文获奖名单出炉!
Quickly play ci/cd graphical choreography