当前位置:网站首页>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";}
边栏推荐
- [Fortune-telling-60]: "The Soldier, the Tricky Way"-2-Interpretation of Sun Tzu's Art of War
- 云原生(三十二) | Kubernetes篇之平台存储系统介绍
- 使用二维码传输文件的小工具 - QFileTrans 1.2.0.1
- Semi-Decentralized Federated Learning for Cooperative D2D Local Model Aggregation
- 2022-08-04:输入:去重数组arr,里面的数只包含0~9。limit,一个数字。 返回:要求比limit小的情况下,能够用arr拼出来的最大数字。 来自字节。
- shell statement to modify txt file or sh file
- The linear table lookup
- Beidou no. 3 short message terminal high slope in open-pit mine monitoring programme
- Introduction to SDC
- 数据增强Mixup原理与代码解读
猜你喜欢
随机推荐
汉字转拼音
Hash table lookup (hash table)
数据增强Mixup原理与代码解读
Data storage practice based on left-order traversal
QStyle platform style
nodeJs--encapsulate routing
mysql tree structure query problem
Matlab画图3
PostgreSQL数据库 用navicat 打开表结构的时候报错 cannot update secondarysnapshot during a parallel operation 怎么解决?
The 22-07-31 weeks summary
dmp(dump)转储文件
继承关系下构造方法的访问特点
从零到一快速学会三子棋
开源协议说明LGPL
软链接引发的物理备份问题
View handler stepping record
private package
torch.roll()
Syntax basics (variables, input and output, expressions and sequential statements)
1873. 计算特殊奖金