zoukankan      html  css  js  c++  java
  • c++Vector的使用

    vector<Student> vec;  //声明vector容器,里边存的是Student类
        for(int i = 0; i < 5;i++)
        {
            vec.push_back(stu[i]);   //将Student数组添加到vector中
            }
        cout << "原始数据"  << endl;
      //利用循环输出
    for(int i = 0 ; i < vec.size();i++) { cout << "学生ID:" << vec[i].getId() << "姓名:" << vec[i].getName() << "年龄:" << vec[i].getAge() << endl; } cout << "顺序排列后的结果" << endl; sort(vec.begin(),vec.end(),myCompare1); //调用自定义的比较函数,根据学生学号就行排序
          //利用迭代器的方式进行遍历
    for(vector<Student>::iterator it = vec.begin(); it!=vec.end(); it++) cout << "学生ID:" << (*it).getId() << "姓名:" <<(*it).getName() << "年龄:" << (*it).getAge() << endl; cout << "逆序排序的结果" << endl; sort(vec.begin(),vec.end(),myCompare2);//调用自定义的比较函数,根据学生学号就行排序
    for(vector<Student>::iterator it = vec.begin(); it!=vec.end(); it++) cout << "学生ID:" << (*it).getId() << "姓名:" <<(*it).getName() << "年龄:" << (*it).getAge() << endl;

    //自定义的比较函数,这里是对学号进行排序
    static bool myCompare1(const Student& a1,const Student& a2)
    {
    return a1.id <= a2.id;   //按照递增的方式进行排列
    }
    static bool myCompare2(const Student& a1,const Student& a2)
    {
    return a1.id >= a2.id;  //利用递减的方式进行排列
    }



  • 相关阅读:
    python3 TypeError: a bytes-like object is required, not 'str'
    Centos 安装Python Scrapy PhantomJS
    Linux alias
    Vim vimrc配置
    Windows下 Python Selenium PhantomJS 抓取网页并截图
    Linux sort
    Linux RSync 搭建
    SSH隧道 访问内网机
    笔记《鸟哥的Linux私房菜》7 Linux档案与目录管理
    Tornado 错误 "Global name 'memoryview' is not defined"
  • 原文地址:https://www.cnblogs.com/1gaoyu/p/11939285.html
Copyright © 2011-2022 走看看