做网站用c语言吗,网站上可以做直播吗,凡克,附近做app的公司今日任务
试编程#xff1a;
封装一个学生的类#xff0c;定义一个学生这样类的vector容器, 里面存放学生对象#xff08;至少3个#xff09;
再把该容器中的对象#xff0c;保存到文件中。
再把这些学生从文件中读取出来#xff0c;放入另一个容器中并且遍历输出该容…今日任务
试编程
封装一个学生的类定义一个学生这样类的vector容器, 里面存放学生对象至少3个
再把该容器中的对象保存到文件中。
再把这些学生从文件中读取出来放入另一个容器中并且遍历输出该容器里的学生。
代码
#include iostream
#include vector
#include fstream
#include cstring
using namespace std;
class Stu{
private:string name;int id;
public:Stu(){}Stu(string name,int id):name(name),id(id){}string getName(Stu s){return s.name;}int getId(Stu s){return s.id;}
};int main()
{//--------------写入/*vectorStu v1;Stu stu[3]{{zzz,18},{xxx,58},{sss,24}};v1.push_back(stu[0]);v1.push_back(stu[1]);v1.push_back(stu[2]);// for(vectorStu::iterator iterv1.begin();iterv1.end();iter){
// cout iter-getName(*iter) iter-getId(*iter) endl;
// }ofstream os;os.open(../stu.txt,ios::out);for(vectorStu::iterator iterv1.begin();iterv1.end();iter){os iter-getName(*iter) | iter-getId(*iter) endl;}os.close();*///--------------读取vectorStu v2;ifstream is;is.open(../stu.txt,ios::in);char buf[128];char n[10];int a;while(is buf){//cout buf endl;//sscanf(buf,%s|%d,n,a);不太行只能用空格作为分割好像char *tempstrtok(buf,|);strcpy(n,temp);//获取第一次分割字符串temp strtok(NULL,|);//获取第二次aatoi(temp);//将字符串转为整型并返回//cout n n a a endl;Stu s(n,a);v2.push_back(s);}is.close();for(vectorStu::iterator iterv2.begin();iterv2.end();iter){cout iter-getName(*iter) iter-getId(*iter) endl;}return 0;
}运行结果 今日思维导图