当前位置:网站首页>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";}
边栏推荐
- Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
- Regular expression to match a certain string in the middle
- 继承关系下构造方法的访问特点
- 2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。
- Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!
- ARM Mailbox
- LeetCode使用最小花费爬楼梯----dp问题
- leetcode - a subtree of another tree
- 注意潍坊开具发票一般需要注意
- 协作D2D局部模型聚合的半分散联合学习
猜你喜欢
Principle and Technology of Virtual Memory
In 2022, you still can't "low code"?Data science can also play with Low-Code!
select tag custom style
VSCode Change Default Terminal how to modify the Default Terminal VSCode
mysql没法Execute 大拿们求解
云原生(三十二) | Kubernetes篇之平台存储系统介绍
线上MySQL的自增id用尽怎么办?
剑指offer专项突击版第20天
C语言实现简单猜数字游戏
【解密】OpenSea免费创造的NFT都没上链竟能出现在我的钱包里?
随机推荐
Open Source License Description LGPL
[Fortune-telling-60]: "The Soldier, the Tricky Way"-2-Interpretation of Sun Tzu's Art of War
采用redis缓存的linux主从同步服务器图片硬盘满了移到新目录要修改哪些指向
C student management system Find student nodes based on student ID
甘特图来啦,项目管理神器,模板直接用
The linear table lookup
[In-depth study of 4G/5G/6G topic-51]: URLLC-16-"3GPP URLLC related protocols, specifications, and technical principles in-depth interpretation"-11-High reliability technology-2-Link adaptive enhancem
mysql没法Execute 大拿们求解
Unleashing the engine of technological innovation, Intel joins hands with ecological partners to promote the vigorous development of smart retail
QT MV\MVC structure
Everyone in China said data, you need to focus on core characteristic is what?
How Jin Cang database correctness verification platform installation file
腾讯云【Hiflow】新时代自动化工具
C语言实现简单猜数字游戏
汉字转拼音
select 标签自定义样式
leetcode - a subtree of another tree
金仓数据库如何验证安装文件平台正确性
Quickly learn chess from zero to one
基于左序遍历的数据存储实践