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;
}
相关推荐
辞旧 lekkk2 小时前
【Qt】信号和槽
linux·开发语言·数据库·qt·学习·mysql·萌新
2zcode3 小时前
运动模糊图像复原的MATLAB仿真与优化
开发语言·matlab
袁雅倩19973 小时前
当吸尘器、筋膜枪都用上Type-C,供电方案该怎么选?浅谈PD取电芯片ECP5702的应用
c语言·开发语言·支持向量机·动态规划·推荐算法·最小二乘法·图搜索算法
Aaswk4 小时前
Java Lambda 表达式与流处理
java·开发语言·python
万邦科技Lafite4 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
王老师青少年编程5 小时前
csp信奥赛C++高频考点专项训练之字符串 --【子串查找】:[NOIP 2009 提高组] 潜伏者
c++·字符串·csp·高频考点·信奥赛·子串查找·潜伏者
Cyber4K5 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
初願致夕霞5 小时前
基于系统调用的Linux网络编程——UDP与TCP
linux·网络·c++·tcp/ip·udp
Le_ee6 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
yong99907 小时前
MATLAB读取高光谱图像
开发语言·matlab