如何在C++中从一组对象指针打印

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

本文关键字:一组 对象 打印 指针 C++      更新时间:2023-10-16

我有一类学生,我将其存储到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);
    }
}

学生.h

#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);

迭代器的行为就像指向实际集合元素的指针,因此取消引用迭代器会得到集合元素。该元素本身就是指向Student对象的指针。所以你需要取消引用两次:

(**it).display(cout);

哪个写得更好:

(*it)->display(cout);

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

cout << (**it);

(括号仅为清楚起见。)


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

#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,