Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.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++ 匹配txt文件并计算字数C++;? 与 1> [ 1> _Myvec=std:_Vector_val 1> ] 第17行:可以是“Word&Word::operator=(const Word&)” 1> 尝试匹配参数列表时“(Word,std::_Vector_iterator)” 1> 与 1> [ 1> _Myvec=std:_Vector_val 1> ] 第120行:错误C2679:二进制“=”:未找到接受“Word”类型的右操作数的运算符(或没有可接受的转换) 1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\vector(390):可以是“std::\u vector\u iterator&std::\u vector\u iterator::operator=(常量std::\u vector\u iterator&)” 1> 与 1> [ 1> _Myvec=std:_Vector_val 1> ]_C++ - Fatal编程技术网

C++ 匹配txt文件并计算字数C++;? 与 1> [ 1> _Myvec=std:_Vector_val 1> ] 第17行:可以是“Word&Word::operator=(const Word&)” 1> 尝试匹配参数列表时“(Word,std::_Vector_iterator)” 1> 与 1> [ 1> _Myvec=std:_Vector_val 1> ] 第120行:错误C2679:二进制“=”:未找到接受“Word”类型的右操作数的运算符(或没有可接受的转换) 1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\vector(390):可以是“std::\u vector\u iterator&std::\u vector\u iterator::operator=(常量std::\u vector\u iterator&)” 1> 与 1> [ 1> _Myvec=std:_Vector_val 1> ]

C++ 匹配txt文件并计算字数C++;? 与 1> [ 1> _Myvec=std:_Vector_val 1> ] 第17行:可以是“Word&Word::operator=(const Word&)” 1> 尝试匹配参数列表时“(Word,std::_Vector_iterator)” 1> 与 1> [ 1> _Myvec=std:_Vector_val 1> ] 第120行:错误C2679:二进制“=”:未找到接受“Word”类型的右操作数的运算符(或没有可接受的转换) 1> c:\ProgramFiles(x86)\microsoft visual studio 11.0\vc\include\vector(390):可以是“std::\u vector\u iterator&std::\u vector\u iterator::operator=(常量std::\u vector\u iterator&)” 1> 与 1> [ 1> _Myvec=std:_Vector_val 1> ],c++,C++,请告诉我如何更正代码。代码分为几个部分 下面的代码: class Word { public: string name; int hits; Word() { } }; Update : /***** Prototypes *****/ void ReadFileToVector(vector<string> &v, string strFileName); void ReadFileToVector(vector<Word

请告诉我如何更正代码。代码分为几个部分

下面的代码:

 class Word 
 { 
 public: 
 string name; 
 int hits; 
 Word() 
 { } 
 }; 

 Update : /***** Prototypes *****/ 

 void ReadFileToVector(vector<string> &v, string strFileName); 
 void ReadFileToVector(vector<Word> &v, string strFileName); 
 void PrintString(string strIn); 
 void CompareToBanned(vector<string> &banned, vector<string> &textFile); 
 vector<Word> CompareToBanned(vector<Word> &banned, vector<string> &textFile); 
 vector<Word> ConvertToWords(vector<Word> arrWords, string strOneWord); 
 int main() 
 { 
 // Read banned words into a string array 
 string strBanned = "banned.txt"; 

 Update 2: vector<string> arrBanned; 
 vector<Word> arrBannedWords; 
 ReadFileToVector(arrBanned, strBanned); 
 //for_each(arrBanned.begin(), arrBanned.end(), PrintString); 

 string strTextOne = "text1.txt"; 
 vector<string> arrTextOne; 
 ReadFileToVector(arrTextOne, strTextOne); 

 vector<string>::iterator it; 
 for(it = arrBanned.begin(); it != arrBanned.end(); it++) 
 { 
  arrBannedWords = ConvertToWords(arrBannedWords, *it); 
 } 
 CompareToBanned(arrBannedWords, arrTextOne); 

 system("pause"); 
 } 

 void ReadFileToVector(vector<string> &v, string strFileName) 
 { 
  ifstream objFileIn; // Create stream object 
  string strOneWord; 
  objFileIn.open(strFileName); // Open a file and put it into the stream created 

  while(!objFileIn.eof()) 
  { 
   objFileIn >> strOneWord; 
   v.push_back(strOneWord); // For every word in the file push it back to the vector 
  } 
  } 

  void ReadFileToVector(vector<Word> &v, string strFileName) 
  { 
    ifstream objFileIn; // Create stream object 
    string strOneWord; 

    objFileIn.open(strFileName); // Open a file and put it into the stream created 
    Word oneWord; 

    while(!objFileIn.eof()) 
  { 
   objFileIn >> strOneWord; 
   oneWord.name = strOneWord; 
   v.push_back(oneWord); // For every word in the file push it back to the vector 
  } 
 } 

 void PrintString(string strIn) 
 { 
  cout << strIn << endl; 
 } 

 void CompareToBanned(vector<string> &banned, vector<string> &textFile) // Take the two files to compare 
 { 
  // For each word in the new text file we need to compare it with every word in the banned list 
  vector<string>::iterator itText; 
  vector<string>::iterator itBanned; 
  int hits = 0; 

  for(itText = textFile.begin(); itText != textFile.end(); itText++) 
  { 
   for(itBanned = banned.begin(); itBanned != banned.end(); itBanned++) 
  { 
   if(*itText == *itBanned) 
  { 
   string foundWord = *itText; 
   hits++; 
   cout << "I have found " << *itBanned << endl; 
  } 
  } 
  } 
  cout << "There were a total of " << hits << " hits in the file." << endl; 

  } 

  vector<Word> CompareToBanned(vector<Word> &banned, vector<string> &textFile) // Take the two files to compare 
  { 
   // For each word in the new text file we need to compare it with every word in the banned list 
   vector<string>::iterator itText; 
   vector<Word>::iterator itBanned; 
   Word oneWord; 
   for(itText = textFile.begin(); itText != textFile.end(); itText++) 
   { 
   for(itBanned = banned.begin(); itBanned != banned.end(); itBanned++) 
   { 
     oneWord = itBanned; // Unable to set a word element of a vector to equal a word 
     if(*itText == oneWord.name ) 
   { 
     oneWord.hits++; 
     itBanned = oneWord; 
     cout << "I have found " << oneWord.name << endl; 
   } 
   } 
   } 
   cout << "There were a total of " << oneWord.hits << " hits in the file." << endl; 
   return banned; 
   } 

   vector<Word> ConvertToWords(vector<Word> arrWords, string strOneWord) 
   { 
   Word oneWord; 
   oneWord.name = strOneWord; 
   arrWords.push_back(oneWord); 
   return arrWords; 
  } 
类字
{ 
公众:
字符串名;
整数命中率;
单词()
{ } 
}; 
更新:/****原型******/
void ReadFileToVector(vector&v,字符串strFileName);
void ReadFileToVector(vector&v,字符串strFileName);
无效打印字符串(字符串strIn);
无效比较禁止(矢量和禁止、矢量和文本文件);
向量比较禁止(向量和禁止、向量和文本文件);
向量转换词(向量arrWords、字符串strOneWord);
int main()
{ 
//将禁止的单词读入字符串数组
字符串strBanned=“banked.txt”;
更新2:禁止使用矢量编码;
矢量字;
ReadFileToVector(禁用、禁用);
//对于每个(arrbanked.begin()、arrbanked.end()、PrintString);
字符串strTextOne=“text1.txt”;
向量arrTextOne;
ReadFileToVector(arrTextOne、strTextOne);
向量::迭代器;
for(it=arrbanked.begin();it!=arrbanked.end();it++)
{ 
arrbandowords=转换词(arrbandowords,*it);
} 
比较禁止(arrbandwords、arrTextOne);
系统(“暂停”);
} 
void ReadFileToVector(vector&v,字符串strFileName)
{ 
ifstream objFileIn;//创建流对象
字符串strOneWord;
objFileIn.open(strFileName);//打开一个文件并将其放入创建的流中
而(!objFileIn.eof())
{ 
objFileIn>>strOneWord;
v、 push_back(strOneWord);//对于文件中的每个单词,将其推回向量
} 
} 
void ReadFileToVector(vector&v,字符串strFileName)
{ 
ifstream objFileIn;//创建流对象
字符串strOneWord;
objFileIn.open(strFileName);//打开一个文件并将其放入创建的流中
一个字一个字;
而(!objFileIn.eof())
{ 
objFileIn>>strOneWord;
oneWord.name=strOneWord;
v、 push_back(oneWord);//对于文件中的每个单词,将其推回向量
} 
} 
无效打印字符串(字符串strIn)
{ 

cout您试图将
vector::iterator
直接分配给
Word
对象,反之亦然。您需要使用*运算符从迭代器中取消引用
Word

oneWord = *itBanned;


缩进不会增加可执行文件的大小,但是是的,它会节省大量调试时间。谢谢你在visual studio中的建议,但是当我复制到这里时,它丢失了所有的格式!谢谢你,似乎已经完成了勾选!:)如果你认为有更好的方法做事情,请提出建议!不能谢谢你gh!还有一个问题。代码是用来计算在我的情况下找到(点击)9的单词,但它实际上显示-858993457代码来自第90行。我做错了什么?非常感谢
*itBanned = oneWord;