9.8 C++

cpp 复制代码
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
class Stu
{
    friend istream &operator >>(istream &cin, Stu &p);
   friend  ostream &operator <<(ostream &cout,const Stu &p);
private:
    string name;
    int age;
public:
    Stu(){}
    Stu(string name,int age):name(name),age(age)
    {}

};
ostream &operator <<(ostream &cout,const Stu &p)
{
    cout << p.name << " " << p.age << endl;
    return cout;
}
istream &operator >>(istream &cin, Stu &p)
{
    cin >> p.name >>p.age;
    return cin;
}
void printv(vector<Stu> &v2)
{
    vector<Stu>::iterator iter;
    for(iter=v2.begin();iter !=v2.end();iter++)
    {
        cout << *iter <<" ";
    }
    cout << endl;
}
int main()
{
    Stu s1("张三",19);
    Stu s2("李四",20);
    Stu s3("王五",21);
    vector<Stu> v1;
    v1.push_back(s1);
    v1.push_back(s2);
    v1.push_back(s3);
    ofstream ofs;
    ofs.open("C:/Users/Bestow/Desktop/C++/day7/Stu.txt",ios::out);
    for(Stu s : v1)
    {
        ofs << s <<endl;
    }
    ofs.close();
    ifstream ifs;
    ifs.open("C:/Users/Bestow/Desktop/C++/day7/Stu.txt",ios::in);


    vector<Stu> v2;
    Stu temp;
    while(ifs >> temp )
        {
           v2.push_back(temp);
        }
    printv(v2);
    return 0;
}

思维导图:

list使用:

cpp 复制代码
#include <iostream>
#include <vector>
#include <list>

using namespace std;
void printv(list<int> &v)
{
    list<int>::iterator iter;
    for(iter=v.begin();iter !=v.end();iter++)
    {
        cout << *iter <<" ";
    }
    cout << endl;
}

int main()
{
    list<int> v;
    v.push_back(61);
    v.push_back(71);
    v.push_back(81);
    v.push_back(91);
    v.push_back(101);
    printv(v);
     list<int> v1(v);
     printv(v1);
     list<int> v2(v1.begin(),v1.end());
     printv(v2);
     list<int> v3(6,91);
      printv(v3);
      list<int> v4;
      v4=v3;
      printv(v4);
      list<int> v5;
      v5.assign(v2.begin(),v2.end());
      printv(v5);
      v5.assign(6,6);
      printv(v5);
      v5.resize(10);
      printv(v5);
      v5.resize(3);
      printv(v5);
      if(!v5.empty())
      {
          cout << v5.size() << endl;

      }
      cout << "=============================" << endl;
      list<int> v6=v2;
      printv(v6);
       v6.pop_back();
       printv(v6);
       v6.insert(v6.begin(),1);
       v6.insert(v6.begin(),3,66);
       printv(v6);
       v6.clear();
       printv(v6);
       cout << "=============================" << endl;
       list<int> v7=v2;
       printv(v7);

       cout << v7.front() << endl;
       cout << v7.back() << endl;
    return 0;
}
相关推荐
yaoxin5211232 小时前
384. Java IO API - Java 文件复制工具:Copy 示例完整解析
java·开发语言·python
WBluuue2 小时前
数据结构与算法:康托展开、约瑟夫环、完美洗牌
c++·算法
NotFound4862 小时前
实战指南如何实现Java Web 拦截机制:Filter 与 Interceptor 深度分享
java·开发语言·前端
木子墨5162 小时前
LeetCode 热题 100 精讲 | 并查集篇:最长连续序列 · 岛屿数量 · 省份数量 · 冗余连接 · 等式方程的可满足性
数据结构·c++·算法·leetcode
Ava的硅谷新视界3 小时前
用了一天 Claude Opus 4.7,聊几点真实感受
开发语言·后端·编程
rabbit_pro3 小时前
Python调用onnx模型
开发语言·python
王老师青少年编程3 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:均分纸牌
c++·算法·编程·贪心·csp·信奥赛·均分纸牌
weixin_513449964 小时前
PCA、SVD 、 ICP 、kd-tree算法的简单整理总结
c++·人工智能·学习·算法·机器人
浪客川4 小时前
【百例RUST - 010】字符串
开发语言·后端·rust
烟锁池塘柳04 小时前
一文讲透 C++ / Java 中方法重载(Overload)与方法重写(Override)在调用时机等方面的区别
java·c++·面向对象