string::npos的一些说明

string::npos的一些说明

一、定义

std::string::npos的定义:

static const size_t npos = -1;

表示size_t的最大值(Maximum
value for size_t),如果对 -1表示size_t的最大值有疑问可以采用如下代码验证:

#include <iostream>
#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;
}

在我的PC上执行结果为:

                 npos:           4294967295

                 size_t max:  4294967295

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

二、使用

2.1 如果作为一个返回值(return value)表示没有找到匹配项,例如:

#include <iostream>
#include <limits>
#include <string>
using namespace std;

int main()
{
    string filename = "test";
    cout << "filename : " << filename << endl;

    size_t idx = filename.find(‘.‘);   //作为return value,表示没有匹配项
    if(idx == string::npos)
    {
        cout << "filename does not contain any period!" << endl;
    }
}

2.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 <limits>
#include <string>
using namespace std;

int main()
{
    string filename = "test.cpp";
    cout << "filename : " << filename << endl;

    size_t idx = filename.find(‘.‘);   //as a return value
    if(idx == string::npos)
    {
        cout << "filename does not contain any period!" << endl;
    }
    else
    {
        string tmpname = filename;
        tmpname.replace(idx + 1, string::npos, "xxx"); //string::npos作为长度参数,表示直到字符串结束
        cout << "repalce: " << tmpname << endl;
    }
}

执行结果为:

filename:test.cpp

replace: test.xxx

string::npos的一些说明,布布扣,bubuko.com

时间: 2024-08-05 18:01:46

string::npos的一些说明的相关文章

字符串的查找删除---C++中string.find()函数与string::npos

给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串 输入: 输入只有一组数据 输入一个短字符串(不含空格),再输入若干字符串直到文件结束为止 输出: 删除输入的短字符串(不区分大小写)并去掉空格 #include <stdio.h> #include <string> #include <iostream> #include <ctype.h> using namespace std; int main() { char st

std::string::find() 和 std::string::npos

npos是一个常数,用来表示不存在的位置,string::npos代表字符串到头了结束了. int idx = str.find("abc");if (idx == string::npos)  ... 上述代码中,idx的类型被定义为int,这是错误的,即使定义为 unsigned int 也是错的,它必须定义为 string::size_type. npos 是这样定义的:static const size_type npos = -1; 因为 string::size_type

C++中string.find()函数,string.find_first_of函数与string::npos

查找字符串a是否包含子串b,不是用strA.find(strB) > 0而是strA.find(strB) != string:nposstring::size_type pos = strA.find(strB);if(pos != string::npos){}-------------------------------------------int idx = str.find("abc");if (idx == string::npos)...上述代码中,idx的类型被

string::npos 速成 及其在自定义split函数中的应用

string::npos的定义: static const size_t npos = -1; 表示size_t的最大值(Maximum value for size_t) C++中并没有拆分字符串函数,但是在刷题时经常遇到要拆分字符串的情况, 故编写一个自定义的split函数. r:egmkang void SplitString(const std::string& s, std::vector<std::string>& v, const std::string&

C++ string的查找函数和npos特殊值

STL中的string有6个查找函数: 1.find() 2.rfind() 从最后一个字符开始往前找. 3.find_first_of() 4.find_not_first_of() 5.find_last_of() 6.find_not_last_of() 所有这些查找函数返回值都是size_type类型(找到了)或者是一个名为 string::npos的特殊值(没找到). string::npos常用来表示没找到的结果或者string类型的末尾. #include <iostream>

split 实现(c++ string)

#include <iostream> #include <vector> size_t split(std::string &src, std::vector<std::string> *tokens, std::string sep) { size_t last= 0; size_t index = src.find(sep, last); size_t length = src.size(); while(index != std::string::npo

Leetcode: Reverse Words in a String

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". Clarification: What constitutes a word? A sequence of non-space characters constitutes a word. Could the input

C++中字符数组和字符串string

字符数组 C++中字符数组用char str[]可以用来表示一个字符串. (1)   数组的大小和字符串的长度. 数组的大小一定要大于字符串的长度,因为系统会自动补上一个'\0'作为字符串的结束标志.当然对于未初始化的也补'\0'. #include <iostream> #include <string> using namespace std; int main() { char str[11] = "I am happy"; // 系统会自动补上'\0'空

16年蓝桥杯第七题_(string)

手链样式 小明有3颗红珊瑚,4颗白珊瑚,5颗黄玛瑙.他想用它们串成一圈作为手链,送给女朋友.现在小明想知道:如果考虑手链可以随意转动或翻转,一共可以有多少不同的组合样式呢? 请你提交该整数.不要填写任何多余的内容或说明性的文字. 一开始以为是搜索,发现不行,想了一会儿没有思路,看了题解...用了一个比较慢,但是想法简单的方法. 主要用了一些stl的函数, #include<iostream> #include<cstdio> #include<cstring> #inc