gpt4 book ai didi

c++ - STL map 构建

转载 作者:行者123 更新时间:2023-11-28 01:08:03 24 4
gpt4 key购买 nike

class Garage {
string WorkerFirstName;
string WorkerLastName;
string WorkerNumber;
public:
Garage()
{
WorkerFirstName = "";
WorkerLastName = "";
WorkerNumber = "";
}

void SetFirstName(string FirstName) { WorkerFirstName = FirstName;}
void SetLastName(string LastName) { WorkerLastName = LastName; }
void SetNumber(string Number) { WorkerNumber = Number; }

string GetFirstName() { return WorkerFirstName; }
string GetLastName() { return WorkerLastName; }
string GetNumber() { return WorkerNumber; }
};

class GarageList {
Garage List[500];
int MaxSize;
int Size;
public:
GarageList()
{
MaxSize = 500;
}

... //list out functions
};

那是我的设置的精简版。我无法弄清楚如何使用基于姓氏的引用 ID 和包含车库类所有属性的值来制作 map 。我猜像 map< string, Garage List[500] > directory.

最佳答案

你很接近。您正在寻找的是:

map<string, Garage>;

在您的 GarageList 类中使用它,您无需担心条目数量, map 将为您管理。

所以 GarageList 看起来像这样:

class GarageList {
map<string, Garage> garages_;
public:
GarageList()
{
}

int Size() {
return garages_.size();
}

void AddGarage(const Garage& garage) {
garages_[garage.GetLastName()] = garage;
}

// Return true if found, false otherwise
bool FindGarage(const string& lastName, Garage& foundGarage) {
if (garages_.find(lastName) != garages_.end()) {
foundGarage = garages_[lastName];
return true;
}
return false; // garage not found with lastName
}
return
... //list out functions
};

关于c++ - STL map 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5087335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com