当前位置:网站首页>std::string::find 返回值的坑
std::string::find 返回值的坑
2022-08-05 01:52:00 【王大渣】
先看一个bug:
string str = "csdn";
unsigned int i = str.find("-");
if (i < 0)
{
cout << "Not Found";
}
else
{
cout << "Found";
}
bug在于,unsigned int不可能<0,所以这段代码一定都会走到else。常见的正确写法大家都知道:
string str = "csdn";
int i = str.find("-");
if (i < 0)
{
cout << "Not Found";
}
else
{
cout << "Found";
}
但实际上string::find的返回值真的是unsigned
string str = "csdn";
auto i = str.find("-");
cout << typeid(i).name() << endl;
这里,string::find不存在时,返回的实际上是string::npos,所以标准写法应该是:
string str = "csdn";
if (str.find("-") == string::npos)
{
cout << "Not Found\n";
}
else
{
cout << "Found\n";
}
边栏推荐
- Transfer Learning - Distant Domain Transfer Learning
- .Net C# 控制台 使用 Win32 API 创建一个窗口
- [Unity Entry Plan] Handling of Occlusion Problems in 2D Games & Pseudo Perspective
- Xunrui cms website cannot be displayed normally after relocation and server change
- 如何发现一个有价值的 GameFi?
- Why is this problem reported when installing oracle11
- Methods commonly used interface automation test framework postman tests
- source program in assembly language
- ORA-00604 ORA-02429
- PHP Skills Assessment
猜你喜欢
《.NET物联网从零开始》系列
The use of pytorch: temperature prediction using neural networks
蓝牙Mesh系统开发四 ble mesh网关节点管理
.Net C# 控制台 使用 Win32 API 创建一个窗口
Transfer Learning - Distant Domain Transfer Learning
Activity Recommendation | Kuaishou StreamLake Brand Launch Conference, witness together on August 10!
迁移学习——Distant Domain Transfer Learning
ExcelPatternTool: Excel table-database mutual import tool
【Unity入门计划】2D游戏中遮挡问题的处理方法&伪透视
LiveVideoStackCon 2022 Shanghai Station opens tomorrow!
随机推荐
Leetcode brushing questions - 22. Bracket generation
Method Overriding and Object Class
day14--postman interface test
Knowledge Points for Network Planning Designers' Morning Questions in November 2021 (Part 2)
没有对象的程序员如何过七夕
[Endnote] Word inserts a custom form of Endnote document format
【Unity入门计划】2D游戏中遮挡问题的处理方法&伪透视
【MySQL系列】- LIKE查询 以%开头一定会让索引失效吗
Opencv - video frame skipping processing
[Redis] Redis installation under Linux
高数_复习_第1章:函数、极限、连续
SAP ERP和ORACLE ERP的区别是哪些?
如何基于OpenVINO POT工具简单实现对模型的量化压缩
超越YOLO5-Face | YOLO-FaceV2正式开源Trick+学术点拉满
AI+小核酸药物|Eleven完成2200万美元种子轮融资
原生js实现多选框全部选中和取消效果
新来个技术总监,把DDD落地的那叫一个高级,服气
<开发>实用工具
Three handshake and four wave in tcp
数仓4.0(三)------数据仓库系统