• c++(8.29)auto关键字,lambda表达式,数据类型转换,标准模板库,list,文件操作+Xmind


    作业:

    封装一个学生的类,定义一个学生这样类的vector容器, 里面存放学生对象(至少3个)

    再把该容器中的对象,保存到文件中。

    再把这些学生从文件中读取出来,放入另一个容器中并且遍历输出该容器里的学生。

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. class Stu
    6. {
    7. friend istream & operator>>(istream &cin,Stu &R);
    8. friend ostream & operator<<(ostream &cout,const Stu &R);
    9. private:
    10. string name;
    11. int age;
    12. public:
    13. Stu(){};
    14. Stu(string name,int age):name(name),age(age){};
    15. };
    16. ostream & operator<<(ostream &cout,const Stu &R)
    17. {
    18. cout << R.name << " ";
    19. cout << R.age << endl;
    20. return cout;
    21. }
    22. istream & operator>>(istream &cin,Stu &R)
    23. {
    24. cin >> R.name;
    25. cin >> R.age;
    26. return cin;
    27. }
    28. int main()
    29. {
    30. Stu s1("张三",18);
    31. Stu s2("李四",20);
    32. Stu s3("王五",19);
    33. vector stu;
    34. stu.push_back(s1);
    35. stu.push_back(s2);
    36. stu.push_back(s3);
    37. ofstream ofs;
    38. ofs.open("D:/2.txt",ios::out);
    39. vector::iterator iter;
    40. for(iter = stu.begin();iter!=stu.end();iter++)
    41. {
    42. ofs << *iter ;
    43. }
    44. ofs.close();
    45. vectorstu1;
    46. Stu s;
    47. ifstream ifs;
    48. ifs.open("D:/2.txt",ios::in);
    49. while(ifs>>s)
    50. {
    51. stu1.push_back(s);
    52. }
    53. for(iter=stu1.begin();iter!=stu1.end();iter++)
    54. {
    55. cout << *iter ;
    56. }
    57. ifs.close();
    58. return 0;
    59. }

     1.模板类

    1. #include
    2. using namespace std;
    3. template < class T,class N>
    4. class A
    5. {
    6. private:
    7. T t;
    8. N n;
    9. public:
    10. A(){};//无参构造
    11. A(T t,N n):t(t),n(n){}//有参构造
    12. void show()
    13. {
    14. cout << t << endl << n << endl;
    15. }
    16. };
    17. int main()
    18. {
    19. Aint> a("张三",18);
    20. a.show();
    21. return 0;
    22. }

    2.异常(异常情况为取钱时取的钱小于0或者大于余额)

    1. #include
    2. using namespace std;
    3. class BankAccount
    4. {
    5. private:
    6. double balance;
    7. public:
    8. BankAccount(){};
    9. BankAccount(double balance):balance(balance){};
    10. void withdraw(double money)
    11. {
    12. if(money<0)
    13. {
    14. throw(invalid_argument("取款金额不能为负数"));
    15. }
    16. else if(money>balance)
    17. {
    18. throw(runtime_error("余额不足"));
    19. }
    20. else
    21. {
    22. balance -= money;
    23. cout << "余额为:" << balance << endl;
    24. }
    25. }
    26. };
    27. int main()
    28. {
    29. BankAccount account1(1000);
    30. try {
    31. account1.withdraw(-100);
    32. } catch (invalid_argument &e)
    33. {
    34. cout << "Erro:" << e.what() << endl;
    35. } catch (runtime_error &e)
    36. {
    37. cout << "Erro:" << e.what() << endl;
    38. }
    39. try {
    40. account1.withdraw(1500);
    41. } catch (invalid_argument &e)
    42. {
    43. cout << "Erro:" << e.what() << endl;
    44. } catch (runtime_error &e)
    45. {
    46. cout << "Erro:" << e.what() << endl;
    47. }
    48. try {
    49. account1.withdraw(500);
    50. } catch (invalid_argument &e)
    51. {
    52. cout << "Erro:" << e.what() << endl;
    53. } catch (runtime_error &e)
    54. {
    55. cout << "Erro:" << e.what() << endl;
    56. }
    57. return 0;
    58. }

    3.lambda表达式和auto的使用

    1. #include
    2. using namespace std;
    3. int main()
    4. {
    5. int a=100;
    6. double b=3.14;
    7. char c='a';
    8. auto fun=[a,b,c](){};//捕获外界a,b,c变量的值,fun函数中的a,b,c不是外界的a,b,c,地址不同,
    9. //想要修改fun中的a,b,c的值,必须在()后加上mutable
    10. auto fun1=[=](){};//捕获外界所有的变量值
    11. auto fun2=[&a,&b](){};//捕获外界a,b变量的地址,fun函数中的a,b是外界的a,b,地址相同,
    12. //想要修改fun2中的值,可以直接改变
    13. auto fun3=[&](){};//捕获外界所有的变量的地址
    14. auto fun4=[=,&a,&b](){};//捕获外界所有的值,但是变量a和变量b是引用捕获,
    15. //fun函数中的a,b是外界的a,b,地址相同,可以直接修改,不用加上mutable
    16. auto fun5=[&,a,b](){};//捕获外界所有变量的地址,但变量a,b捕获的是值,修改需要加mutable
    17. return 0;
    18. }

    4.容器

    1. #include
    2. #include
    3. using namespace std;
    4. void printVector(vector<int> &v)
    5. {
    6. vector <int>::iterator ite;//创建一个vector类型的迭代器ite
    7. for(ite=v.begin();ite!=v.end();ite++)
    8. {
    9. cout << *ite << ' ';
    10. }
    11. cout << endl;
    12. }
    13. int main()
    14. {
    15. //容器
    16. vector<int>v;//无参构造容器v
    17. v.push_back(10);//尾插
    18. v.push_back(20);
    19. v.push_back(30);
    20. v.push_back(40);
    21. //算法
    22. printVector(v);
    23. vector<int>v2(v.begin(),v.end());//拷贝v中begin到end区间中的值
    24. printVector(v2);
    25. vector<int>v3(6,10);//拷贝构造,将6个10拷贝给v3
    26. printVector(v3);
    27. vector<int>v4=v2;//拷贝构造,将v2中的值拷贝给v4
    28. printVector(v4);
    29. vector<int>v5(v3);//拷贝构造,将v3中的值拷贝给v5
    30. printVector(v5);
    31. vector<int>v6;
    32. v6=v5;//拷贝赋值,将v5的值拷贝一份给v6
    33. v6.assign(v5.begin(),v5.end());//将v5begin到end区间的值拷贝一份赋值给v6
    34. v6.assign(8,99);//将8个99拷贝一份赋值给v6
    35. if(v6.empty())//判断v6是否为空
    36. {
    37. cout << "v6容器为空" << endl;
    38. }
    39. else
    40. {
    41. cout << "v6容器的容量为:" << v6.capacity() << endl;
    42. cout << "v6容器的大小(容器中的元素个数)为:" << v6.size() <
    43. printVector(v6);
    44. v6.resize(15);//大小重新设置为15,数据不够补充0
    45. printVector(v6);
    46. }
    47. return 0;
    48. }

    5.list链表

    1. #include
    2. #include
    3. using namespace std;
    4. void printList(list<char> &v)
    5. {
    6. list <char>::iterator ite;//创建一个list类型的迭代器ite
    7. for(ite=v.begin();ite!=v.end();ite++)
    8. {
    9. cout << *ite << ' ';
    10. }
    11. cout << endl;
    12. }
    13. int main()
    14. {
    15. list <char> lst;//定义一个链表,里面存放char类型元素
    16. lst.push_back('h');//存放一个字符a
    17. lst.push_back('e');
    18. lst.push_back('l');
    19. lst.push_back('l');
    20. lst.push_back('o');
    21. printList(lst);//输出lst中的所有元素
    22. lst.push_front('a');//在lsit表头部插入字符'a'
    23. lst.push_back('!');//在尾部插入元素'!'
    24. printList(lst);//输出lst中的所有元素
    25. lst.pop_front();//删除list表头部元素
    26. lst.pop_back();//删除list表尾部元素
    27. printList(lst);//输出lst中的所有元素
    28. list <char> lst2(lst.begin(),lst.end());//拷贝构造函数,将lst内从begin到end的元素拷贝到链表lst2中
    29. printList(lst2);//输出lst2中的所有元素
    30. list <char> lst3(3,'6');//拷贝构造函数,将3个字符6存入链表lst3中
    31. printList(lst3);//输出lst3中的所有元素
    32. list <char> lst4(lst3);//拷贝构造函数,将lst3中的元素拷贝到lst4中
    33. printList(lst4);//输出lst4中的所有元素
    34. list <char> lst5;
    35. lst5.assign(lst.begin(),lst.end());//拷贝赋值函数,将lst中begin到end区间的值拷贝一份赋值到lst5中
    36. printList(lst5);//输出lst5中的所有元素,结果和lst结果一致
    37. lst5.assign(7,'h');//将5个h赋值到lst5中
    38. printList(lst5);//输出lst5中的所有元素,结构为7个h
    39. lst5.swap(lst);//将lst中的元素和本身中的元素交换
    40. printList(lst5);//输出lst5中的所有元素,因为交换,变成了lst中的元素
    41. printList(lst);//输出lst中的所有元素,因为交换,变为之前lst5中的元素了
    42. cout << "------------------------------" << endl;
    43. list <char> lstt(5,'a');//创建一个lstt链表,里面含有5个'a'
    44. list <char> ::iterator ite;
    45. lstt.insert(lstt.begin(),'h');//在begin位置插入一个字符'h'
    46. printList(lstt);//输出lstt链表中的所有元素
    47. lstt.remove('a');//删除lstt链表中所有的字符'a'
    48. printList(lstt);//输出lstt链表中的所有元素
    49. lstt.clear();//删除lstt链表中的所有元素
    50. printList(lstt);//输出lstt链表中的所有元素
    51. return 0;
    52. }

    ​​​​​​​

  • 相关阅读:
    计算机毕业设计Java校园社团管理平台演示录像2021(源码+系统+mysql数据库+Lw文档)
    线程互斥锁、进程互斥锁、死锁,递归锁
    【Linux 文件的权限管控信息,读写执行三种权限含义】
    Python爬虫-IP隐藏技术与代理爬取
    Android如何实现实时音视频会议的背景分割
    Go使用Gin+Redis实现增删改查
    算法 day29 回溯5
    SELECT COUNT(*) 会造成全表扫描吗?
    单链表的创建和遍历的分析实现(思路分析) [数据结构][Java]
    flutter开发实战-自定义长按TextField输入框剪切、复制、选择全部菜单AdaptiveTextSelectionToolba样式UI效果
  • 原文地址:https://blog.csdn.net/yymbxqdm/article/details/132567762