`
ljl_xyf
  • 浏览: 618693 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

string::npos在 c++ 的find_first_of判断是什么意思

    博客分类:
  • c++
阅读更多

string::npos的解释如下:

 

昨天写的逻辑判断,对find返回npos的意义理解的很模糊,查看了cpp primer后发现下面的内容,得知find方法返回一个名为 string::npos 的特殊值,说明查找没有匹配。粘上以备记忆.

The string class provides six search functions, each named as a variant of find . The operations all return a string::size_type value that is the index of where the match occurred, or a special value named string::npos if there is no match. The string class defines npos as a value that is guaranteed to be greater than any valid index.

string 类提供了 6 种查找函数,每种函数以不同形式的 find 命名。这些操作全都返回 string::size_type 类型的值,以下标形式标记查找匹配所发生的位置;或者返回一个名为 string::npos 的特殊值,说明查找没有匹配。string 类将 npos 定义为保证大于任何有效下标的值。

 

 

 

例如:

 

string str="http://www.my400800.cn ";

pos=str.find_first_of("www");

if(pos!=string::npos)

{

 

 

}

分享到:
评论

相关推荐

    详解C++ string常用截取字符串方法

    find_last_of(string strSub, npos); 其中strSub是需要寻找的子字符串,npos为查找起始位置。找到返回子字符串首次出现的位置,否则返回-1; 注: (1)find_last_of的npos为从末尾开始寻找的位置。 (2)下文中用到...

    C++ string 字符串查找匹配实例代码

    在写C++程序中,总会遇到要从一个字符串中查找一小段子字符串的情况,...find_first_of():在一个目标串中进行查找,返回值是第一个与指定字符组中任何字符匹配的字符位置。如果没有查找到匹配的内容,则返回npos。 fin

    C++_STL_示例

    C++ STL_示例word 格式 提供例程 string vector list等数据结构示例 #include #include<string> using namespace std; void main() { //用const char * 构造strText对象 string strText("This is a test"); //...

    leetcode双人赛-MyLeetCode:我的LeetCode

    i=s.find_first_not_of(' '); if(i==string::npos) return false; int j=s.find_last_not_of(' '); // mn s=s.substr(i,j-i+1); 比较繁琐,有些细节要注意 KMP 1.3 链表 链表 1. 防止删除后链表为空 2. 递归的思路 ...

    基于IPicture图片浏览,带滚动条的对话框

    ScrollWindow(0, nPostion - nPos); SetScrollPos (SB_VERT, nPos); } break; case SB_LINELEFT: { nPostion = GetScrollPos (SB_VERT); SetScrollPos (SB_VERT, nPostion - 1); if ...

    【C++17】新特性梳理

    目录 if init表达式 structual bindings inline变量 std::string_view   if init表达式 C++17语言引入了一个新版本的if/switch语句...= string::npos) { cout << it << - Hello\n; } if(const auto it =

    delphi中如何读取有逗号分割符数据

    nPos:integer):string; var sList:TStringList; begin Result:=''; sList:=TStringList.Create; sList.Text:=StringReplace(Astring,Dlmter,#13#10,[rfReplaceAll]); if (nPos>0) and (sList.Count>=nPos) then...

    C++MFC教程

    Visual C++MFC入门教程 目录 +-- 第一章 VC入门 |------ 1.1 如何学好VC |------ 1.2 理解Windows消息机制 |------ 1.3 利用Visual C++/MFC开发Windows程序的优势 |------ 1.4 利用MFC进行开发的通用方法介绍 |----...

    STL 备忘录

    STL备忘录,使用技巧,初学者进阶的首选,如:string.find等查找的结果要和string::npos比较,而不是和-1比较。(各个平台可能不同)

    MFC实现一个可以控制进度的Flash播放器

    nCurPos = nPos; } int nMin, nMax; pScrollBar->GetScrollRange(&nMin, &nMax); if(nCurPos ) pScrollBar->SetScrollPos(0); else if(nCurPos > nMax) pScrollBar->SetScrollPos(nMax); else ...

    北大天网搜索引擎TSE源码

    = string::npos )" Comment the right paragraph. 3) In Crawl.cpp file, i. "if( iPage.m_sContentType != "text/html" Comment the right paragraph. ii. find "if(file_length )" Choose right one line. ...

    EasyHook 函数钩子 最好的完整稳定的钩子Demo程序(VS2010 C++ 版本)

    Inject.exe是用MFC写的界面程序,只需要在界面上输入进程ID就能正确的HOOK上相应的进程,操作起来非常的简便。 这个Demo的代码风格也非常的好,用VS2010成功稳定编译通过,非常值得下载使用。 部分代码片段摘录如下...

    windos编程(通讯录)

    while( (nPos = sTemp.Find( szBad )) > -1 ) { sCooked = sTemp.Mid( nStart, nPos ); sCooked += szGood; sTemp = sCooked + sTemp.Right( sTemp.GetLength() - (nPos + nBadLength) ); } return sTemp;...

    界面技术-对话框的内容滚动显示.doc

    Selecting the "Horizontal Scroll" and "Verticla Scroll" styles among the properties of your dialog box in the resource editor, you can add scroll bars to the dialog box. Remember also to select the '...

    stdafx.h代码

    stdafx.h的代码// This is a part of the Microsoft Foundation Classes C++ library. // Copyright (C) 1992-1998 Microsoft Corporation // All rights reserved. // // This source code is only intended as a ...

    ICTC-6

    eWeight:2, nPOS:0, sWord: C@末 row:4, col:6, eWeight:3, nPOS:0, sWord: D@末 row:5, col:6, eWeight:1, nPOS:0, sWord: E@末 2、计算出每个结点上可达最短路的PreNode 在求解N-最短路径之前,先...

    Python 查找字符在字符串中的位置实例

    如下所示: str_1='wo shi yi zhi da da niu ...nPos=str_1.find(char_1) print(nPos) 结果:5 ========如何查找所有‘i’在字符串中位置呢?=========== #开挂模式 str_1='wo shi yi zhi da da niu ' char_1=str(in

    如何在对话框上使用滚动条控件

    二,在上面增加一个滚动条控件,并为其关联一个控制型的变量m_scroll。 三,在CEx081122Dlg::OnInitDialog的初始化函数中加上如下代码: m_scroll.SetScrollRange(0,100); m_scroll.SetScrollPos(0); 四,利用类...

    具有 NPoS/PoC 共识的 EVM 兼容链

    repo,请运行:git clone --recursive https://github.com/reef-defi/reef-chain安装您可以使用以下命令安装最新的编译器和工具链:make init启动开发节点make run命令将启动一个临时节点,并且在您终止进程后其状态...

Global site tag (gtag.js) - Google Analytics