当前位置:网站首页>The pit of std::string::find return value
The pit of std::string::find return value
2022-08-05 02:48:00 【Wang Dazhao】
Look at a bug first:
string str = "csdn";unsigned int i = str.find("-");if (i < 0){cout << "Not Found";}else{cout << "Found";}The bug is that unsigned int cannot be < 0, so this code must go to else.We all know the common correct spelling:
string str = "csdn";int i = str.find("-");if (i < 0){cout << "Not Found";}else{cout << "Found";}But in fact the return value of string::find is really unsigned
string str = "csdn";auto i = str.find("-");cout << typeid(i).name() << endl;Here, when string::find does not exist, it actually returns string::npos, so the standard writing should be:
string str = "csdn";if (str.find("-") == string::npos){cout << "Not Found\n";}else{cout << "Found\n";}边栏推荐
- 1667. 修复表中的名字
- 2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。
- The problem of lack of dynamic library "libtinfo.so.5" in ksql application under UOS system
- shell statement to modify txt file or sh file
- HDU 1114:Piggy-Bank ← 完全背包问题
- 从零到一快速学会三子棋
- VSCode Change Default Terminal 如何修改vscode的默认terminal
- mysql树状结构查询问题
- 2022-08-04: Input: deduplicated array arr, the numbers in it only contain 0~9.limit, a number.Return: The maximum number that can be spelled out with arr if the requirement is smaller than limit.from
- View handler stepping record
猜你喜欢
随机推荐
dmp (dump) dump file
The 22-07-31 weeks summary
北斗三号短报文终端露天矿山高边坡监测方案
leetcode - a subtree of another tree
Physical backup issues caused by soft links
sql server 安装提示用户名不存在
private封装
Data to enhance Mixup principle and code reading
Introduction to SDC
word分栏小记
One hundred - day plan -- -- DAY2 brush
语法基础(变量、输入输出、表达式与顺序语句)
In 2022, you still can't "low code"?Data science can also play with Low-Code!
[ROS] (10) ROS Communication - Service Communication
Semi-Decentralized Federated Learning for Cooperative D2D Local Model Aggregation
Chinese characters to Pinyin
[C language] Detailed explanation of stacks and queues (define, destroy, and data operations)
Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
select 标签自定义样式
百日刷题计划 ———— DAY2








