1.封装一个学生的类,定义一个学生这样类的vector容器, 里面存放学生对象
再把该容器中的对象,保存到文件中。(至少3个)
再把这些学生从文件中读取出来,放入另一个容器中并且遍历输出该容器里的学生。
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class Student {
private:
string name;
int age;
public:
//无参构造函数
Student(){}
//有参构造函数
Student(const string &name, int age):name(name), age(age) {}
string getName() const
{
return name;
}
int getAge() const
{
return age;
}
};
int main()
{
vector<Student> students;
students.push_back(Student("cqq", 23));
students.push_back(Student("xjx", 22));
students.push_back(Student("xjj", 20));
// 保存学生对象到文件
ofstream ofs;
ofs.open("C:/QT/stu.txt",ios::out);
if (ofs.is_open()) {
for (const auto& student : students) {
ofs << student.getName() << " " << student.getAge() << endl;
}
ofs.close();
} else {
cout << "无法打开文件进行写入" << endl;
return 1;
}
vector<Student> loadedStudents;
ifstream inFile("C:/QT/stu.txt");
if (inFile.is_open()) {
string name;
int age;
while (inFile >> name >> age) {
loadedStudents.push_back(Student(name, age));
}
inFile.close();
} else {
cout << "无法打开文件进行读取" << endl;
return 1;
}
// 遍历输出读取到的学生对象
for (const auto& student : loadedStudents) {
cout << "姓名: " << student.getName() << " 年龄: " << student.getAge() << endl;
}
return 0;
}
2.实现与list相关的函数
#include <iostream>
#include <list>
using namespace std;
template <typename T>
class MyList : public list<T>
{
public:
MyList() : list<T>() {}
MyList(typename list<T>::iterator beg, typename list<T>::iterator end) : list<T>(beg, end) {}
MyList(int n, const T& elem) : list<T>(n, elem) {}
MyList(const MyList<T>& l) : list<T>(l) {}
MyList<T>& operator=(const MyList<T>& lst)
{
list<T>::operator=(lst);
return *this;
}
void assign(typename list<T>::iterator beg, typename list<T>::iterator end)
{
list<T>::assign(beg, end);
}
void assign(int n, const T& elem)
{
list<T>::assign(n, elem);
}
void swap(MyList<T>& lst)
{
list<T>::swap(lst);
}
int size() const
{
return list<T>::size();
}
bool empty() const
{
return list<T>::empty();
}
void resize(int num)
{
list<T>::resize(num);
}
void resize(int num, const T& elem)
{
list<T>::resize(num, elem);
}
void push_front(const T& elem)
{
list<T>::push_front(elem);
}
void pop_front()
{
list<T>::pop_front();
}
typename list<T>::iterator insert(typename list<T>::const_iterator pos, const T& elem)
{
return list<T>::insert(pos, elem);
}
void insert(typename list<T>::const_iterator pos, int count, const T& elem)
{
list<T>::insert(pos, count, elem);
}
void insert(typename list<T>::const_iterator pos, typename list<T>::iterator beg, typename list<T>::iterator end)
{
list<T>::insert(pos, beg, end);
}
typename list<T>::iterator erase(typename list<T>::const_iterator pos)
{
return list<T>::erase(pos);
}
typename list<T>::iterator erase(typename list<T>::const_iterator start, typename list<T>::const_iterator end)
{
return list<T>::erase(start, end);
}
void clear()
{
list<T>::clear();
}
void remove(const T& elem)
{
list<T>::remove(elem);
}
T& front()
{
return list<T>::front();
}
const T& front() const
{
return list<T>::front();
}
T& back()
{
return list<T>::back();
}
const T& back() const
{
return list<T>::back();
}
};
//定义全局函数当做策略函数
bool comfunc(int a, int b) {
return a < b;
}
//定义仿函数当做函数策略
class compare {
public:
compare() {}
//重载小括号运算符
bool operator()(int a, int b) {
return a > b;
}
};
int main() {
MyList<int> l; //创建一个链表
cout << "size of l = " << l.size() << endl; //0
//调用头插函数
l.push_front(1);
l.push_front(2);
l.push_front(3);
//调用尾插函数
l.push_back(4);
l.push_back(5);
l.push_back(6);
for (int val : l) {
cout << val << " ";
}
cout << endl;
//调用判空函数
if (l.empty()) {
cout << "yes" << endl;
} else {
cout << "no" << endl;
}
//更改第一个元素
l.front() = 987;
l.back() = 654;
for (int val : l) {
cout << val << " ";
}
cout << endl;
//调用排序函数
//l.sort(comfunc); //使用了全局函数当做策略的排序函数
//l.sort(compare()); //使用仿函数当做函数策略
//使用lambda表达式当做函数策略
l.sort([](int a, int b) -> bool { return a > b; });
for (int val : l) {
cout << val << " ";
}
cout << endl;
//调用翻转函数
// l.reverse();
// for(int val:l)
// {
// cout<<val<<" ";
// }
// cout<<endl;
return 0;
}