地图的迭代器

发布于 2024-09-17 10:39:59 字数 578 浏览 7 评论 0原文

你好,我正在尝试制作一个包含以下对象的地图: Employee 类 >> 派生自 Employee :有以下类: Worker 、 Manager 和 ViceManage 。 在我的地图中,我想让对象 Employee 按他的 ID(即 char*)排序 我尝试创建一个这样的地图:`

multimap<const string,Employee*> t1
t1<string,Employee>::iterator myIterator;
Date e(17,6,1985);
Address a("somecity","somestreet",15,12);
ViceManager* d = new ViceManager("John","Doh","035845778", e,a,"03-9458353",10000,85,"gsdasda");
t1.insert(make_pair(d->GetId(),d));
myIterator=t1.begin();
myIterator->comp_value->PrintEmployee();

我的代码中遇到了很多问题,我非常想提前听听你们想说的话,谢谢

hello i am trying to make a map containing objects as the following : class Employee >>dervied from Employee : are the following classes : Worker , Manager and ViceManage.
in my map i want to have the object Employee sorted by his ID which is char*
i tried to create a map like this:`

multimap<const string,Employee*> t1
t1<string,Employee>::iterator myIterator;
Date e(17,6,1985);
Address a("somecity","somestreet",15,12);
ViceManager* d = new ViceManager("John","Doh","035845778", e,a,"03-9458353",10000,85,"gsdasda");
t1.insert(make_pair(d->GetId(),d));
myIterator=t1.begin();
myIterator->comp_value->PrintEmployee();

i got alot of problems in my code i would very much like to hear what you guys want to say thx in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2024-09-24 10:40:35

首先,迭代器是在您的情况下在 multimap 模板类中进行 typedef 的类型。因此,您需要编写以下内容:

multimap<const string,Employee*>::iterator myIterator;

对于问题的第二部分,您可以在 Employee 类中添加一个新字段,用于识别员工类型( Worker 、 Manager 和 ViceManage )。然后根据该字段进行强制转换:

if ( myIterator->type == VICE_MANAGER ) 
  static_cast<ViceManager*>(*myIterator->second)->PrintEmployee();

如果您的类是多态的(这是首选解决方案),您可以调用 PrintEmployee 而无需额外强制转换:

myIterator->second->PrintEmployee();

First of all, iterator is a type which is typedef'ed in multimap template class in your case. So you need to write the following:

multimap<const string,Employee*>::iterator myIterator;

As for second part of your question, you could add a new field in the Employee class that will identify employee type (Worker , Manager and ViceManage). Then cast depending on that field:

if ( myIterator->type == VICE_MANAGER ) 
  static_cast<ViceManager*>(*myIterator->second)->PrintEmployee();

If your classes are polymorphic (which is a preferred solution) you could call PrintEmployee without additional cast:

myIterator->second->PrintEmployee();
二手情话 2024-09-24 10:40:28

实际上,只有两个错误,都与迭代器有关。

multimap<const string,Employee*> t1;
multimap<string,Employee*>::iterator myIterator; //Note the corrected iterator type
Date e(17,6,1985);
Address a("somecity","somestreet",15,12);
Employee* d = new ViceManager("John","Doh","035845778", e,a,"03-9458353",10000,85,"gsdasda");
t1.insert(make_pair(d->GetId(),d));
myIterator=t1.begin();
myIterator->second->PrintEmployee(); //Note the way of accessing the value

我注意到这段代码并没有真正利用地图功能,我认为这适用于代码的其他部分。

编辑以修复我错过的一些错误

Really, only two errors there, both related to iterators.

multimap<const string,Employee*> t1;
multimap<string,Employee*>::iterator myIterator; //Note the corrected iterator type
Date e(17,6,1985);
Address a("somecity","somestreet",15,12);
Employee* d = new ViceManager("John","Doh","035845778", e,a,"03-9458353",10000,85,"gsdasda");
t1.insert(make_pair(d->GetId(),d));
myIterator=t1.begin();
myIterator->second->PrintEmployee(); //Note the way of accessing the value

I notice that this code isn't really taking advantage of the map functionality, I assume that's for other sections of the code.

Editing to fix some mistakes I missed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文