是否可以创建以键为对象的地图并通过键(即对象的字段)查找?

时间:2018-12-29 10:06:23

标签: c++ dictionary

我有2个大类: 英雄: *

//
// Created by david on 25/12/18.
//
#ifndef UNTITLED4_HERO_H
#define UNTITLED4_HERO_H
#include "Creature.h"
#include <iostream>
#include <string>
#include <map>
#include <list>
using namespace std;
class Hero  {
    std::string m_userName;
protected:
    std::string m_typeOfHero;
    map<Hero*,int>creaturesMap ;  // i have only 5 heros so i want to find them by their type
    double gold;
public:
    Hero();
    Hero(std::string userName);
    virtual void useSpecialSkill()=0;
    void buyCreatures();
    void getDailyGold();
    void attack(Hero& hero) ;
    void showDetails()const;
    void endTurn();
    double getGold()const;
    string getName()const;
    string getType()const;
    void setGold(double gold);
    void setType(std::string type);
};

生物:

#ifndef UNTITLED4_CREATURE_H
#define UNTITLED4_CREATURE_H
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Creature {
protected:
int powerAttack;
int powerDefence;
int price;
public:
    Creature();
    virtual ~Creature();
    virtual std::string getType()const=0;
    virtual double attackCreature(Creature &creature)=0;
    void showCreatureDetails()const;
    int getPowerAttack()const;
    int getPowerDefence()const;
    int getPrice()const;
    void setPowerAttack(int newAttack);
    void setPowerDefence(int newDefence);
    void setPrice(int newPrice);
};

在我的Attack(Hero&h)功能中,我想查找英雄的类型并进行一些所需的更改,但是当关键是对象时如何找到? 我如何使用std :: find并覆盖默认的比较功能,并通过std :: string m_userName查找我的对象?

我被困在这里:

void Hero::buyCreatures()
{
    cout<<"1. Buy Zombies."<<endl;
    cout<<"2. Buy Archers."<<endl;
    cout<<"3. Buy Vampire."<<endl;
    cout<<"4. Buy Wizard."<<endl;
    cout<<"5. Buy Black Dargon."<<endl;
    int choise,numberOfCreatures;
    auto it=creaturesMap.begin();
    cin >> choise;
    switch(choise)
    {
        case 1:
            cin >> numberOfCreatures;
            Zombie *zombie;
            zombie->showCreatureDetails();
            if(this->getGold()>=numberOfCreatures*(Zombie::zombiePrice)) {//if
                it = creaturesMap.find(// what i need to write here? )
.
.
.
.

一些想法? 感谢您的帮助

0 个答案:

没有答案