[英]strstr function does not work vs 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;
}
你能給我些幫助嗎? 錯誤為:“嚴重性代碼說明項目文件行抑制狀態錯誤C2665'strstr':2個重載均不能轉換所有參數類型T1E1 e:\\ uni \\ an ii \\ lf \\ t1e1 \\ t1e1 \\ e1.cpp 20”
strstr
使用C字符串,而不是std::string
。
使用std::string::find
代替。
代替
if (strstr(name, ana) != 0)
nr++;
采用
if ( name.find(ana) != std::string::npos )
nr++;
否則,您可以包括頭文件cstring
,其中包含strstr
函數。 在您的.cpp
文件中添加以下代碼。
#include <cstring>
有關更多詳細信息,請訪問cppreference.com 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.