当前位置:网站首页>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";
}边栏推荐
- [Word] #() error occurs after Word formula is exported to PDF
- 张驰咨询:揭晓六西格玛管理(6 Sigma)长盛不衰的秘密
- [Endnote] Word inserts a custom form of Endnote document format
- 接口自动化测试框架postman tests常用方法
- MySQL3
- “嘀哩哩,等灯等灯”,工厂安全生产的提示音
- AI+PROTAC|dx/tx完成500万美元种子轮融资
- 记录谷歌gn编译时碰到的一个错误“I could not find a “.gn“ file ...”
- JZ搜索引擎solr研究-从数据库创建索引
- MySQL learning
猜你喜欢

VOC格式数据集转COCO格式数据集

Exploding the circle of friends, Alibaba produced billion-level concurrent design quick notes are too fragrant

Are testing jobs so hard to find?I am 32 this year and I have been unemployed for 2 months. What should an older test engineer do next to support his family?

测试工作这么难找吗?今年32,失业2个月,大龄测试工程师接下来该拿什么养家?

多线程涉及的其它知识(死锁(等待唤醒机制),内存可见性问题以及定时器)

hypervisor相关的知识点

安装oracle11的时候为什么会报这个问题

Live playback including PPT download | Build Online Deep Learning based on Flink & DeepRec

Xunrui cms website cannot be displayed normally after relocation and server change

从一次数据库误操作开始了解MySQL日志【bin log、redo log、undo log】
随机推荐
How to create an rpm package
dotnet 6 为什么网络请求不跟随系统网络代理变化而动态切换代理
意识形态的机制
Three handshake and four wave in tcp
2022杭电多校第一场
迅睿cms网站搬迁换了服务器后网站不能正常显示
LiveVideoStackCon 2022 Shanghai Station opens tomorrow!
VOC格式数据集转COCO格式数据集
EBS uses virtual columns and hint hints to optimize sql case
从一次数据库误操作开始了解MySQL日志【bin log、redo log、undo log】
Chapter 09 Use of Performance Analysis Tools [2. Index and Tuning] [MySQL Advanced]
AI+PROTAC|dx/tx完成500万美元种子轮融资
释放技术创新引擎,英特尔携手生态合作伙伴推动智慧零售蓬勃发展
树形查找(二叉查找树)
"Configuration" is a double-edged sword, it will take you to understand various configuration methods
开篇-开启全新的.NET现代应用开发体验
行业现状?互联网公司为什么宁愿花20k招人,也不愿涨薪留住老员工~
安装oracle11的时候为什么会报这个问题
IJCAI2022 | DictBert:采用对比学习的字典描述知识增强的预训练语言模型
蓝牙Mesh系统开发四 ble mesh网关节点管理