创建实体类Emp 包含名字,年龄,性别,毕业学校 然后创建对象 给名字赋值为A-Z字母开头 加上0-1000的随机数 年龄15-30的随机数 相别随机男和女 然后插入300条数据存入emp.txt文件中 按行写入 (以name,age,gender,school存入) 接下来读取数据 存入list集合 进行一下操作 1.以名字为key 对象为value 将所有元素存入map集合 2.按照年龄对元素进行排序
时间: 2024-03-02 12:54:15 浏览: 134
好的,以下是实现你提出的需求的示例代码:
```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>
#include <random>
#include <ctime>
using namespace std;
// 定义Emp类
class Emp {
public:
Emp(string name, int age, char gender, string school)
: name(name), age(age), gender(gender), school(school) {}
string getName() const { return name; }
int getAge() const { return age; }
char getGender() const { return gender; }
string getSchool() const { return school; }
private:
string name;
int age;
char gender;
string school;
};
// 生成以A-Z字母开头的随机名字
string generateName() {
static default_random_engine e(time(nullptr));
static uniform_int_distribution<unsigned> u(0, 25);
static uniform_int_distribution<unsigned> v(0, 999);
char c = 'A' + u(e);
int n = v(e);
return string(1, c) + to_string(n);
}
// 生成15-30的随机年龄
int generateAge() {
static default_random_engine e(time(nullptr));
static uniform_int_distribution<unsigned> u(15, 30);
return u(e);
}
// 生成随机性别
char generateGender() {
static default_random_engine e(time(nullptr));
static uniform_int_distribution<unsigned> u(0, 1);
return u(e) == 0 ? 'M' : 'F';
}
// 生成随机学校
string generateSchool() {
static default_random_engine e(time(nullptr));
static vector<string> schools = {"MIT", "Stanford", "Harvard", "Caltech", "Princeton"};
static uniform_int_distribution<unsigned> u(0, schools.size() - 1);
return schools[u(e)];
}
// 生成Emp对象
Emp generateEmp() {
string name = generateName();
int age = generateAge();
char gender = generateGender();
string school = generateSchool();
return Emp(name, age, gender, school);
}
// 将Emp对象写入文件
void writeEmpToFile(const Emp& emp, ofstream& ofs) {
ofs << emp.getName() << "," << emp.getAge() << "," << emp.getGender() << "," << emp.getSchool() << endl;
}
// 将Emp对象读入容器
void readEmpFromFile(vector<Emp>& emps, ifstream& ifs) {
string line;
while (getline(ifs, line)) {
size_t pos1 = line.find(",");
string name = line.substr(0, pos1);
size_t pos2 = line.find(",", pos1 + 1);
int age = stoi(line.substr(pos1 + 1, pos2 - pos1 - 1));
char gender = line[pos2 + 1];
string school = line.substr(pos2 + 3);
emps.push_back(Emp(name, age, gender, school));
}
}
int main() {
// 生成Emp对象并写入文件
ofstream ofs("emp.txt");
for (int i = 0; i < 300; ++i) {
Emp emp = generateEmp();
writeEmpToFile(emp, ofs);
}
ofs.close();
// 从文件中读取Emp对象并存入vector容器
vector<Emp> emps;
ifstream ifs("emp.txt");
readEmpFromFile(emps, ifs);
ifs.close();
// 将Emp对象存入以名字为key、Emp对象为value的map中
map<string, Emp> empMap;
for (const auto& emp : emps) {
empMap[emp.getName()] = emp;
}
// 按照年龄对Emp对象进行排序
sort(emps.begin(), emps.end(), [](const Emp& emp1, const Emp& emp2) { return emp1.getAge() < emp2.getAge(); });
// 输出排序结果
for (const auto& emp : emps) {
cout << emp.getName() << ", " << emp.getAge() << ", " << emp.getGender() << ", " << emp.getSchool() << endl;
}
return 0;
}
```
上述代码中,我们定义了Emp类,用于存储一个人的姓名、年龄、性别和毕业学校。然后编写了生成随机Emp对象的函数,以及将Emp对象写入文件和从文件中读取Emp对象的函数。接着在main函数中,我们生成了300个随机Emp对象,并将它们存入文件中。然后将文件中的Emp对象读入vector容器中,并将它们存入以名字为key、Emp对象为value的map中。最后按照年龄对Emp对象进行排序,并输出排序结果。
你可以根据自己的需要修改这段代码,比如修改生成随机Emp对象的方式,或者修改文件的读写方式。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)