国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

深入C++ string.find()函數的用法總結

2024-08-31 00:56:14
字體:
來源:轉載
供稿:網友
#include <string>
#include <iostream>
using namespace std;
void main()
{

復制代碼 代碼如下:


////find函數返回類型 size_type
string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");
string flag;
string::size_type position;
//find 函數 返回jk 在s 中的下標位置
position = s.find("jk");
if (position != s.npos) //如果沒找到,返回一個特別的標志c++中用npos表示,我這里npos取值是4294967295,
{
cout << "position is : " << position << endl;
}
else
{
cout << "Not found the flag" + flag;
}


復制代碼 代碼如下:


//find 函數 返回flag 中任意字符 在s 中第一次出現的下標位置
flag = "c";
position = s.find_first_of(flag);
cout << "s.find_first_of(flag) is : " << position << endl;


復制代碼 代碼如下:


//從字符串s 下標5開始,查找字符串b ,返回b 在s 中的下標
position=s.find("b",5);
cout<<"s.find(b,5) is : "<<position<<endl;


復制代碼 代碼如下:


//查找s 中flag 出現的所有位置。
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 中最后出現的位置
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) 這兩個函數,如果完全匹配,才返回匹配的索引,即:當s中含有abc三個連續的字母時,才返回當前索引。
s.find_first_of(sub), s.find_first_not_of(sub), s.find_last_of(sub), s.find_last_not_of(sub) 這四個函數,查找s中含有sub中任意字母的索引。
2.如果沒有查詢到,則返回string::npos,這是一個很大的數,其值不需要知道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌兰县| 昆明市| 南涧| 南丹县| 庆安县| 桑日县| 栾川县| 嫩江县| 红河县| 博客| 南汇区| 西华县| 南丹县| 霍林郭勒市| 濉溪县| 额尔古纳市| 仙桃市| 南充市| 潜山县| 昭觉县| 东至县| 柘城县| 陇西县| 萍乡市| 广南县| 行唐县| 凌海市| 淮阳县| 长乐市| 故城县| 濉溪县| 福海县| 西宁市| 扶绥县| 维西| 资溪县| 烟台市| 六安市| 六安市| 万荣县| 新蔡县|