学习相关知识,耗时 1 小时

Str2Int Str2UInt Str2ULongLong Str2Double Int2String ……
stoi stof stod stoll stoull to_string
// 以下是 www.cplusplus.com 对 stoi 的说明
int stoi (const string& str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);
Convert string to integer
Parses str interpreting its content as an integral number of the specified base,
which is returned as an int value.
If idx is not a null pointer,
the function also sets the value of idx to the position of the first character in str after the number.
复制
atoi atof atol atoll
// 以下是 www.cplusplus.com 对 atoi 的说明
int atoi (const char * str);
Convert string to integer
Parses the C-string str interpreting its content as an integral number,
which is returned as a value of type int.
The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found.
Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.
复制
stoi 的入参是 string,atoi 的入参是 c-string(char*) atoi 会处理前面多余的空格 atoi 的入参如果不是一个数字型,会返回 0
string s = " 10a";
cout << "s:" << stoi(s) << endl; // 10
cout << "s:" << atoi(s.c_str()) << endl; // 10
s = "";
// cout << "s:" << stoi(s) << endl; core
cout << "s:" << atoi(s.c_str()) << endl; // 0
s = " ";
// cout << "s:" << stoi(s) << endl; core
cout << "s:" << atoi(s.c_str()) << endl; // 0
s = " a10";
// cout << "s:" << stoi(s) << endl; core
cout << "s:" << atoi(s.c_str()) << endl; // 0
复制
第一个非空字符是数字,stoi 和 atoi 都能成功转换 第一个非空字符不是数字,stoi 会 core 掉,atoi 返回 0
try
{
int i = boost::lexical_cast<int>("1");
}
catch (boost::bad_lexical_cast&)
{
continue;
}
复制
文章转载自每天一个开发小知识,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
2025年4月中国数据库流行度排行榜:OB高分复登顶,崖山稳驭撼十强
墨天轮编辑部
1889次阅读
2025-04-09 15:33:27
2025年3月国产数据库大事记
墨天轮编辑部
872次阅读
2025-04-03 15:21:16
2025年3月国产数据库中标情况一览:TDSQL大单622万、GaussDB大单581万……
通讯员
605次阅读
2025-04-10 15:35:48
征文大赛 |「码」上数据库—— KWDB 2025 创作者计划启动
KaiwuDB
497次阅读
2025-04-01 20:42:12
数据库,没有关税却有壁垒
多明戈教你玩狼人杀
488次阅读
2025-04-11 09:38:42
国产数据库需要扩大场景覆盖面才能在竞争中更有优势
白鳝的洞穴
466次阅读
2025-04-14 09:40:20
最近我为什么不写评论国产数据库的文章了
白鳝的洞穴
408次阅读
2025-04-07 09:44:54
天津市政府数据库框采结果公布!
通讯员
361次阅读
2025-04-10 12:32:35
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
359次阅读
2025-04-17 17:02:24
优炫数据库成功入围新疆维吾尔自治区行政事业单位数据库2025年框架协议采购!
优炫软件
338次阅读
2025-04-18 10:01:22