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è)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C++實(shí)現(xiàn)將輸入的內(nèi)容輸出到文本文件
這篇文章主要介紹了C++實(shí)現(xiàn)將輸入的內(nèi)容輸出到文本文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
C語(yǔ)言如何利用異或進(jìn)行兩個(gè)值的交換詳解
最近在工作中遇到了兩個(gè)值交換的需求,發(fā)現(xiàn)自己對(duì)異或有些忘記,所以索性寫(xiě)出來(lái),方便以后需要的時(shí)候參考學(xué)習(xí),下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言如何利用異或進(jìn)行兩個(gè)值的交換的相關(guān)資料,需要的朋友可以參考下。2017-09-09
QT 實(shí)現(xiàn)隨機(jī)驗(yàn)證碼功能
本文介紹了如何使用QT技術(shù)實(shí)現(xiàn)一個(gè)具有動(dòng)態(tài)效果的隨機(jī)驗(yàn)證碼系統(tǒng),詳述了CaptchaMovableLabel和CaptchaLabel兩個(gè)自定義類的功能,包括顯示和拖動(dòng)字母、繪制噪音點(diǎn)和線條、以及隨機(jī)生成字母等,講解了如何通過(guò)繼承QWidget和QLabel來(lái)實(shí)現(xiàn)這些功能,并通過(guò)MainWindow創(chuàng)建界面2024-10-10

