문제

나는 내가 가고있는 쓰레기를 제거하고 싶다. vector<int> synsAux 아래에. 인쇄해야합니다 :

1 
7
7
1

첫 번째와 세 번째 숫자 전에 추가 2를 받고 있습니다. 왜? 이 2는 빈 공간에 대한 ASCII 값입니까? 독서를 피하려면 어떻게해야합니까?

이것은 프로그램을 실행하는 데 필요한 사전 파일입니다.

dictionary.txt
1 cute
2 hello
3 ugly
4 easy
5 difficult
6 tired
7 beautiful
synonyms
1 7
7 1
antonyms
1 3
3 1 7
4 5
5 4
7 3

#include <iostream>
#include <fstream>
#include <string>

#include <sstream>
#include <vector>


using namespace std;

class WordInfo{
      public:
             WordInfo(){}
             ~WordInfo() {     
             }

             int id() const {return myId;}

             void readWords(istream &in)
             {
               in>>myId>>word;     
             }

             void pushSynonyms (string synline, vector <WordInfo> wordInfoVector)
             {
                 stringstream synstream(synline);
                 vector<int> synsAux;
                 int num;
                 while (synstream >> num) synsAux.push_back(num);
                 cout<<synsAux.size();

                  for (int i=0; i<synsAux.size(); i++){
                      cout<<synsAux[i]<<endl;  
                      // THIS LINE SHOULD BE PRINTING
                      //1
                      //7
                      //7
                      //1
                 }       
             }

             void pushAntonyms (string antline, vector <WordInfo> wordInfoVector)
             {

             }

             //--dictionary output function
             void printWords (ostream &out)
             {
                out<<myId<< " "<<word;     
             }

             //--equals operator for String
             bool operator == (const string &aString)const
             {
                           return word ==aString; 
             }

             //--less than operator
             bool operator <(const WordInfo &otherWordInfo) const
             { return word<otherWordInfo.word;}

             //--more than operator
             bool operator > (const WordInfo &otherWordInfo)const
             {return word>otherWordInfo.word;}

             private:
                   vector <int> mySynonyms;
                   vector <int> myAntonyms;
                   string word;
                   int myId;
      };

      //--Definition of input operator for WordInfo
      istream & operator >>(istream &in, WordInfo &word)
      {
         word.readWords(in); 
      }

      //--Definition of output operator
      ostream & operator <<(ostream &out, WordInfo &word)
      {
            word.printWords(out);  
      }

  int main() {
      string wordFile;
      cout<<"enter name of dictionary file: ";
      getline (cin,wordFile);

      ifstream inStream (wordFile.data());

      if(!inStream.is_open())
      {
          cerr<<"cannot open "<<wordFile<<endl; 
          exit(1);                      
      }

      vector <WordInfo> wordVector; 
      WordInfo aword;

      while (inStream >>aword && (!(aword=="synonyms")))
      {
          wordVector.push_back(aword);
      }

      inStream.clear(); // clears failbit on the ifstream

      int i=0;          
      while (i<wordVector.size()){
            cout<<wordVector[i]<<endl;
            i++;
      }

      vector <int> intVector;
      string synLine; //suspect

      while (getline(inStream, synLine)&&(synLine!=("antonyms"))){
            aword.pushSynonyms(synLine, wordVector);
      }

      system("PAUSE");
      return 0;
  }
도움이 되었습니까?

해결책

여기서는 두 가지를 인쇄하고 있습니다.

cout<<synsAux.size();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top