C++中std::string::npos的用法
C++中std::string::npos
(1)它是一個常量靜態(tài)成員值,對于 size_t 類型的元素具有最高可能值。
(2)它實(shí)際上意味著直到字符串的末尾。
(3)它用作字符串成員函數(shù)中長度參數(shù)的值。
(4)作為返回值,它通常用于表示沒有匹配項(xiàng)。
(5)數(shù)據(jù)類型為size_t的話string:npos常量被定義為-1,因?yàn)閟ize_t是無符號整數(shù)類型,-1是該類型的最大可能表示值。
使用示例
作為沒有匹配項(xiàng)的示例
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "I am cver";
size_t index = str.find('.');
if(index == string::npos)
{
cout << "This does not contain any period!" << endl;
cout << index << endl;
}
}輸出
This does not contain any period!
18446744073709551615
作字符串成員函數(shù)中長度參數(shù)的值
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str = "I am cver.";
size_t index = str.find('.');
if(index == string::npos)
{
cout << "This does not contain any period!" << endl;
cout << index << endl;
}
else
{
str.replace(index, string::npos, "!");
cout << str << endl;
cout << index << endl;
}
}輸出:
I am cver!
9
string::npos的一些說明
定義
std::string::npos的定義:
static const size_t npos = -1;
表示size_t的最大值(Maximum value for size_t),如果對 -1表示size_t的最大值有疑問可以采用如下代碼驗(yàn)證:
#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上執(zhí)行結(jié)果為:
npos: 4294967295
size_t max: 4294967295
可見他們是相等的,也就是說npos表示size_t的最大值
使用
1.如果作為一個返回值(return value)表示沒有找到匹配項(xiàng)
例如:
#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,表示沒有匹配項(xiàng)
? ? if(idx == string::npos) ? ?
? ? {
? ? ? ? cout << "filename does not contain any period!" << endl;
? ? }
}2.但是string::npos作為string的成員函數(shù)的一個長度參數(shù)時
表示“直到字符串結(jié)束(until the end of the string)”
例如:
tmpname.replace(idx+1, string::npos, suffix);
這里的string::npos就是一個長度參數(shù),表示直到字符串的結(jié)束,配合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作為長度參數(shù),表示直到字符串結(jié)束
? ? ? ? cout << "repalce: " << tmpname << endl;
? ? }
}執(zhí)行結(jié)果為:
filename:test.cpp
replace: test.xxx
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- 利用C++實(shí)現(xiàn)從std::string類型到bool型的轉(zhuǎn)換
- C/C++中關(guān)于std::string的compare陷阱示例詳解
- C++ float轉(zhuǎn)std::string 小數(shù)位數(shù)控制問題
- C++17 使用 std::string_view避免字符串拷貝優(yōu)化程序性能
- C++17中std::string_view的使用
- C++面試八股文之std::string實(shí)現(xiàn)方法
- C++中std::stringstream多類型數(shù)據(jù)拼接和提取用法小結(jié)
- c++使用 std::string 存儲二進(jìn)制數(shù)據(jù)
相關(guān)文章
詳解C++中虛析構(gòu)函數(shù)的作用及其原理分析
這篇文章主要介紹了C++中虛析構(gòu)函數(shù)的作用及其原理分析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
如何寫好C main函數(shù)的幾個注意事項(xiàng)
這篇文章主要介紹了如何寫好C main函數(shù)的幾個注意事項(xiàng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
C語言實(shí)現(xiàn)返回字符串函數(shù)的四種方法
在C語言中實(shí)現(xiàn)函數(shù)返回字符串,首先要確定函數(shù)返回的字符串地址的來源,一般分為四種方式,下面這篇文章就給大家通過示例代碼詳細(xì)介紹這幾種方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12
C++實(shí)現(xiàn)LeetCode(12.整數(shù)轉(zhuǎn)化成羅馬數(shù)字)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(12.整數(shù)轉(zhuǎn)化成羅馬數(shù)字),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語言深入探究sizeof與整型數(shù)據(jù)存儲及數(shù)據(jù)類型取值范圍
在main函數(shù)中,sizeof是可以正常工作的,但是在自定義函數(shù)中就不可以了。所以本文將為大家詳細(xì)講解一下關(guān)鍵字sizeof、整型數(shù)據(jù)存儲深入、數(shù)據(jù)類型取值范圍深入2022-07-07

