C++ STL 第四次实验

来源:互联网 发布:淘宝免费申请试用手机 编辑:程序博客网 时间:2024/06/03 18:18

从billionaires中找到中国38~45岁的亿万富翁!

billionaires.bin来源是福布斯网页

billion.bin是struct billion的2进制存储,billion的定义为:

struct billion {
  int no;
  char name[20];
  char account[6];
  int age;
  char company[20];
  char country[20];
};

要求内容:
1、读取2进制文件,形成一个vector的容器内容。
2、使用for_each遍历vector容器内所有元素,调用find_billion(“China”, 38, 45),打印出满足要求的所有富翁。

billion.bin文件放不上来,需要的可以加我的QQ:739444364,不懂得也可以问

#include <iostream>#include <fstream>#include <algorithm>#include <vector>#include <string.h>using namespace std;struct billion{    int no;    char name[20];    char account[6];    int age;    char company[20];    char country[20];};class find_billion{private:    billion b;    char* country;    int min;    int max;public:    find_billion(){        memset(this->country,NULL,sizeof(this->country));        min = 0;        max = 0;    }    find_billion(char *country,int min,int max){        this->country = country;        this->min = min;        this->max = max;    }    void operator()(billion b){        if(min<b.age<max && !strcmp(b.country,country)){            cout << b.no << "\n";            cout << b.name << "\n";            cout << b.account << "\n";            cout << b.age << "\n";            cout << b.company << "\n";            cout << b.country << "\n";        }    }};int main(){    ifstream in;    in.open("billion.bin",ios::binary);    vector<billion>v;    billion b;    in.read((char*)&b,sizeof(billion));    while(!in.eof()){        in.read((char*)&b,sizeof(billion));        v.push_back(b);    }    find_billion bil = for_each(v.begin(),v.end(),find_billion("China",38,45));    in.close();}