• STL:C++标准容器库 :vector


    STL:C++标准容器库 :vector 

    #include <iostream>
    #include<string>
    #include<vector>
    #include<algorithm>
    
    using namespace std;
    
    class Student
    {
    public:
        Student()
        {
            this->name = " ";
            this->age = 0;
        }
    
        Student(const string &name,const int &age)
        {
            this->name = name;
            this->age = age;
        }
    
        void showStu()const
        {
            cout<<this->name<<" "<<this->age<<endl;
        }
    
    
    private:
        string name;
        int age;
    };
    
    void show(const Student &stu)
    {
        stu.showStu();
    }
    int main(int argc, char *argv[])
    {
        vector<Student> vec_stu;
        vec_stu.push_back(Student("zhangsan",22));
        vec_stu.push_back(Student("lisi",21));
        vec_stu.push_back(Student("wangwu",28));
    
        //通过下标遍历
    //    for(int i=0;i<vec_stu.size();i++)
    //    vec_stu.at(i).showStu();
        //迭代子遍历
    //    vector<Student>::iterator it;
    //    for(it = vec_stu.begin();it!=vec_stu.end();it++)//it 就是一个Student *
    //        it->showStu();  //所以it-> 但是.不出来;
    //    for_each(vec_stu.begin(),vec_stu.end(),show);  //需要在外部定义函数
        //通过通用算法遍历
        //插入
    //    vector<Student> vec_stu2;
    //    vec_stu2.push_back(Student("yankang",22));
    //    vec_stu2.push_back(Student("ziwen",21));
    //    vec_stu.insert(vec_stu.begin(),vec_stu2.begin(),vec_stu2.end());
    //    vec_stu.insert(vec_stu.begin(),2,Student("xiaoli",52));
    //    for_each(vec_stu.begin(),vec_stu.end(),show); 
          vector<Student>::iterator ist = vec_stu.begin();  //查找
    //      ist = find(vec_stu.begin(),vec_stu.end(),Student("lisi",0));  单值
    //         if(ist == vec_stu.end())
    //        cout<<"not found!"<<endl;
    //      else
    //          cout<<"found!"<<endl;
    
          while((ist=find(ist,vec_stu.end(),Student("lisi",0)))!=vec_stu.end())//多值  需要对==操作符重载
          {
              ist->showStu();
              ist++;
          }
    bool operator == (const Student &stu1,const Student &stu2)  ==号操作符重载  同时需要在 类内声明为友元函数
    {                                      //定义在类外
        cout<<"bool operator == (const Student &stu1,const Student &stu2)"<<endl;
        if(stu1.name == stu2.name)
            return true;
        return false;
    }
  • 相关阅读:
    ConcurrentHashMap的初步使用场景、源码分析讲解(中)
    ConcurrentHashMap的初步使用场景、源码分析讲解(上)
    CyclicBarrier用例、源码分析讲解
    Semaphore用例、源码分析讲解
    CountDownLatch用例、源码分析讲解
    Condition用例、源码分析详解(下)
    Condition用例、源码分析详解(上)
    图解数据结构之数组、链表、栈、队列
    Python--day27--复习
    Python--day26--反射
  • 原文地址:https://www.cnblogs.com/xiaozoui11cl/p/12778019.html
Copyright © 2020-2023  润新知