当前位置:网站首页>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";}
边栏推荐
- Regular expression to match a certain string in the middle
- undo problem
- 人人都在说的数据中台,你需要关注的核心特点是什么?
- HDU 1114:Piggy-Bank ← 完全背包问题
- 2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。
- 02 [Development Server Resource Module]
- [深入研究4G/5G/6G专题-51]: URLLC-16-《3GPP URLLC相关协议、规范、技术原理深度解读》-11-高可靠性技术-2-链路自适应增强(根据无线链路状态动态选择高可靠性MCS)
- Likou - preorder traversal, inorder traversal, postorder traversal of binary tree
- 倒计时 2 天|云原生 Meetup 广州站,等你来!
- tree table lookup
猜你喜欢
C language diary 9 3 kinds of statements of if
Compressed storage of special matrices
dmp(dump)转储文件
你要的七夕文案,已为您整理好!
Matlab drawing 3
从零到一快速学会三子棋
mysql can't Execute, please solve it
What should I do if the self-incrementing id of online MySQL is exhausted?
【 2 】 OpenCV image processing: basic knowledge of OpenCV
继承关系下构造方法的访问特点
随机推荐
常见的硬件延迟
Pisanix v0.2.0 released | Added support for dynamic read-write separation
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
[Fortune-telling-60]: "The Soldier, the Tricky Way"-2-Interpretation of Sun Tzu's Art of War
云原生(三十二) | Kubernetes篇之平台存储系统介绍
【 2 】 OpenCV image processing: basic knowledge of OpenCV
HDU 1114:Piggy-Bank ← 完全背包问题
[ROS] (10) ROS Communication - Service Communication
后期学习计划
Matlab drawing 3
HDU 1114: Piggy-Bank ← The Complete Knapsack Problem
One hundred - day plan -- -- DAY2 brush
1484. Sell Products by Date
QT: The Magical QVarient
Access Characteristics of Constructor under Inheritance Relationship
基于左序遍历的数据存储实践
mysql没法Execute 大拿们求解
undo问题
数据增强Mixup原理与代码解读
leetcode - a subtree of another tree