如何打印出对象矢量元素

时间:2016-12-25 19:11:41

标签: c++ visual-c++

我想创建一个测验银行游戏。我有一个.txt文件存储信息并将其推回到向量,但我的主要问题是如何使用此指针打印我的get方法到对象的向量。

这是我的全部代码。

#include <iostream>
#include <string>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

class question {
private :
    string ques ;
    string answer;
    int point ;
public :
    question(string ques = "",string answer = "",int point = 0):ques(ques),answer(answer),point(point){}

  virtual string getAnswer () = 0;
  virtual string getQuestion() = 0;
  virtual int getPoint() = 0;
};

class SAquestion : public question {
private :
    string ques ;
    string answer;
    int point ;
public :
 SAquestion(string ques = "",string answer = "",int point = 0):ques(ques),answer(answer),point(point){}
  string getAnswer () {return answer;}
  string getQuestion(){return ques ;}
  int getPoint() {return point ;}
};

class MCquestion : public question {
private :
    string ques ;
    string answer;
    int point ;
public :
 MCquestion(string ques = "",string answer = "",int point = 0):ques(ques),answer(answer),point(point){}
  string getAnswer () {return answer;}
  string getQuestion(){return ques ;}
  int getPoint() {return point ;}
};

class TFquestion : public question {
private :
    string ques ;
    string answer;
    int point ;
public :
 TFquestion(string ques = "",string answer = "",int point = 0):ques(ques),answer(answer),point(point){}
  string getAnswer () {return answer;}
  string getQuestion(){return ques ;}
  int getPoint() {return point ;}
};

void readDataByDelimiter(const char* filename, vector< SAquestion>*SHORTQ) {
    string line;
    ifstream ifs(filename);
    if (ifs.is_open()) {
        cout << "Reading data...\n";
        int c = 0;
        while ( getline (ifs,line) && (*line.c_str() != '\0') )  {
            string delimiter = ",";
            size_t pos = 0;
            string* token = new string[5];
            int f = -1;
            while ((pos = line.find(delimiter)) != string::npos) {
                token[++f] = line.substr(0, pos);
                cout << " " << token[f] << " | " ;
                line.erase(0, pos + delimiter.length());
            }
            token[++f] = line;
            cout << token[f] << endl;       // last item in string
            c++;

            // push to vector (numerical data converted to int)
            SAquestion b(token[1], token[2], atoi(token[3].c_str()));
            SHORTQ->push_back(b);
        }
        cout << c << " row(s) read." << endl << endl;
        ifs.close();
    }
    else
        cout << "Unable to open file";
}

    enter code here

int main()
{
    vector<SAquestion> *s = new vector<SAquestion>();
    readDataByDelimiter("SHORQ.txt", s);
cout <<s[0]->getAnswer();

}

 - List item

1 个答案:

答案 0 :(得分:1)

以下是打印vector的代码:

void Print_Vector(const std::vector<SAquestion>& v)
{
  std::vector<SAquestion>::const_iter  iter;
  const std::vector<SAquestion>::const_iter end_iter = v.end();
  for (iter = v.begin(); iter != end_iter; ++iter)
  {
    cout << *iter << "\n";
  }
}

上面的代码使用迭代器来访问vector中的每个元素。由于打印不会更改vector的内容,因此vector通过常量引用传递,常量使用迭代器。

注意:此功能要求重载operator <<以打印SAquestion项目。

注意:通过使用按引用传递,不需要使用指针。

编辑1:重载operator<<
要打印对象,请更改透视图并让对象打印其成员。一种方便的方法是重载operator>>

class SAquestion
{
 //...
  public:
    friend std::ostream& operator<<(std::ostream& output, const SAquestion& saq);
};
std::ostream& operator<<(std::ostream& output, const SAquestion& saq)
{
  output << "Q: " << saq.ques << "\n";
  output << "A: " << saq.answer << "\n";
  output << "\n";
  return output;
}

您无需使用getAnswer()方法,因为friend关键字允许operator<<功能直接访问SAquestion成员。