如何在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);
}
}
学生.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,
相关文章:
- C++ 将一组对象传递给抽象类的成员函数
- 在一组模板化对象上运行函数
- 是否可以为CPP中的对象创建一组指针
- 在C 中创建一组对象
- 获取5个最后一个数组对象C
- 在C++中对一组对象使用lower_bound()
- C++包含一组对象的类
- 将一个子对象数组传递给接受Parent*的函数
- 创建一组对象
- 在C++中将一个对象添加到另一组对象中
- 构建一组对象并将它们放入元组中
- 如何在C++中从一组对象指针打印
- 为什么 count 不返回一组C++对象的正确值?
- 打印出一组对象
- 如何使用静态库(.a文件)而不是一组对象文件(.o)来创建共享库
- 管理一组对象,以便被保存的对象可以访问保存它们的数据结构
- c++确保对象被分配给一组对象
- 如何重载一组对象的运算符
- 初始化仅由一组对象共享的静态变量
- 循环问题:从它自己的成员函数中访问一组对象