C++中string::npos的用法总结

一.关于npos的定义

在cplusplus.com中有如下解释:

static const size_t npos = -1;
Maximum value for size_t
npos is a static member constant value with the greatest possible value for an element of type size_t.
This value, when used as the value for a len (or sublen) parameter in string’s member functions, means “until the end of the string”.
As a return value, it is usually used to indicate no matches.
This constant is defined with a value of -1, which because size_t is an unsigned integral type, it is the largest possible representable value for this type.

以上的意思是npos是一个常数,表示size_t的最大值(Maximum value for size_t)。许多容器都提供这个东西,用来表示不存在的位置,类型一般是std::container_type::size_type。

下面我们测试一下npos的值:

#include <limits>  
#include <string>  
using namespace std;  
  
int main()  
{  
    size_t npos = -1;  
    cout << "npos: " << npos << endl;  
    cout << "size_t max: " << numeric_limits<size_t>::max() << endl;
}

执行结果为:

npos = 4294967295
size_t max=4294967295

可见他们是相等的,也就是说npos表示size_t的最大值

二.nops的常见用法

1、npos可以表示string的结束位子,是string::type_size 类型的,也就是find()返回的类型。find函数在找不到指定值得情况下会返回string::npos。

举例如下(计算字符串中含有的不同字符的个数):

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string str;
    getline(cin,str);
    int count=0;
    for(int i=0;i<=127;i++)
        if(b.find(i)!=string::npos)
        count++;
    cout<<count;
}

举例2:

#include<iostream>
#include<string>
using namespace std;
int main()
{
string name("Annaqijiashe");
int pos=name.find("Anna");
if(pos==string::npos)     
cout<<"Anna not found!\n";
else  cout<<"Anna found at pos:"<<pos<<endl;

2.string::npos作为string的成员函数的一个长度参数时,表示“直到字符串结束(until the end of the string)”。例如:
tmpname.replace(idx+1, string::npos, suffix);
这里的string::npos就是一个长度参数,表示直到字符串的结束,配合idx+1表示,string的剩余部分。

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string b;
    getline(cin,b);
    int count=0;
    for(int i=0;i<=127;i++)
        if(b.find(i)!=string::npos)
        count++;
    cout<<count;
}

执行结果如下:
filename:test.cpp
replace: test.xxx

三.注意点

int idx = str.find("abc");
if (idx == string::npos)

上述代码中,idx的类型被定义为int,这是错误的,即使定义为 unsigned int 也是错的,它必须定义为 string::size_type。因为 string::size_type (由字符串配置器 allocator 定义) 描述的是 size,故需为无符号整数型别。因为缺省配置器以型别 size_t 作为 size_type,于是 -1 被转换为无符号整数型别,npos 也就成了该型别的最大无符号值。不过实际数值还是取决于型别 size_type 的实际定义。不幸的是这些最大值都不相同。事实上,(unsigned long)-1 和 (unsigned short)-1 不同(前提是两者型别大小不同)。因此,比较式 idx == string::npos 中,如果 idx 的值为-1,由于 idx 和字符串string::npos 型别不同,比较结果可能得到 false。
要想判断 find() 的结果是否为npos,最好的办法是直接比较:
if (str.find(“abc”) == string::npos) { … }

  • 26
    点赞
  • 108
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
C++,`string::npos`是`string`类的一个静态成员常量,它表示`size_t`的最大值。具体来说,`npos`被用作字符串查找操作(比如`find`和`find_last_of`)的返回值,用于表示没有找到匹配的情况。在比较`find`的结果是否等于`npos`时,可以直接使用`string::npos`进行比较。需要注意的是,为了确保正确的类型匹配,定义存储`find`结果的变量时应该使用`string::size_type`而不是`int`或`unsigned int`。证明`-1`表示`size_t`的最大值可以通过输出`size_t`类型的最大值和`string::npos`的值进行验证。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [详解C++ string常用截取字符串方法](https://download.csdn.net/download/weixin_38502428/14874161)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [C++string::npos](https://blog.csdn.net/m0_51913750/article/details/128352256)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值