c++ string 中的rfind函数的一点疑问

爱读庄子的码农 2015-06-10 08:38:59
首先贴一段代码,我用的工具是vs2012,

#include<iostream>
#include<string>
#include<cstring>

using std::cout;
using std::endl;

int main()
{

std::string str("hello world ABCDEFG hello XYZ");

std::string::size_type index = str.rfind("hello",0,std::strlen("hello"));

if(index != std::string::npos)
for(std::string::size_type i = index ; i < str.size(); ++i)
cout<<str[i];
cout<<endl;

system("pause");
return 0;
}


vs2012的运行结果如下:


按照我的理解应该是输出 “hello XYZ” 啊,求大神解释解释!谢谢
...全文
379 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
苏叔叔 2015-06-10
  • 打赏
  • 举报
回复
用这个 std::string::size_type index = str.rfind("hello");
jiqiang01234 2015-06-10
  • 打赏
  • 举报
回复
size_type rfind( value_type _Ch, size_type _Off = npos ) const; size_type rfind( const value_type* _Ptr, size_type _Off = npos ) const; size_type rfind( const value_type* _Ptr, size_type _Off, size_type _Count ) const; size_type rfind( const basic_string<CharType, Traits, Allocator>& _Str, size_type _Off = npos ) const; _Ch The character value for which the member function is to search. _Off Index of the position at which the search is to begin. 注意这里,是从源字符串指定的位置开始,反向查找。可不是从字符串尾查找。 _Ptr The C-string for which the member function is to search. _Count The number of characters, counting forward from the first character, in the C-string for which the member function is to search. _Str The string for which the member function is to search.
pengzhixi 2015-06-10
  • 打赏
  • 举报
回复
std::string::size_type index = str.rfind("hello",str.size(),std::strlen("hello"));
fly_dragon_fly 2015-06-10
  • 打赏
  • 举报
回复
rfind("hello",0,std::strlen("hello")),你指定了从位置0开始找, 把后面的参数去掉改成rfind("hello")

64,318

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧