第七章 STL 及其应用

#include
#include
#include
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< vec{2,4,5,6,10,15,3,21,36,72,9,13};
 vector 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<
6
15
3
[Finished in 0.4s]
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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:"< idmap;  
  vector students;
  SortType sortType=BY_ID;
};void StudentManage::setSortType (SortType st){
  /*if (choice>3 || choice<0) {
    cout<<"Illigal!\n";
    return ;
  }  
  SortType st=(SortType)choice;
  cout< f;
  switch(st){
  case BY_ID: f=[](const Student& s1,const Student& s2)
  {return s1.getName()>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<>i;
    for_each(students.begin(),students.end(),[i](Student& x){if(x.getId()==i) cout< 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<>choice;
  sm.setSortType (static_cast(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<>command;
    if (command==0) break;
    switch(command){
      case 1:sm.addStudent();break;
      case 2:sm.findStudent();break;
      case 3:sm.removeStudent();break;
    }
   // cout<
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
using namespace std;



int main(){
  map count;//save as pair
  string word;
  int n;cin>>n;
  while(n--) {
    cin>>word;
    ++count[word];
  }
  for(auto e:count){//what inside map ??????
    cout<
看情况
#include
#include 
#include 
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& nums){
  double mult=accumulate(all(nums),1,product);
  return pow(mult,1.0/nums.size());
}

int main(){
  list data;
  data.pb(1);data.pb(2);data.pb(4);
  cout<
#include
#include 
#include 
#include 
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& nums){
  double mult=accumulate(all(nums),1,product);
  return pow(mult,1.0/nums.size());
}

int main(){
  list data;
  data.pb(1);data.pb(2);data.pb(4);
  cout<
#include
#include
#include

using namespace std;

using namespace std::placeholders;

int main(){
  auto divd=[](auto x,auto y){return x/y;};
  auto dint=bind(divd,_1,_2);
  auto rddou=bind(divd,_2,_1);
  cout<

include

using namespace std;

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

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

0x7ffee452bb3c
1
1
[Finished in 0.4s]
#include
#include
#include
#include
#include
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(sum)/static_cast(num);
    }
    operator double()//重载的好处,简洁!
    {
        return static_cast(sum)/static_cast(num);
    }
private:
    long num;
    long sum;
};

template
auto add(T1 &a)->decltype(a+10){
  return a+=10;
}

template
class AddValue
{
public:
    AddValue(const T& v):theValue(v){}
    void operator() (T& elem)const
    {
        elem+=theValue;
    }
private:
    T theValue;
};

int main()
{
    vector 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:"<);//参数可以自己改,eg:*(myvector.begin())
    for_each (myvector.begin(), myvector.end(), myobject);
    cout << endl;

    double mv2=for_each(myvector.begin(),myvector.end(),MeanVlaue());
    cout<<"MeanValue:"<

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

```java
#include
#include
using namespace std;

template
T use_f(T x,function f){
  static int count=0;
  ++count;
  cout<<"count = "<#include
#include
#include
using namespace std;

#define all(x) x.begin(),x.end()

int main(){
  vector vec1(10,10),vec2(10,11);
  for_each(all(vec1),[](int x){cout<
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
using namespace std;
#define all(x) x.begin(),x.end()
#define pb push_back

template
void out(T x){
  cout< vec;
  cout<);
  cout< vec2(vec);
  reverse(all(vec));
  for_each(all(vec),out);
  cout<);
  cout<);
  cout<
#include 
#include  
using namespace std;
#define all(x) x.begin(),x.end()
#define pb push_back

template
void out(T x){
  cout< vec;
  cout<);
  cout< vec2(vec);
  reverse(all(vec));
  for_each(all(vec),out);
  cout<);
  cout<);
  cout<

你可能感兴趣的:(第七章 STL 及其应用)