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),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
C++實(shí)現(xiàn)將輸入的內(nèi)容輸出到文本文件
這篇文章主要介紹了C++實(shí)現(xiàn)將輸入的內(nèi)容輸出到文本文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08QT 實(shí)現(xiàn)隨機(jī)驗(yàn)證碼功能
本文介紹了如何使用QT技術(shù)實(shí)現(xiàn)一個具有動態(tài)效果的隨機(jī)驗(yàn)證碼系統(tǒng),詳述了CaptchaMovableLabel和CaptchaLabel兩個自定義類的功能,包括顯示和拖動字母、繪制噪音點(diǎn)和線條、以及隨機(jī)生成字母等,講解了如何通過繼承QWidget和QLabel來實(shí)現(xiàn)這些功能,并通過MainWindow創(chuàng)建界面2024-10-10