陣列使用find查詢用法

露水上的青蛙發表於2013-07-13

#include "stdafx.h"
#include <string>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;

class Student
{
public:
 Student(){}
 Student(string name,string sex,int age,string number);
 int getAge(){return _age;}
    string getNumber(){return _number;}
 bool operator==(const string&str)//過載操作符
 {
       return str==_number;
 }
 bool operator==(const int&stemp)
 {
  return stemp==_age;
 }
private:
 string _name;
 string _sex;
 int _age;
 string _number;
};

Student::Student(string name,string sex,int age,string number)
{
 _name=name;
 _sex=sex;
 _age=age;
 _number=number;
}

int _tmain(int argc, _TCHAR* argv[])
{
 Student st[5]={
      Student("la","男",12,"122"),
   Student("小林","男",13,"123"),
   Student("小林","男",14,"124")
 };

 string strnum;
 cout<<"enter number:";
 cin>>strnum;

 Student *present=find(st,st+4,strnum);
 cout<<"這個學生的學號是:"<<present->getNumber()<<endl;

 int age;
 cout<<"enter age: ";
 cin>>age;
 Student *pre=find(st,st+4,age);
 cout<<"這個學生的年齡是:"<<pre->getAge();
 getchar();
 getchar();
 return 0;
}

 

相關文章