C++ 使用getword函数c++;
我在这里使用的getword函数是否错误?编译器一直告诉我没有成员函数C++ 使用getword函数c++;,c++,string,C++,String,我在这里使用的getword函数是否错误?编译器一直告诉我没有成员函数 #include <iostream> #include <stdlib.h> #include <string> #include <fstream> using namespace std; int OccuranceOfString(ofstream & Out) { string Occur; string Temp; int OccurLeng
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
using namespace std;
int OccuranceOfString(ofstream & Out)
{
string Occur;
string Temp;
int OccurLength;
int count;
cout << "please enter to string to search for";
cout << endl;
cin >> Occur;
OccurLength = Occur.length();
while(Out.getword(Temp))
{
if (Temp == Occur)
{
count ++;
}
}
return count;
}
#包括
#包括
#包括
#包括
使用名称空间std;
内部发生的事件字符串(流和流)
{
字符串出现;
字符串温度;
整数占用长度;
整数计数;
不可能发生;
发生长度=发生长度();
while(Out.getword(Temp))
{
如果(温度==发生)
{
计数++;
}
}
返回计数;
}
我的代码怎么了?我正在尝试使用此函数查找字符串的所有发生情况
std::ofstream
没有getword
函数:请参阅
也许你正在考虑。你可以用这个
做这样的事
int pos = 0;
int occurrences = 0
string input = "YAaaaAH";
string find = "a";
while(pos != -1){
pos = input.find(find,pos);
occurrences++;
}
在列出的头文件中没有函数
getword
。您只需构造一个函数,从一行中提取单词。截线
getline(out,line);
第行将有一行字符串,并使用第[index]行使连续字符等于一个单词 使用Regex可以很容易做到这一点代码有什么问题
getword
不是std::ofstream
中的函数。它根本就不存在。而且你只要做Out>>Temp
就足够了。所以我可以用Out>>Temp替换Out.getword(Temp)?你为什么要从流中获取输入?流的中的“o”表示“输出”。您需要ifstream
。也没有std::getline
函数可用于std::ofstream
std::ifstream
,是。比较应该是std::string::npos
,而不是-1
。不能保证两者相等。@Yuushi这个常数是用一个值-1定义的,因为size\u t是一个无符号整数类型,所以它是这个类型的最大可能的可表示值。
正确吗?有趣的是,我只是查了一下,它实际上是标准强制要求的。我想在我断言之前我需要更仔细地阅读。不过,我仍然认为使用npo
更清晰、更惯用(这意味着在标准发生变化的情况下(不太可能)代码不会中断)。