C++Primer P231 中的一个小问题

yechun 2002-06-17 09:37:12
string::npos
#include<string>
#include<iostream>
int main()
{
string name("AnnaBelle");
int pos = name.find("Anna");
if ( pos == string::npos)
cout << "Anna not found!\n";
else cout << "Anna found at pos: " << pos << endl;
}
里面的string::npo是什么意思啊。 放在开头有什么作用呢?
...全文
71 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yechun 2002-06-18
  • 打赏
  • 举报
回复
我明白了。谢谢各位了。
hello_wyq 2002-06-17
  • 打赏
  • 举报
回复
// support fat_horse(每天进步一点点)

catameng 2002-06-17
  • 打赏
  • 举报
回复
哎,来晚了。同意楼上的
qxp 2002-06-17
  • 打赏
  • 举报
回复
这是一种比较好的编程习惯
Jackstraw 2002-06-17
  • 打赏
  • 举报
回复
string::npos是string类的一个静态常成员变量,恒等于-1,作为find函数失败之后的返回值
CALM 2002-06-17
  • 打赏
  • 举报
回复
奇怪,那页有写啊,你没有看到么

返回特定值string::npos表示没有匹配啊
fat_horse 2002-06-17
  • 打赏
  • 举报
回复
basic_string::npos

static const size_type npos = -1;

The constant is the largest representable value of type size_type. It is assuredly larger than max_size(); hence it serves as either a very large value or as a special code.

delphihero 2002-06-17
  • 打赏
  • 举报
回复
如果 name.find() 失败就返回 string::npos

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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