匹配txt文件并计算单词C ++的数量?

时间:2014-07-22 13:17:52

标签: c++

您好我是C ++新手并且很难使用此代码,请帮助您排序,以确保您易于理解,因为我不知道如何解决这些错误。 当我运行我的代码时,它会抛出3个错误

on line 116:error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::_Vector_iterator<_Myvec>' (or there is no acceptable conversion) 
1> with 
1> [ 
1> _Myvec=std::_Vector_val<std::_Simple_types<Word>> 
1> ] 
On line 17: could be 'Word &Word::operator =(const Word &)' 
1> while trying to match the argument list '(Word, std::_Vector_iterator<_Myvec>)' 
1> with 
1> [ 
1> _Myvec=std::_Vector_val<std::_Simple_types<Word>> 
1> ] 
On line 120: error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Word' (or there is no acceptable conversion) 
1> c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector(390): could be 'std::_Vector_iterator<_Myvec> &std::_Vector_iterator<_Myvec>::operator =(const std::_Vector_iterator<_Myvec> &)' 
1> with 
1> [ 
1> _Myvec=std::_Vector_val<std::_Simple_types<Word>> 
1> ] 

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

以下代码:

 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; 
  } 

1 个答案:

答案 0 :(得分:0)

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

oneWord = *itBanned;

*itBanned = oneWord;