dede门户网站模版,宁波网络公司联系方式,广州网站推广联盟,wordpress文章头图作业#xff1a;
封装一个学生的类#xff0c;定义一个学生这样类的vector容器, 里面存放学生对象#xff08;至少3个#xff09;
再把该容器中的对象#xff0c;保存到文件中。
再把这些学生从文件中读取出来#xff0c;放入另一个容器中并且遍历输出该容器里的学生。…作业
封装一个学生的类定义一个学生这样类的vector容器, 里面存放学生对象至少3个
再把该容器中的对象保存到文件中。
再把这些学生从文件中读取出来放入另一个容器中并且遍历输出该容器里的学生。
#include iostream
#includevector
#includefstream
using namespace std;class Stu
{friend istream operator(istream cin,Stu R);friend ostream operator(ostream cout,const Stu R);
private:string name;int age;
public:Stu(){};Stu(string name,int age):name(name),age(age){};
};ostream operator(ostream cout,const Stu R)
{cout R.name ;cout R.age endl;return cout;
}istream operator(istream cin,Stu R)
{cin R.name;cin R.age;return cin;
}
int main()
{Stu s1(张三,18);Stu s2(李四,20);Stu s3(王五,19);vectorStu stu;stu.push_back(s1);stu.push_back(s2);stu.push_back(s3);ofstream ofs;ofs.open(D:/2.txt,ios::out);vectorStu::iterator iter;for(iter stu.begin();iter!stu.end();iter){ofs *iter ;}ofs.close();vectorStustu1;Stu s;ifstream ifs;ifs.open(D:/2.txt,ios::in);while(ifss){stu1.push_back(s);}for(iterstu1.begin();iter!stu1.end();iter){cout *iter ;}ifs.close();return 0;
} 1.模板类
#include iostreamusing namespace std;template class T,class N
class A
{
private:T t;N n;
public:A(){};//无参构造A(T t,N n):t(t),n(n){}//有参构造void show(){cout t endl n endl;}
};
int main()
{Astring,int a(张三,18);a.show();return 0;
}
2.异常异常情况为取钱时取的钱小于0或者大于余额
#include iostreamusing namespace std;class BankAccount
{
private:double balance;
public:BankAccount(){};BankAccount(double balance):balance(balance){};void withdraw(double money){if(money0){throw(invalid_argument(取款金额不能为负数));}else if(moneybalance){throw(runtime_error(余额不足));}else{balance - money;cout 余额为: balance endl;}}
};int main()
{BankAccount account1(1000);try {account1.withdraw(-100);} catch (invalid_argument e){cout Erro: e.what() endl;} catch (runtime_error e){cout Erro: e.what() endl;}try {account1.withdraw(1500);} catch (invalid_argument e){cout Erro: e.what() endl;} catch (runtime_error e){cout Erro: e.what() endl;}try {account1.withdraw(500);} catch (invalid_argument e){cout Erro: e.what() endl;} catch (runtime_error e){cout Erro: e.what() endl;}return 0;
}
3.lambda表达式和auto的使用
#include iostreamusing namespace std;int main()
{int a100;double b3.14;char ca;auto fun[a,b,c](){};//捕获外界a,b,c变量的值fun函数中的a,b,c不是外界的a,b,c,地址不同//想要修改fun中的a,b,c的值必须在()后加上mutableauto fun1[](){};//捕获外界所有的变量值auto fun2[a,b](){};//捕获外界a,b变量的地址,fun函数中的a,b是外界的a,b,地址相同,//想要修改fun2中的值可以直接改变auto fun3[](){};//捕获外界所有的变量的地址auto fun4[,a,b](){};//捕获外界所有的值,但是变量a和变量b是引用捕获//fun函数中的a,b是外界的a,b,地址相同,可以直接修改,不用加上mutableauto fun5[,a,b](){};//捕获外界所有变量的地址但变量a,b捕获的是值,修改需要加mutablereturn 0;
}4.容器
#include iostream
#includevector
using namespace std;void printVector(vectorint v)
{vector int::iterator ite;//创建一个vectorint类型的迭代器itefor(itev.begin();ite!v.end();ite){cout *ite ;}cout endl;
}
int main()
{//容器vectorintv;//无参构造容器vv.push_back(10);//尾插v.push_back(20);v.push_back(30);v.push_back(40);//算法printVector(v);vectorintv2(v.begin(),v.end());//拷贝v中begin到end区间中的值printVector(v2);vectorintv3(6,10);//拷贝构造将6个10拷贝给v3printVector(v3);vectorintv4v2;//拷贝构造将v2中的值拷贝给v4printVector(v4);vectorintv5(v3);//拷贝构造将v3中的值拷贝给v5printVector(v5);vectorintv6;v6v5;//拷贝赋值将v5的值拷贝一份给v6v6.assign(v5.begin(),v5.end());//将v5begin到end区间的值拷贝一份赋值给v6v6.assign(8,99);//将8个99拷贝一份赋值给v6if(v6.empty())//判断v6是否为空{cout v6容器为空 endl;}else{cout v6容器的容量为: v6.capacity() endl;cout v6容器的大小(容器中的元素个数)为: v6.size() endl;printVector(v6);v6.resize(15);//大小重新设置为15,数据不够补充0printVector(v6);}return 0;
}5.list链表
#include iostream
#include list
using namespace std;void printList(listchar v)
{list char::iterator ite;//创建一个listchar类型的迭代器itefor(itev.begin();ite!v.end();ite){cout *ite ;}cout endl;
}
int main()
{list char lst;//定义一个链表里面存放char类型元素lst.push_back(h);//存放一个字符alst.push_back(e);lst.push_back(l);lst.push_back(l);lst.push_back(o);printList(lst);//输出lst中的所有元素lst.push_front(a);//在lsit表头部插入字符alst.push_back(!);//在尾部插入元素!printList(lst);//输出lst中的所有元素lst.pop_front();//删除list表头部元素lst.pop_back();//删除list表尾部元素printList(lst);//输出lst中的所有元素list char lst2(lst.begin(),lst.end());//拷贝构造函数将lst内从begin到end的元素拷贝到链表lst2中printList(lst2);//输出lst2中的所有元素list char lst3(3,6);//拷贝构造函数将3个字符6存入链表lst3中printList(lst3);//输出lst3中的所有元素list char lst4(lst3);//拷贝构造函数将lst3中的元素拷贝到lst4中printList(lst4);//输出lst4中的所有元素list char lst5;lst5.assign(lst.begin(),lst.end());//拷贝赋值函数将lst中begin到end区间的值拷贝一份赋值到lst5中printList(lst5);//输出lst5中的所有元素结果和lst结果一致lst5.assign(7,h);//将5个h赋值到lst5中printList(lst5);//输出lst5中的所有元素结构为7个hlst5.swap(lst);//将lst中的元素和本身中的元素交换printList(lst5);//输出lst5中的所有元素,因为交换变成了lst中的元素printList(lst);//输出lst中的所有元素,因为交换变为之前lst5中的元素了cout ------------------------------ endl;list char lstt(5,a);//创建一个lstt链表里面含有5个alist char ::iterator ite;lstt.insert(lstt.begin(),h);//在begin位置插入一个字符hprintList(lstt);//输出lstt链表中的所有元素lstt.remove(a);//删除lstt链表中所有的字符aprintList(lstt);//输出lstt链表中的所有元素lstt.clear();//删除lstt链表中的所有元素printList(lstt);//输出lstt链表中的所有元素return 0;
}