地图的迭代器
你好,我正在尝试制作一个包含以下对象的地图: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,迭代器是在您的情况下在
multimap
模板类中进行 typedef 的类型。因此,您需要编写以下内容:对于问题的第二部分,您可以在 Employee 类中添加一个新字段,用于识别员工类型( Worker 、 Manager 和 ViceManage )。然后根据该字段进行强制转换:
如果您的类是多态的(这是首选解决方案),您可以调用
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: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 your classes are polymorphic (which is a preferred solution) you could call
PrintEmployee
without additional cast:实际上,只有两个错误,都与迭代器有关。
我注意到这段代码并没有真正利用地图功能,我认为这适用于代码的其他部分。
编辑以修复我错过的一些错误
Really, only two errors there, both related to iterators.
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