繁体   English   中英

如何从C ++中的一组对象指针进行打印?

[英]How do I print from a set of object pointers in C++?

我有一类学生,我将它们存储在cpp文件中。 我遇到的问题是打印出实际的Student对象。 我已经尝试过所有我能想到的,而我得到的只是指针的地址或编译错误。 我的学生课堂上有一个方法叫display,它以我想要的格式打印出所有信息。

这是我到目前为止所拥有的。

.cpp文件

#include "Name.h"
#include "Student.h"
#include<iostream>
#include<string>
#include<set>
using namespace std;

int main()
{
    Student s1;
    set<Student *> s;
    while(cin>>s1)
    {
        s.insert(new Student(s1));
    }
    for(set<Student *>::const_iterator it = s.begin(); it != s.end(); ++it)
    {
        &(*it).display(cout);
    }
}

学生

#ifndef STUDENT_H
#define STUDENT_H
#include<string>
#include<iostream>
#include<map>
#include "Name.h"
typedef std::map<std::string, int> GradeMap;

class Student {
public:
    Student(const std::string id="", const Name& name = Name(),const GradeMap & grades = GradeMap()):id_(id),name_(name),grades_(grades){}
    Student(const Student& s):id_(s.id_),name_(s.name_),grades_(s.grades_){}
    virtual ~Student(){}
    friend std::istream& operator>>(std::istream& is, Student& s);

    virtual void display(std::ostream& os) const{
        os << "ID: " << id_ <<std::endl<< "Name: " << name_ << std::endl;

        for(std::map<std::string, int>::const_iterator it = grades_.begin(); it != grades_.end(); ++it)
            os<<it->first<<' '<<it->second<<std::endl;
    }

private:
  std::string id_;
  Name name_;
  GradeMap grades_;

};

inline std::istream& operator>>(std::istream& is, Student& s)
{
    std::string id;
    std::string key;
    int grade;
    int count = 0;
    Name name;

    if(is>>id>>name>>count){
        s.id_ = id;
        s.name_ = name;
    }
    else {
        is.setstate(std::ios_base::failbit);
    }


    for(int i = 0; i < count; i++)
    {
        if(is>>key>>grade)
        {
            s.grades_[key] = grade;
        }
    }
    return is;
}
#endif

名称.h

#ifndef NAME_H
#define NAME_H
#include <string>
#include <iostream>
class Name{
    public:
        explicit Name(const std::string& first = "",const std:: string& last = ""):first_(first),last_(last){}
        friend std::ostream& operator<<(std::ostream&, const Name&);
        friend std::istream& operator>>(std::istream&, Name&);
private:
        std::string first_;
        std::string last_;
};

inline std::ostream& operator<<(std::ostream& os, const Name& n){
    return os << n.first_<< " " << n.last_;
}
inline std::istream& operator>>(std::istream& is, Name& n){
    std::string first,last;
    if(is >> first >> last ){
        n.first_ = first;
        n.last_ = last;
    }
    else
        is.setstate(std::ios_base::failbit);
    return is;
}
#endif

这也是我用来测试的文件

111111111
john smith
3
comp2510 25
eng2525 60
bio3512 45
222222222
jane doe
2
elex1510 90
comp2510 85

该文件的组织方式如下。 首先是学生的id,然后是他们的名字,然后是他们所修课程的数量,然后是那么多的课程,加上他们所获得的分数。

我的问题是如何打印出实际的学生对象?

for循环中:

for(set<Student *>::const_iterator it = s.begin(); it != s.end(); ++it)
{
    &(*it).display(cout);
}

(*it)Student*

(*it)->display(cout);

迭代器的行为类似于指向实际set元素的指针,因此取消引用迭代器可以为您提供set元素。 该元素本身就是指向Student对象的指针。 因此,您需要取消引用两次:

(**it).display(cout);

最好写成:

(*it)->display(cout);

更好的是,以类似于>>方式重载<<运算符,您可以编写:

cout << (**it);

(括号仅是为了清楚起见。)


更好的是,根本不用理会指针,并将s声明为set<Student> 当然,还会有更多的复制,但是您将停止泄漏内存,并得到更干净,更简单,更安全,更可维护的代码。 (不过,您需要<重载来比较Student 。)

#include <string>
#include <iostream>

class Student
{
    std::string m_name;

public:
    Student(const std::string& name) :
    m_name(name)
    {
    }

    std::ostream& display(std::ostream& os) const
    {
        return os << m_name;
    }

    bool operator<(const Student& other) const
    {
        return m_name < other.m_name;
    }
};

Student定义operator<<

std::ostream& operator<<(std::ostream& os, const Student& student)
{
    return student.display(os);
}

使用它:

#include <set>
#include <algorithm>
#include <iterator>

int main()
{
    std::set<Student> students;

    students.insert(Student("Alan"));
    students.insert(Student("Rudy"));

    std::copy(students.begin(), students.end(),
              std::ostream_iterator<Student>(std::cout, ", "));

    return 0;
}

输出:

Alan, Rudy, 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM