深入C++ string.find()函數(shù)的用法總結(jié)
更新時間:2013年05月29日 16:23:13 作者:
本篇文章是對C++中string.find()函數(shù)的用法進(jìn)行了詳細(xì)的總結(jié)與分析,需要的朋友參考下
#include <string>
#include <iostream>
using namespace std;
void main()
{
////find函數(shù)返回類型 size_type
string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;
//find 函數(shù) 返回jk 在s 中的下標(biāo)位置
position = s.find("jk");
if (position != s.npos) //如果沒找到,返回一個特別的標(biāo)志c++中用npos表示,我這里npos取值是4294967295,
{
cout << "position is : " << position << endl;
}
else
{
cout << "Not found the flag" + flag;
}
//find 函數(shù) 返回flag 中任意字符 在s 中第一次出現(xiàn)的下標(biāo)位置
flag = "c";
position = s.find_first_of(flag);
cout << "s.find_first_of(flag) is : " << position << endl;
//從字符串s 下標(biāo)5開始,查找字符串b ,返回b 在s 中的下標(biāo)
position=s.find("b",5);
cout<<"s.find(b,5) is : "<<position<<endl;
//查找s 中flag 出現(xiàn)的所有位置。
flag="a";
position=0;
int i=1;
while((position=s.find_first_of(flag,position))!=string::npos)
{
//position=s.find_first_of(flag,position);
cout<<"position "<<i<<" : "<<position<<endl;
position++;
i++;
}
//查找flag 中與s 第一個不匹配的位置
flag="acb12389efgxyz789";
position=flag.find_first_not_of (s);
cout<<"flag.find_first_not_of (s) :"<<position<<endl;
//反向查找,flag 在s 中最后出現(xiàn)的位置
flag="3";
position=s.rfind (flag);
cout<<"s.rfind (flag) :"<<position<<endl;
}
說明:
1.如果string sub = ”abc“;
string s = ”cdeabcigld“;
s.find(sub) , s.rfind(sub) 這兩個函數(shù),如果完全匹配,才返回匹配的索引,即:當(dāng)s中含有abc三個連續(xù)的字母時,才返回當(dāng)前索引。
s.find_first_of(sub), s.find_first_not_of(sub), s.find_last_of(sub), s.find_last_not_of(sub) 這四個函數(shù),查找s中含有sub中任意字母的索引。
2.如果沒有查詢到,則返回string::npos,這是一個很大的數(shù),其值不需要知道。
#include <iostream>
using namespace std;
void main()
{
復(fù)制代碼 代碼如下:
////find函數(shù)返回類型 size_type
string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;
//find 函數(shù) 返回jk 在s 中的下標(biāo)位置
position = s.find("jk");
if (position != s.npos) //如果沒找到,返回一個特別的標(biāo)志c++中用npos表示,我這里npos取值是4294967295,
{
cout << "position is : " << position << endl;
}
else
{
cout << "Not found the flag" + flag;
}
復(fù)制代碼 代碼如下:
//find 函數(shù) 返回flag 中任意字符 在s 中第一次出現(xiàn)的下標(biāo)位置
flag = "c";
position = s.find_first_of(flag);
cout << "s.find_first_of(flag) is : " << position << endl;
復(fù)制代碼 代碼如下:
//從字符串s 下標(biāo)5開始,查找字符串b ,返回b 在s 中的下標(biāo)
position=s.find("b",5);
cout<<"s.find(b,5) is : "<<position<<endl;
復(fù)制代碼 代碼如下:
//查找s 中flag 出現(xiàn)的所有位置。
flag="a";
position=0;
int i=1;
while((position=s.find_first_of(flag,position))!=string::npos)
{
//position=s.find_first_of(flag,position);
cout<<"position "<<i<<" : "<<position<<endl;
position++;
i++;
}
復(fù)制代碼 代碼如下:
//查找flag 中與s 第一個不匹配的位置
flag="acb12389efgxyz789";
position=flag.find_first_not_of (s);
cout<<"flag.find_first_not_of (s) :"<<position<<endl;
復(fù)制代碼 代碼如下:
//反向查找,flag 在s 中最后出現(xiàn)的位置
flag="3";
position=s.rfind (flag);
cout<<"s.rfind (flag) :"<<position<<endl;
}
說明:
1.如果string sub = ”abc“;
string s = ”cdeabcigld“;
s.find(sub) , s.rfind(sub) 這兩個函數(shù),如果完全匹配,才返回匹配的索引,即:當(dāng)s中含有abc三個連續(xù)的字母時,才返回當(dāng)前索引。
s.find_first_of(sub), s.find_first_not_of(sub), s.find_last_of(sub), s.find_last_not_of(sub) 這四個函數(shù),查找s中含有sub中任意字母的索引。
2.如果沒有查詢到,則返回string::npos,這是一個很大的數(shù),其值不需要知道。
相關(guān)文章
sqlserver實現(xiàn)樹形結(jié)構(gòu)遞歸查詢(無限極分類)的方法
下面小編就為大家?guī)硪黄猻qlserver實現(xiàn)樹形結(jié)構(gòu)遞歸查詢(無限極分類)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07SQL有外連接的時候注意過濾條件位置否則會導(dǎo)致網(wǎng)頁慢
這個SQL之所以跑得慢是因為開發(fā)人員把SQL的條件寫錯位置了 正確的寫法應(yīng)該是下面這樣的,感興趣的朋友可以參考下2013-05-05sqlserver/mysql按天、按小時、按分鐘統(tǒng)計連續(xù)時間段數(shù)據(jù)【推薦】
最近小編接到這樣的需求,公司需要按天,按小時查看數(shù)據(jù),可以直觀的看到時間段的數(shù)據(jù)峰值。這篇文章主要介紹了sqlserver/mysql按天,按小時,按分鐘統(tǒng)計連續(xù)時間段數(shù)據(jù) ,需要的朋友可以參考下2019-06-06萬能密碼的SQL注入漏洞其PHP環(huán)境搭建及防御手段
這篇文章主要介紹了萬能密碼的SQL注入漏洞其PHP環(huán)境搭建及防御手段,對此感興趣的小伙伴趕快收藏起來吧2021-09-09SQL?Server?Agent?服務(wù)啟動后又停止問題
這篇文章主要介紹了SQL?Server?Agent?服務(wù)啟動后又停止問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04必須會的SQL語句(八) 數(shù)據(jù)庫的完整性約束
這篇文章主要介紹了sqlserver中數(shù)據(jù)庫的完整性約束使用方法,需要的朋友可以參考下2015-01-01