首页 > 编程语言 >【c&c++】std::string::npos的使用

【c&c++】std::string::npos的使用

时间:2023-04-23 15:56:03浏览次数:40  
标签:std string max npos type find size

std::string::npos
std::string::npos是一个常数,它等于size_type类型可以表示的最大值,用来表示一个不存在的位置,类型一般是std::container_type::size_type。

定义
static const size_type npos = -1;

#include <iostream>

int main(int argc, char *argv[]) {
    size_t a = -1;
    std::cout << "a : " << a << std::endl;
    std::cout << "npos : " << std::string::npos << std::endl;
    std::cout << "size_type max : " << std::numeric_limits<std::string::size_type>::max() << std::endl;
    std::cout << "size_t max : " << std::numeric_limits<size_t>::max() << std::endl;
    std::cout << "uint64_t max : " << std::numeric_limits<uint64_t>::max() << std::endl;
    return 0;
}

执行结果

a : 18446744073709551615
npos : 18446744073709551615
size_type max : 18446744073709551615
size_t max : 18446744073709551615
uint64_t max : 18446744073709551615

使用

std::string的查找函数

  1. find
  2. rfind
  3. find_first_of
  4. find_first_not_of
  5. find_last_of
  6. find_last_not_of
    这些操作全都返回 string::size_type 类型的值。

example

#include <iostream>

int main(int argc, char *argv[]) {
    std::string const s = "This is a string";
    
    std::string::size_type n1 = s.find("is");
    if (std::string::npos == n1) {
        std::cout << "not found is" << std::endl;
    } else {
        std::cout << "index : " << n1 << std::endl;
    }
    
    std::string::size_type n2 = s.find("oo");
    if (std::string::npos == n2) {
        std::cout << "not found oo" << std::endl;
    } else {
        std::cout << "index : " << n2 << std::endl;
    }
    
    return 0;
}

执行结果

index : 2
not found oo

 


标签:std,string,max,npos,type,find,size
From: https://www.cnblogs.com/opensmarty/p/17346767.html

相关文章

  • js 中 对 String 的操作
    //charAt():返回指定位置的字符。conststr="hello";constchar=str.charAt(1);//"e"//charCodeAt():返回指定位置字符的Unicode编码。conststr="hello";constunicode=str.charCodeAt(1);//101//concat():连接两个或多个字符串,并返回新的字符串。constst......
  • time&datetime&string相互转换
    time&datetime&string相互转换importdatetimeimporttime#日期时间字符串st="2017-11-2316:10:10"#当前日期时间dt=datetime.datetime.now()#当前时间戳sp=time.time()#1.把datetime转成字符串defdatetime_toString(dt):print("1.把datetime转成......
  • 【面试题】对 JSON.stringify()与JSON.parse() 理解
    大厂面试题分享面试题库前后端面试题库(面试必备)推荐:★★★★★地址:前端面试题库  web前端面试题库VSjava后端面试题库大全重新学习这两个API的起因在本周五有线上的项目,16:30开始验证线上环境。开始都是顺顺利利,一帆风顺。大概17:50左右,我正在收拾东西。准备下班去王者峡......
  • String对象
    一、 数据类型转换数据类型值类型(基本类型):字符串(String)、数字(Number)、布尔(Boolean[ˈbuliən])、对空(Null)、未定义(Undefined)、Symbol [ˈsɪmbəl]。引用数据类型:对象(Object)、数组(Array)、函数(Function)。typeof和constructor类型转换方法:Number()转换为数......
  • ZSTD多线程压缩
    测试代码:1//main.cpp2//34#include<iostream>5#include<fstream>6#include<vector>7#include<chrono>8#include<thread>9#include"./zstd/lib/zstd.h"1011usingbyte=uint8_t;12usingbuff......
  • string的使用方法和高级用法
    字符串的定义和初始化#include<string>usingnamespacestd;stringstr1;//定义一个空的字符串stringstr2="helloworld";//使用字符串字面量进行初始化stringstr3("helloworld");//使用构造函数进行初始化stringstr4(str2);//使用拷贝构造函数进行初始化字符串......
  • Java中的String的intren方法详解
    intern方法会从字符串常量池中查询当前字符串是否存在,若不存在就会将当前字符串放入常量池Stringa=newString("hello").intern();Stringb="hello";System.out.println(a==b);//Stringa=newString("hello");a.intern();Stringb="hello";System.out.println(a==b)......
  • std::initialize_list<T>作为返回值
    正常情况,我们应该返回一个vector<T>,但是呢,因为我这是嵌入式,我不想动态开辟。于是我突发其想,返回个intialize_list,在写这个代码时,我就觉得可能不正确,因为intialize_list类中没有个数。抱着试一试的心态,我还是写了这个代码。结果,果然是不正确的。原因是这样的,intialize_list实际是......
  • SpringDataRedis的序列化方式和StringRedisTemplate手动序列化详解
    一.SpringDataRedis之前新创建一个Spring项目,在进行配置完成redis和common-pool依赖:1.引入依赖redis:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis-reactive</artifactId></dependency>......
  • Json使用toString
    Json的使用:1.对象转Json {"":"",“”:""}2.数组转Json [{"":"",“”:""},{"":"",“”:""}]3.JSon转对象4.Json转数组fastjson是ali的,比较使用于SpringC、SpringB/***转json给前端**@par......