链表问题

[复制链接]
1335|4
 楼主| txcy 发表于 2013-7-28 12:30 | 显示全部楼层 |阅读模式
要求写一个结构体链表,比如一个list<student> LISTST
struct student
{
  int id;
  int number;
  int score;
  char sex;
  string address;
}stu;
怎么用C++写出一个个输出里面的元素啊,我不明白该把list做对象还是把struct做对象。
无冕之王 发表于 2013-7-28 12:45 | 显示全部楼层
重载 << 操作符
函数声明如下:
ostream& operator << (ostream& os, const student &object)
{
os << stu.id << endl;
os << stu.number << endl;
……  //把stu的成员都输出

return os;
}

然后
cout << *(LISTST.begin());
hsbjb 发表于 2013-7-28 13:02 | 显示全部楼层
你可以这样做:
在struct头文件中在结构体重增加一个operator<<()操作,再在同一文件中增加一个operator<<()调用类中的operator<<();
秋天落叶 发表于 2013-7-28 13:11 | 显示全部楼层
struct student
{
  int id;
  int number;
  int score;
  char sex;
  string address;
  struct student *next;
}stu;
gxgclg 发表于 2013-7-28 19:13 | 显示全部楼层
2楼的方法应该可行
您需要登录后才可以回帖 登录 | 注册

本版积分规则

274

主题

2106

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部