美文网首页
第七章 STL 及其应用

第七章 STL 及其应用

作者: DeepWeaver | 来源:发表于2018-01-02 21:42 被阅读20次
    #include<iostream>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    class Predicate{
    private:
      int count=0;
    public:
      bool operator()(int data){
        if ( count>=3 ) return false;
        else{
          bool x=(data<=20 && data%3==0);
          count+=x;
         // cout<<x<<" "<<count<<endl;
          return x;
        }
      }
    };
    
    int main(){
     Predicate predicate;
     vector<int> vec{2,4,5,6,10,15,3,21,36,72,9,13};
     vector<int> result;
     result.resize (vec.size());
    // auto end=copy_if(vec.begin (),vec.end (),result.begin(),predicate);
     int count=0;
     auto end=copy_if(vec.begin (),vec.end (),result.begin()
                      ,[&count](int data)->bool{    
                          if ( count>=3 ) return false;
                          else{
                            bool x=(data<=20 && data%3==0);
                            count+=x;return x;
                          }
                        }
                      );
    
     result.erase(end,result.end ());
     for_each(result.begin (),result.end (),[](int e){cout<<e<<endl;});
     return 0;
    }
    
    6
    15
    3
    [Finished in 0.4s]
    
    #include <iostream>
    #include <vector>
    #include <map>
    #include <functional>
    #include <string>
    #include <algorithm>
    #include <sstream>
    using namespace std;
    class Student{
    private:
      string name;
      string specialty;//专业
      long id;// 学号
      double creditPoint; //学分积点
    public:
      Student(string n,string s,long i,double c)
      :name(n),specialty(s),id(i),creditPoint(c){}
      void setCreditPoint(double c){creditPoint=c;}
      double getCreditPoint()const{return creditPoint;}
      string getName()const{return name;}
      long getId()const{return id;}
      string getSpecialty()const{return specialty;}
      friend ostream& operator<<(ostream& out,const Student& s);
    };
    ostream& operator<<(ostream& out,const Student& s){
      cout<<"Name:"<<s.name<<endl;
      cout<<" Id:"<<s.id<<endl;
      cout<<" Specialty:"<<s.specialty<<endl;
      cout<<" CreditPoint:"<<s.creditPoint<<endl;
    }
    
    
    class StudentManage{
    public:
      enum SortType{BY_NAME,BY_ID,BY_SPECIALTY,BY_CRDITPOINT};
      StudentManage()=default;
      void addStudent();
      void removeStudent();
      void setSortType(SortType st);
      void findStudent();
      int size()const{ return students.size();}
      friend ostream& operator<<(ostream& out,const StudentManage& sm);
    private:
      map<long,int> idmap;  
      vector<Student> students;
      SortType sortType=BY_ID;
    };void StudentManage::setSortType (SortType st){
      /*if (choice>3 || choice<0) {
        cout<<"Illigal!\n";
        return ;
      }  
      SortType st=(SortType)choice;
      cout<<st<<endl;*/
    //function+lambda......666
      function<bool(const Student&,const Student&)> f;
      switch(st){
      case BY_ID: f=[](const Student& s1,const Student& s2)
      {return s1.getName()<s2.getName();};break;
      case BY_NAME:f=[](const Student& s1,const Student& s2)
      {return s1.getId()<s2.getId();};break;
      case BY_SPECIALTY:f=[](const Student& s1,const Student& s2)
      {return s1.getSpecialty()<s2.getSpecialty();};break;
      case BY_CRDITPOINT:f=[](const Student& s1,const Student& s2)
      {return s1.getCreditPoint()<s2.getCreditPoint();};break;
      }
      sort(students.begin(),students.end(),f);
    }
    
    void StudentManage::addStudent(){
      string n,s;long i;double c;
    //  cout<<"Please enter the name, speciality, id ,credit point of the student:\n";
      cin>>n>>s>>i>>c;
      if (idmap.count(i)){
        cout<<"This id has existed!\n";
        return ;
      }
      else{
        idmap[i]++;
        students.push_back(Student(n,s,i,c));
        setSortType(sortType);
      }
    }
    void StudentManage::removeStudent(){
      cout<<"Please enter the student's id you want to remove:\n";
      long x;cin>>x;
      if (!idmap.count(x)){
        cout<<"There is no such a student!\n";
        return ;
      }
      for(auto i=students.begin();i!=students.end();++i){
        if( (*i).getId()==x ) {
          idmap.erase(x);
          students.erase(i);
          cout<<"OK!This student is removed!\n";
          return ;
        }
      }
    }
    void StudentManage::findStudent(){
      cout<<"Please enter a name or an id:\n";
      string n;cin>>n;
      long i;
      if (n[0]<='9' && n[0]>='0'){
        stringstream ss;ss<<n;ss>>i;
        for_each(students.begin(),students.end(),[i](Student& x){if(x.getId()==i) cout<<x<<endl;});
      } 
      else {
        for_each(students.begin(),students.end(),[&n](Student& x){if(x.getName()==n) cout<<x<<endl;});
      }
    }
    
    ostream& operator<<(ostream& out,const StudentManage& sm){
      for(auto& e:sm.students) out<<e<<endl;
      return out;
    }
    int main(){
    /*  cout<<"Test1:\n";
      vector<Student> vec{
      {"zhang","Computer",11001,4.2},
      {"wang","Computer",11002,3.8},
      {"Li","English",12001,4.1},
      {"Tang","English",12002,3.9},
      {"Qian","Computer",11003,4.0},
      {"Song","Geology",10001,4.1}
      };
      for(auto e: vec) 
      cout<<e;
    */
      cout<<"Test2:\n";
      StudentManage sm;
      
      cout<<"Welcome to the Student Management System!\nBut can "
          <<"I must say the our teacher has less creativity in teaching...\n"
          <<"In two continuous semeters this project always is the hardest task...\n"
          <<"I only look foward there will be some intersting task in the 14-15th week.\n\n";
     
    
      cout<<"sort type(0-name,1-id,2-specialty,3-creditpoint):\n";
      int choice;
      cin>>choice;
      sm.setSortType (static_cast<StudentManage::SortType>(choice));
    
      cout<<"\nPlease enter the student number:\n";
      int n;cin>>n;
    
        cout<<"Please enter the name, speciality, id ,credit point of the student:\n";
        while(n--) sm.addStudent();
      cout<<sm;
    
      int command;
      while(1){  
        cout<<"Please enter command, 1-add,2-find,3-remove,0-exit:\n";
        cin>>command;
        if (command==0) break;
        switch(command){
          case 1:sm.addStudent();break;
          case 2:sm.findStudent();break;
          case 3:sm.removeStudent();break;
        }
       // cout<<endl<<"Now:\n";
       // cout<<sm;
      }
      cout<<"Syytem is shot down!\n";
      return 0;
    }
    
    Test2:
    Welcome to the Student Management System!
    But can I must say the our teacher has less creativity in teaching...
    In two continuous semeters this project always is the hardest task...
    I only look foward there will be some intersting task in the 14-15th week.
    
    #include<bits/stdc++.h>
    using namespace std;
    
    
    
    int main(){
      map<string,int> count;//save as pair
      string word;
      int n;cin>>n;
      while(n--) {
        cin>>word;
        ++count[word];
      }
      for(auto e:count){//what inside map ??????
        cout<<e.first<<"\t"<<e.second<<"\n";//it's sorted as pair
      }
    }
    
    看情况
    
    #include<iostream>
    #include <map>
    #include <string>
    using namespace std;
    #define all(x) x.begin(),x.end()
    #define pb push_back
    
    int product(int num1,int num2){
      return num1*num2;
    }
    double geoMean(const list<int>& nums){
      double mult=accumulate(all(nums),1,product);
      return pow(mult,1.0/nums.size());
    }
    
    int main(){
      list<int> data;
      data.pb(1);data.pb(2);data.pb(4);
      cout<<geoMean(data)<<endl;
    }
    
    #include<iostream>
    #include <map>
    #include <string>
    #include <list>
    using namespace std;
    #define all(x) x.begin(),x.end()
    #define pb push_back
    
    int product(int num1,int num2){
      return num1*num2;
    }
    double geoMean(const list<int>& nums){
      double mult=accumulate(all(nums),1,product);
      return pow(mult,1.0/nums.size());
    }
    
    int main(){
      list<int> data;
      data.pb(1);data.pb(2);data.pb(4);
      cout<<geoMean(data)<<endl;
    }
    
    #include<iostream>
    #include<algorithm>
    #include<functional>
    
    using namespace std;
    
    using namespace std::placeholders;
    
    int main(){
      auto divd=[](auto x,auto y){return x/y;};
      auto dint=bind<int>(divd,_1,_2);
      auto rddou=bind(divd,_2,_1);
      cout<<dint(20,6)<<endl;
      cout<<rddou((double)20,(double)6)<<endl;
    }
    

    include<iostream>

    using namespace std;

    int main(){
    enum week{Monday,Tuesday,Wednesday};

    week a=Monday;
    a=(week)1;
    cout<<&a<<endl;
    cout<<(int)a<<endl;
    cout<<a<<endl;
    }

    0x7ffee452bb3c
    1
    1
    [Finished in 0.4s]
    
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<algorithm>
    using namespace std;
    void myfunction (int i)
    {
        cout << " " << i;
    }
    struct myclass
    {
        void operator() (int i)
        {
            cout << " " << i;
        }
    } myobject;
    
    class MeanVlaue
    {
    public:
        MeanVlaue():num(0),sum(0){}
        void operator() (int elem)
        {
            num++;
            sum+=elem;
        }
        double value()
        {
            return static_cast<double>(sum)/static_cast<double>(num);
        }
        operator double()//重载的好处,简洁!
        {
            return static_cast<double>(sum)/static_cast<double>(num);
        }
    private:
        long num;
        long sum;
    };
    
    template<typename T1>
    auto add(T1 &a)->decltype(a+10){
      return a+=10;
    }
    
    template<class T>
    class AddValue
    {
    public:
        AddValue(const T& v):theValue(v){}
        void operator() (T& elem)const
        {
            elem+=theValue;
        }
    private:
        T theValue;
    };
    
    int main()
    {
        vector<int> myvector;
        myvector.push_back(10);
        myvector.push_back(20);
        myvector.push_back(30);
    
        cout << "myvector contains:";
        for_each (myvector.begin(), myvector.end(), myfunction);
    
        // or:
        cout << "\nmyvector contains:";
        for_each (myvector.begin(), myvector.end(), myobject);
    
        cout << endl;
    
        MeanVlaue mv=for_each(myvector.begin(),myvector.end(),MeanVlaue());
        cout<<"MeanValue:"<<mv.value()<<endl;
    
        for_each(myvector.begin(), myvector.end(), add<int>);//参数可以自己改,eg:*(myvector.begin())
        for_each (myvector.begin(), myvector.end(), myobject);
        cout << endl;
    
        double mv2=for_each(myvector.begin(),myvector.end(),MeanVlaue());
        cout<<"MeanValue:"<<mv2<<endl;
    
        return 0;
    }
    

    myvector contains: 10 20 30
    myvector contains: 10 20 30
    MeanValue:20
    20 30 40
    MeanValue:30
    [Finished in 0.4s]

    ```java
    #include<iostream>
    #include<functional>
    using namespace std;
    
    template<typename T>
    T use_f(T x,function<T(T)> f){
      static int count=0;
      ++count;
      cout<<"count = "<<count
          <<" &count = "<<&count<<endl;
      return f(x); 
    }
    
    class Fp{
    private:
      double p;
    public:
      Fp(double _p):p(_p){}
      double operator()(double x){return x*p;}
    };
    
    double dub(double u){
      return u*u;
    }
    
    int main(){
    
    //lambda
      cout<<use_f<double>(0.5,[](double u){return u*u;})<<endl;
    //class
      cout<<use_f<double>(0.5,Fp(0.5))<<endl;
    //function
      cout<<use_f<double>(0.5,dub)<<endl;
    }
    
    count = 1 &count = 0x10e73c260
    0.25
    count = 2 &count = 0x10e73c260
    0.25
    count = 3 &count = 0x10e73c260
    0.25
    [Finished in 0.5s]
    
    #include<iostream>
    #include<algorithm>
    #include<vector>
    using namespace std;
    
    #define all(x) x.begin(),x.end()
    
    int main(){
      vector<int> vec1(10,10),vec2(10,11);
      for_each(all(vec1),[](int x){cout<<x<<" ";});cout<<endl;
      cout<<0<<endl;
      transform(all(vec1),vec1.begin(),[](int x){return x*x;});
      cout<<1<<endl;
      for_each(all(vec1),[](int x){cout<<x<<" ";});cout<<endl;
      cout<<2<<endl;
      transform(all(vec1),vec2.begin(),vec1.begin(),[](int x,int y){return x*y;});
      cout<<3<<endl;
      for_each(all(vec1),[](int x){cout<<x<<" ";});cout<<endl;
      cout<<4<<endl;
    }
    
    10 10 10 10 10 10 10 10 10 10 
    0
    1
    100 100 100 100 100 100 100 100 100 100 
    2
    3
    1100 1100 1100 1100 1100 1100 1100 1100 1100 1100 
    4
    [Finished in 0.4s]
    
    #include<bits/stdc++.h>
    using namespace std;
    #define all(x) x.begin(),x.end()
    #define pb push_back
    
    template<typename T>
    void out(T x){
      cout<<x<<" ";
    }
    
    int fun(int x,int y){
      return x*y;
    }
    bool mod2(int x){
      return x%2==0;
    }
    
    class Test{
    public:
      bool operator()(int x) {
        return x%2==0;
      }
    };
    
    int main(){
      srand((unsigned)time(NULL));
    
      vector<int> vec;
      cout<<vec.capacity()<<endl;
      cout<<vec.max_size()<<endl;
      //1e9 is the max_size
      vec.reserve(100);
      cout<<vec.size()<<endl;
      //reserve doesn't make the size
      vec.resize(100);
      cout<<vec.size()<<endl;
      //resize will make the size
    
      vec.clear();
      cout<<vec.capacity()<<endl;
      cout<<vec.size()<<endl;
    
      vec.pb(1);
        vec.pb(3);
          vec.pb(2);
      for_each(all(vec),out<int>);
      cout<<endl;
      vector<int> vec2(vec);
      reverse(all(vec));
      for_each(all(vec),out<int>);
      cout<<endl;
    
      vec.clear();
      vec.resize(3);
      swap_ranges(all(vec2),vec.begin());//will be 
      
      for_each(all(vec),out<int>);
      cout<<endl;
      cout<<accumulate(all(vec),1,fun)<<endl;
    
      Test t;
      cout<<t.operator()(2)<<endl;
    
      generate(all(vec),rand);
      cout<<endl;
      for_each(all(vec),out<int>);
      cout<<endl;
      cout<<"numbers can divide 2 are "<<count_if(all(vec),mod2)<<endl;
    }
    
    #include <iostream>
    #include <vector> 
    using namespace std;
    #define all(x) x.begin(),x.end()
    #define pb push_back
    
    template<typename T>
    void out(T x){
      cout<<x<<" ";
    }
    
    int fun(int x,int y){
      return x*y;
    }
    bool mod2(int x){
      return x%2==0;
    }
    
    class Test{
    public:
      bool operator()(int x) {
        return x%2==0;
      }
    };
    
    int main(){
      srand((unsigned)time(NULL));
    
      vector<int> vec;
      cout<<vec.capacity()<<endl;
      cout<<vec.max_size()<<endl;
      //1e9 is the max_size
      vec.reserve(100);
      cout<<vec.size()<<endl;
      //reserve doesn't make the size
      vec.resize(100);
      cout<<vec.size()<<endl;
      //resize will make the size
    
      vec.clear();
      cout<<vec.capacity()<<endl;
      cout<<vec.size()<<endl;
    
      vec.pb(1);
        vec.pb(3);
          vec.pb(2);
      for_each(all(vec),out<int>);
      cout<<endl;
      vector<int> vec2(vec);
      reverse(all(vec));
      for_each(all(vec),out<int>);
      cout<<endl;
    
      vec.clear();
      vec.resize(3);
      swap_ranges(all(vec2),vec.begin());//will be 
      
      for_each(all(vec),out<int>);
      cout<<endl;
      cout<<accumulate(all(vec),1,fun)<<endl;
    
      Test t;
      cout<<t.operator()(2)<<endl;
    
      generate(all(vec),rand);
      cout<<endl;
      for_each(all(vec),out<int>);
      cout<<endl;
      cout<<"numbers can divide 2 are "<<count_if(all(vec),mod2)<<endl;
    }
    

    相关文章

      网友评论

          本文标题:第七章 STL 及其应用

          本文链接:https://www.haomeiwen.com/subject/xrzqnxtx.html