Mar*_*iun 1 c++ string function visual-studio-2015
所以,我必须介绍一些名称(并不重要),并计算其中有多少我可以找到子字符串'ana'.我不允许使用char作为名称,我需要使用字符串.我编写的代码就像我想的那样简单,但我的strstr函数不起作用.
#include <iostream>
#include <string>
#include<stdio.h>
using namespace std;
void main()
{
string name;
string ana;
int ok = 0;
int nr = 0;
ana = "ana";
cout << "if you want to stop entering names introduce 1 after the last name, else introduce 0.";
while (ok == 0)
{
cout << "Name here: " << flush;
getline(cin, name);
if (strstr(name, ana) != 0)
nr++;
cin >> ok;
}
cout << "The number of names that contain 'ana' is: " << nr;
}
Run Code Online (Sandbox Code Playgroud)
你能帮我个忙吗?错误是:"严重程序代码说明项目文件行抑制状态错误C2665'strstr':2个重载中没有一个可以转换所有参数类型T1E1 e:\ uni\an ii\lf\t1e1\t1e1\e1.cpp 20"
strstr
适用于C字符串,而不是std::string
.
代替
if (strstr(name, ana) != 0)
nr++;
Run Code Online (Sandbox Code Playgroud)
使用
if ( name.find(ana) != std::string::npos )
nr++;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
341 次 |
最近记录: |