永发信息网

c++中find_first_not_of函数的使用

答案:2  悬赏:30  手机版
解决时间 2021-03-08 11:04
  • 提问者网友:听门外雪花风
  • 2021-03-07 11:40
已知有如下string对象"ab2c3d7R4E6"编写程序寻找该字符串中所有的数字字符,然后寻找所有的字母字符,第一个用find_first_of函数,第二个用find_first_not_of函数。请高手赐教!
最佳答案
  • 五星知识达人网友:何以畏孤独
  • 2021-03-07 13:17
#include 
#include
using namespace std;

//使用find_first_of
int main()
{
string s = "ab2c3d7R4E6";
string num = ("0123456789");
string letter = ("abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ");

string::size_type pos = 0;

while((pos = s.find_first_of(num, pos)) != string::npos)
{
cout<<"found number at index: "< ++pos;
}

pos = 0;

while((pos = s.find_first_of(letter, pos)) != string::npos)
{
cout<<"found letter at index: "< ++pos;
}
return 0;
}
全部回答
  • 1楼网友:不如潦草
  • 2021-03-07 13:28
find_first_of()函数: 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::npos, 查找在字符串中第一个与ch匹配的字符,返回它的位置。搜索从index开始。 函数find_first_not_of()功能如下: 1.返回在字符串中首次出现的不匹配str任何字符的首字符索引, 从index开始搜索, 如果全部匹配则返回string::npos。 2.从index开始起搜索当前字符串, 查找其中与str前num个字符中的任意一个都不匹配的序列, 返回满足条件的第一个字符索引, 否则返回string::npos。 3.返回在当前字符串中第一个不匹配ch字符的索引, 从index开始搜索, 没用收获则返回string::npos。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯