搜索空字符串时查找与 find_first_of [英] find vs find_first_of when searching for empty string

查看:32
本文介绍了搜索空字符串时查找与 find_first_of的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 STL 中,当我执行 s.find("") 时,它返回 0 而 s.find_first_of("") 返回 -1 (npos).这种差异的原因是什么?

In STL, when I do s.find(""), it returns 0 while s.find_first_of("") returns -1 (npos). What is the reason for this difference?

推荐答案

s.find(t) 查找子串 ts 中的第一次出现.如果 t 为空,则该事件出现在 s 的开头,并且 s.find(t) 将返回 0.

s.find(t) finds the first occurrence of the substring t in s. If t is empty, then that occurrence is at the beginning of s, and s.find(t) will return 0.

s.find_first_of(t) 查找 t一个字符的第一次出现.如果t为空字符串,则t中没有字符,所以找不到出现,find_first_of会返回npos.

s.find_first_of(t) finds the first occurrence of one of the characters in t. If t is the empty string, then there are no characters in t, so no occurrence can be found, and find_first_of will return npos.

在 ideone 上直播.

这篇关于搜索空字符串时查找与 find_first_of的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆