您的位置:首页 > 编程语言 > C语言/C++

字符串的查找删除---C++中string.find()函数与string::npos

2017-01-29 11:35 351 查看
给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串

输入:

输入只有一组数据

输入一个短字符串(不含空格),再输入若干字符串直到文件结束为止

输出:

删除输入的短字符串(不区分大小写)并去掉空格

#include <stdio.h>
#include <string>
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
char str[101];
gets(str); //输入短字符串
string a = str;
for(int i = 0; i<a.size(); ++i)
{ //将a中字符串全部改为小写字母
a[i] = tolower(a[i]);
}
while(gets(str))
{
string b = str,c = b; //将字符串保存至b,c,b将用于保存小写化后的字符,c保存原字符串
for(int i = 0; i<b.size(); ++i)
{ //将b中的字符全部改为小写,便于匹配
b[i] = tolower(b[i]);
}
int t = b.find(a,0); //在b串中查找a的位置,返回索引
while(t != string::npos) //查找成功,则重复循环
{
c.erase(t,a.size());
b.erase(t,a.size());
t = b.find(a,t); //从t位置为起点继续查找b中下一个出现字符串a的位置
}
t = c.find('',0);
while(t != string::npos)
{ //删除c中所有空格
c.erase(t,1);
t = c.find(' ',0);
}
cout<<c<<endl;
}
return 0;
}


可见,在使用了string对象后,关于字符串处理的问题将得到大大的简化,注意,由于要求中“大小写不区分”,所以我们先将字符串全部改为小写后进行匹配,tolower函数,于

C标准函数库中的头文件 ctype.h中

注意:

查找字符串a是否包含子串b,不是用strA.find(strB) > 0 而是 strA.find(strB) != string:npos

其中string:npos是个特殊值,说明查找没有匹配

string::size_type pos = strA.find(strB);
if(pos != string::npos){}
-------------------------------------------
int idx = str.find("abc");
if (idx == string::npos)
...
上述代码中,idx的类型被定义为int,这是错误的,即使定义为 unsigned int 也是错的,它必须定义为 string::size_type。
npos 是这样定义的:
static const size_type npos = -1;

因为 string::size_type (由字符串配置器 allocator 定义) 描述的是 size,故需为无符号整数型别。因为缺省配置器以型别 size_t 作为 size_type,于是 -1 被转换为无符号整数型别,npos 也就成了该型别的最大无符号值。不过实际数值还是取决于型别 size_type 的实际定义。不幸的是这些最大值都不相同。事实上,(unsigned long)-1 和 (unsigned short)-1 不同(前提是两者型别大小不同)。因此,比较式 idx == string::npos 中,如果 idx 的值为-1,由于 idx 和字符串string::npos 型别不同,比较结果可能得到 false。
要想判断 find() 的结果是否为npos,最好的办法是直接比较:

if (str.find("abc") == string::npos) { ... }

错误:if(str.find("abc") )
注:找不到abc会返回-1,不为0为True。0为False

通常来说,find函数用于寻找某个序列的在string中第一次出现的位置。

find函数有以下四种重载版本:

size_t find (const string& str, size_t pos = 0) const noexcept;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_type n) const;
size_t find (char c, size_t pos = 0) const noexcept;


第一个参数总是被搜寻的对象。
第二个参数指出string内的搜索起点(索引)。
第三个参数指出搜寻的字符个数。


////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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐