C++中std::string::npos的用法
C++中std::string::npos
(1)它是一個(gè)常量靜態(tài)成員值,對(duì)于 size_t 類型的元素具有最高可能值。
(2)它實(shí)際上意味著直到字符串的末尾。
(3)它用作字符串成員函數(shù)中長(zhǎng)度參數(shù)的值。
(4)作為返回值,它通常用于表示沒(méi)有匹配項(xiàng)。
(5)數(shù)據(jù)類型為size_t的話string:npos常量被定義為-1,因?yàn)閟ize_t是無(wú)符號(hào)整數(shù)類型,-1是該類型的最大可能表示值。
使用示例
作為沒(méi)有匹配項(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ù)中長(zhǎng)度參數(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的一些說(shuō)明
定義
std::string::npos的定義:
static const size_t npos = -1;
表示size_t的最大值(Maximum value for size_t),如果對(duì) -1表示size_t的最大值有疑問(wèn)可以采用如下代碼驗(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
可見(jiàn)他們是相等的,也就是說(shuō)npos表示size_t的最大值
使用
1.如果作為一個(gè)返回值(return value)表示沒(méi)有找到匹配項(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,表示沒(méi)有匹配項(xiàng)
? ? if(idx == string::npos) ? ?
? ? {
? ? ? ? cout << "filename does not contain any period!" << endl;
? ? }
}2.但是string::npos作為string的成員函數(shù)的一個(gè)長(zhǎng)度參數(shù)時(shí)
表示“直到字符串結(jié)束(until the end of the string)”
例如:
tmpname.replace(idx+1, string::npos, suffix);
這里的string::npos就是一個(gè)長(zhǎng)度參數(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作為長(zhǎng)度參數(shù),表示直到字符串結(jié)束
? ? ? ? cout << "repalce: " << tmpname << endl;
? ? }
}執(zhí)行結(jié)果為:
filename:test.cpp
replace: test.xxx
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 利用C++實(shí)現(xiàn)從std::string類型到bool型的轉(zhuǎn)換
- C/C++中關(guān)于std::string的compare陷阱示例詳解
- C++ float轉(zhuǎn)std::string 小數(shù)位數(shù)控制問(wèn)題
- 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 存儲(chǔ)二進(jìn)制數(shù)據(jù)
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)運(yùn)動(dòng)會(huì)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)運(yùn)動(dòng)會(huì)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
VC實(shí)現(xiàn)動(dòng)態(tài)菜單的創(chuàng)建方法
這篇文章主要介紹了VC實(shí)現(xiàn)動(dòng)態(tài)菜單的創(chuàng)建方法,需要的朋友可以參考下2014-07-07
C++類中隱藏的幾個(gè)默認(rèn)函數(shù)你知道嗎
這篇文章主要為大家詳細(xì)介紹了C++類中隱藏的幾個(gè)默認(rèn)函數(shù),使用數(shù)據(jù)庫(kù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03
C語(yǔ)言實(shí)現(xiàn)的猴子分桃問(wèn)題算法解決方案
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)的猴子分桃問(wèn)題算法,較為詳細(xì)的分析了猴子分桃問(wèn)題算法的原理與通過(guò)遞歸算法解決問(wèn)題的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10
C語(yǔ)言循環(huán)隊(duì)列與用隊(duì)列實(shí)現(xiàn)棧問(wèn)題解析
循環(huán)隊(duì)列又叫環(huán)形隊(duì)列,是一種特殊的隊(duì)列。循環(huán)隊(duì)列解決了隊(duì)列出隊(duì)時(shí)需要將所有數(shù)據(jù)前移一位的問(wèn)題,本篇帶你一起看看循環(huán)隊(duì)列的問(wèn)題和怎樣用隊(duì)列實(shí)現(xiàn)棧2022-04-04
深入c++中臨時(shí)對(duì)象的析構(gòu)時(shí)機(jī)的詳解
本篇文章對(duì)c++中臨時(shí)對(duì)象的析構(gòu)時(shí)機(jī)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

