Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用getword函数c++;_C++_String - Fatal编程技术网

C++ 使用getword函数c++;

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

我在这里使用的getword函数是否错误?编译器一直告诉我没有成员函数

#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
更清晰、更惯用(这意味着在标准发生变化的情况下(不太可能)代码不会中断)。