把这些垃圾从我的向量中删除

发布于 2024-07-12 05:53:12 字数 3779 浏览 3 评论 0原文

我想消除 vector上的垃圾。 下面的 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;
  }

I want to eliminate the junk that I'm getting on the vector<int> synsAux below. It should print:

1 
7
7
1

I'm getting an extra 2 before the first and third digit, why? Is this 2 an ascii value for the blank space or something? How do I avoid its reading?

This is the dictionary file needed to run the program:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2024-07-19 05:53:12

你在这里打印两个:

cout<<synsAux.size();

You're printing the twos here:

cout<<synsAux.size();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文