当前位置:网站首页>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";}边栏推荐
猜你喜欢

RAID disk array

Everyone in China said data, you need to focus on core characteristic is what?

word column notes

Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!

Regular expression to match a certain string in the middle

nodeJs--encapsulate routing

Compressed storage of special matrices

云原生(三十二) | Kubernetes篇之平台存储系统介绍
![[LeetCode Brush Questions] - Sum of Numbers topic (more topics to be added)](/img/ee/6b52072c841af99488dc0c1141c74c.png)
[LeetCode Brush Questions] - Sum of Numbers topic (more topics to be added)

你要的七夕文案,已为您整理好!
随机推荐
腾讯云【Hiflow】新时代自动化工具
QT MV\MVC structure
1484. 按日期分组销售产品
How OpenGL works
Hash table lookup (hash table)
人人都在说的数据中台,你需要关注的核心特点是什么?
leetcode - symmetric binary tree
1484. Sell Products by Date
1667. Fix names in tables
Everyone in China said data, you need to focus on core characteristic is what?
Optimizing the feed flow encountered obstacles, who helped Baidu break the "memory wall"?
1873. 计算特殊奖金
Countdown to 2 days|Cloud native Meetup Guangzhou Station, waiting for you!
Tencent Cloud [Hiflow] New Era Automation Tool
Access Characteristics of Constructor under Inheritance Relationship
nodeJs--encapsulate routing
常见的硬件延迟
Multithreading (2)
语法基础(变量、输入输出、表达式与顺序语句)
QT语言文件制作