声明一个 Student 结构体或者类,数据成员是学号(int),姓名(string)和成绩
(float),然后定义一个 list 链表类对象,追加 10 个 Student 对象到 list 对象
里面,然后将自己的数据(学号姓名和成绩)插入到其中的第 6 个位置。用
迭代器遍历整个 list 对象,输出全部数据项的值。
#include<iostream>
#include<list>
using namespace std;
struct Student
{
int number;
string name;
float score;
};
int main()
{
int i;
list<Student> List;
Student stu[10]={{0,"aa",98.1},{1,"bb",96.3},{2,"cc",95.2},{3,"dd",97.3},{4,"ee",94.2},{5,"ff",95.9},{6,"gg",91.9},{7,"hh",96.5},{8,"ii",91.9},{9,"jj",91.5}};
for(i=0;i<10;i++)
List.push_back(stu[i]);
list<Student>::iterator iter=List.begin();
for(i=0;i<5;i++)
iter ++;
List.insert(iter,{11,"jz",99.9});
for(iter=List.begin();iter!=List.end();iter++)
cout<<(*iter).number<<","<<(*iter).name<<","<<(*iter).score<<endl;
return 0;
}