当前位置:网站首页>Precautions for using stoi function
Precautions for using stoi function
2022-07-18 11:17:00 【Peanut butter noodles】
Appeared when writing questions bug, After many times of debugging , Only to find out is stoi There are some things you don't pay attention to when using the function .
int main()
{
string s = "";
cout << stoi(s);
return 0;
}
Similar to the code above ,s Is an empty string , perhaps s There are not all numeric characters in , There will be running errors .
therefore stoi When using, the parameter string must be numeric characters . Here again write the following code .
int main()
{
string s;
cin >> s;
vector<int> record;
istringstream is(s);
string add;
while(getline(is, add, '.')) record.push_back(stoi(add));
for(int i = 0; i < record.size(); i++)
{
cout << record[i] << endl;
}
return 0;
}
Input :
12..23
Output :
Report errors
According to the previous stoi The use of logic , while(getline(is, add, ‘.’) The second string read should be an empty string , Should not call stoi, Why do you report errors , This problem is of the same kind .
边栏推荐
猜你喜欢

Detailed steps for installing mysql8 in centos7.9

基于ADC0832的电量指示电路

Valgrind

Redis concept, configuration and sentinel high availability of redis

微信小程序在加载中时如何显示提示框?

【踩坑】Appending a row → DataFrame

redis之redis概念、配置、Sentinel高可用

双线程猜数字

openpcdet之pointpillar代码阅读——第一篇:数据增强与数据处理

Reading a data driven graph generic model for temporary interaction networks
随机推荐
Using off heap memory
OLED时钟
Offre de doigts 64. Trouver 1 + 2 +... + n
Design of DC motor control system
subset sum problem
基于ADC0832的电位器数值显示
Registration form practical skills [continuous update]
编辑距离问题
Uploading and downloading of files
直流电机控制系统设计
OLED circularly displays picture text
什么是MTTF、MTBF、MTRF?
Buckle exercise - and the shortest subarray of at least k
子集和问题
The digital tube displays numbers circularly
基于ADC0832的电量指示电路
mosquitto.h 学习
Digital display of potentiometer based on ADC0832
Algorithm In Interview
We overestimate the value of coding