永发信息网

C++ string类中find_first_not_of() 函数 死循环 求解

答案:3  悬赏:70  手机版
解决时间 2021-01-30 13:19
  • 提问者网友:嗝是迷路的屁
  • 2021-01-30 01:41
期望的到target中所有的字母及其在target中的序号...必须用find_first_not_of实现

#include <iostream>
#include <string>

using namespace std;

int main()
{

string numbers("0123456789");
string target("ab2c3d7R4E6");

string::size_type pos = 0;

cout << "find alphabet at index: " << endl;
while((pos = target.find_first_not_of(numbers,pos) != string::npos))
{
cout << pos << " element is " << target[pos] << endl;
++pos;
}

system("pause");

return 0;

}
最佳答案
  • 五星知识达人网友:逃夭
  • 2021-01-30 02:59
你的while中的括号写错位置了,修改如下:
while((pos = target.find_first_not_of(numbers,pos) != string::npos))
改成:
while ((pos = target.find_first_not_of(numbers, pos)) != string::npos)

全文如下:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string numbers("0123456789");
string target("ab2c3d7R4E6");
string::size_type pos = 0;
cout << "find alphabet at index: " << endl;
while ((pos = target.find_first_not_of(numbers, pos)) != string::npos)
{
cout << pos << " element is " << target[pos] << endl;
++pos;
}
system("pause");
return 0;
}
全部回答
  • 1楼网友:第四晚心情
  • 2021-01-30 05:27
#include <iostream> #include <string> using namespace std; int main() { string numbers("0123456789"); string target("ab2c3d7R4E6"); string::size_type pos = 0; cout << "find alphabet at index: " << endl; while((pos = target.find_first_not_of(numbers,pos)) != string::npos)//小括号括错了 { cout << pos << " element is " << target[pos] << endl; pos++; } system("pause"); return 0; }
  • 2楼网友:青灯有味
  • 2021-01-30 04:30
_count
从以_off开始查找第_count次出现的不属于_ptr中的字符索引数。

例如:
str = "444-555-ggg"
str.find_first_not_of ( "45g", 0 );返回第一个"-"的索引值3。
str.find_first_not_of ( "45g", 0, 2 );返回第二个"-"的索引值7。因为从第0个字符开始,第2次不是‘45g’中的字符,为第二个"-"的索引值7。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯