标题: | 1、Stl::list模版使用 | |
时 限: | 1000 ms | |
内存限制: | 10000 K | |
总时限: | 3000 ms | |
描述: |
| |
输入: | 学生个数(比如为N) N条学生记录(格式为:1 张平一 902418 19,这是其中1表示按顺序插入,张平一是学生姓名,902418是整数学号,19是年龄) 插入学生的个数(比如为M) M条插入学生的记录(格式为:17 张平一 902418 19,其中17表示插入到整个记录表的第17位(从0开始,数组下标为16,若17>数组长度,输出Insert Error),张平一是学生姓名,902418是整数学号,19是年龄) K条删除学生的记录 K个记录的数组位置(从0开始,如果没有这个位置,输出Delete Error)
| |
输出: |
| |
输入样例: | 5 1 张平一 902418 19 1 刘浩 902433 21 1 刘磊 902411 21 1 白平一 902425 20 1 张充直 902429 21 3 1 孙小雪 902429 20 3 张潮 902415 21 13 赵洗星 902435 20 3 18 5 6 | |
输出样例: | Insert Error Delete Error Delete Error Name 张平一 Num 902418 Age 19 Name 孙小雪 Num 902429 Age 20 Name 刘浩 Num 902433 Age 21 Name 张潮 Num 902415 Age 21 Name 刘磊 Num 902411 Age 21 Name 张充直 Num 902429 Age 21 Reverse Name 张充直 Num 902429 Age 21 Name 刘磊 Num 902411 Age 21 Name 张潮 Num 902415 Age 21 Name 刘浩 Num 902433 Age 21 Name 孙小雪 Num 902429 Age 20 Name 张平一 Num 902418 Age 19 DelSame Name 张充直 Num 902429 Age 21 Name 刘磊 Num 902411 Age 21 Name 张潮 Num 902415 Age 21 Name 刘浩 Num 902433 Age 21 Name 张平一 Num 902418 Age 19 Sort Name 刘磊 Num 902411 Age 21 Name 张潮 Num 902415 Age 21 Name 张平一 Num 902418 Age 19 Name 张充直 Num 902429 Age 21 | |
提示: | 请注意,插入和删除时输入的位置从零开始 |
#include <string>
#include <iostream>
using namespace std;
class StudentRecord
{
private:
string name;
int num;
int age;
public:
StudentRecord(string _name = "", int _num = 0, int _age = 1)
{
name = _name;
num = _num;
age = _age;
}
string GetName()
{
return name;
}
int GetNum()
{
return num;
}
int GetAge()
{
return age;
}
friend ostream& operator<<(ostream &out, const StudentRecord &stu)
{
out << "Name " << stu.name << " Num " << stu.num << " Age " << stu.age;
return out;
}
virtual ~StudentRecord() {}
};
list<StudentRecord>::iterator operator +(list<StudentRecord>::iterator const it,int move_length)
{
list<StudentRecord>::iterator that=it;
for(int i=0; i<move_length; i++)
{
that++;
}
//if(that==it) cout<<"指针相同"<<endl;
//else cout<<"指针不同"<<endl;
return that;
}
bool compare(StudentRecord a,StudentRecord b)
{
if(a.GetNum()<=b.GetNum()) return true;
else return false;
}
int main()
{
list<StudentRecord> stu_list;
int n;
cin >> n;
int no_relative, num, age;
string name;
for (int i = 0; i < n; ++i)
{
cin >> no_relative;
cin >> name >> num >> age;
stu_list.push_back(StudentRecord(name, num, age));
}
cin >> n;
int locate;
for (int i = 0; i < n; ++i)
{
cin >> locate;
cin >> name >> num >> age;
if (locate > (signed)stu_list.size()-1)
{
cout << "Insert Error" << endl;
}
else
{
list<StudentRecord>::iterator it = stu_list.begin();
it=it+locate;
stu_list.insert(it, StudentRecord(name, num, age));
}
}
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> locate;
//cout<<locate<<endl;
//cout<<stu_list.size();
if (locate>(signed) stu_list.size()-1||locate < 0 )
{
cout << "Delete Error" << endl;
}
else
{
list<StudentRecord>::iterator it = stu_list.begin();
it=it+locate;
cout<<"Delete right"<<endl;
cout<<*it<<endl;
stu_list.erase(it);
}
}
for (list<StudentRecord>::iterator it = stu_list.begin(); it != stu_list.end(); it++)
{
cout << *it << endl;
}
stu_list.reverse();
cout << "Reverse"<< endl;;
for (list<StudentRecord>::iterator it = stu_list.begin(); it != stu_list.end(); it++)
{
cout << *it << endl;
}
for (list<StudentRecord>::iterator it_locate = stu_list.begin(); it_locate != stu_list.end(); it_locate++)
{
list<StudentRecord>::iterator it = it_locate+1;
for (; it != stu_list.end(); it++)
{
if (it->GetNum() == it_locate->GetNum())
{
it = stu_list.erase(it);
it--;
}
}
}
cout << "DelSame" << endl;
for (list<StudentRecord>::iterator it = stu_list.begin(); it != stu_list.end(); it++)
{
cout << *it << endl;
}
stu_list.sort(compare);
cout << "Sort" << endl;
for (list<StudentRecord>::iterator it = stu_list.begin(); it != stu_list.end(); it++)
{
cout << *it << endl;
}
return 0;
}