Struct vs. array for tiered data organization

I have some data that I need to organize. I should note that I am new to C and C ++.

I have a list of buildings, each of which has the quantity and quantity of resources associated with it. I need to go through the list of buildings, multiply the amount of each building by its difference in resources and add them together to get the total spread of resources.

For instance:

Ice Well: [-100 power, +50 water], n = 2

Solar Matrix: [+150 power], n = 2

Total: [+ 100 units, +100 water]

I am confused how to organize my data. If I use a structure, it will not be easy for you to scroll, but the array will be confusing as the number of building types increases. At this point, it would be best to associate the enumeration with an array in order to get the structure naming functionality with the cyclic array functionality. How should I do it?

+4
source share
3 answers

You might want to consider this approach: (live example: http://ideone.com/xvYFXp )

#include <iostream>
#include <vector>
#include <memory>

class Building {
  public:
    virtual int getPower() = 0;
    virtual int getWater() = 0;
    virtual ~Building() {}
};

class IceWell : public Building {
  public:
    virtual int getPower() {return -100;}
    virtual int getWater() {return 50;}
};

class SolarArray : public Building {
  public:
    virtual int getPower() { return 150; }
    virtual int getWater() { return 0; }
};

int main()
{
    std::vector<std::shared_ptr<Building>> inventory;
    inventory.emplace_back(new IceWell);
    inventory.emplace_back(new SolarArray);
    inventory.emplace_back(new IceWell);
    inventory.emplace_back(new SolarArray);

    int total_power = 0;
    int total_water = 0;
    for (auto building : inventory)
    {
        total_power += building->getPower();
        total_water += building->getWater();
    }

    std::cout << "Total power: " << total_power << "\n";
    std::cout << "Total water: " << total_water << "\n";

    return 0;
}

Building ; , , . IceWell SolarArray , , . , , ( !). .

/ , () - . getWater() , "" .


(. ), . , , , . , .


++ " ". , .

++, . ...

, , . , - . "" . , :

struct ResourceFootprint {
    int power;
    int water;
};

, :

( : http://ideone.com/ubJv8m)

int main()
{
    ResourceFootprint ice_well = {-100, +50};
    ResourceFootprint solar_array = {+150, 0};

    std::vector<ResourceFootprint> buildings;
    buildings.push_back(ice_well);
    buildings.push_back(ice_well);
    buildings.push_back(solar_array);
    buildings.push_back(solar_array);

    ResourceFootprint total = {0, 0};
    for (const ResourceFootprint& r : buildings)
    {
        total.power += r.power;
        total.water += r.water;
    }

    std::cout << "P: " << total.power << ", W: " << total.water << "\n";

    return 0;
}

; , , , . , , , - :

IceWell,-100,50
SolarArray,150,0
HydroElectricPlant,150,150

, , ResourceFootprint. ; , , , , , , . , , , .

; MedicalWaste. , , , , , .

, . ; :

struct ResourceFootprint {
    int power;
    int water;
    int oil;
    int food;
    int medicine;
    int medical_waste;
};

, , ( , , ), , , :

    ResourceFootprint ice_well = {-100, +50, 0, 0, 0, 0};
    ResourceFootprint solar_array = {+150, 0, 0, 0, 0, 0};
    ResourceFootprint hospital = {-100, -50, 0, -50, -50, 50};
    ResourceFootprint gas_station = {-10, 0, -100, 0, 0, 0};
    ResourceFootprint restaurant = {-20, -20, 0, -100, 0, 0};
    ResourceFootprint farm = {-10, -30, -10, 200, 0, 0};

, , , . :

( : http://ideone.com/rukqaz)

    ResourceFootprint total = {0, 0, 0, 0, 0, 0};
    for (const ResourceFootprint& r : buildings)
    {
        total.power += r.power;
        total.water += r.water;
        total.oil += r.oil;
        total.food += r.food;
        total.medicine += r.medicine;
        total.medical_waste += r.medical_waste;
    }

. ?

, , - . , , , , IceWell SolarArray , . , - , .

, , -, . , , , . , ; . , .

:

class Building {
    public:
        virtual ~Building() {}

        virtual ResourceFootprint currentResourceLevel() = 0;
};

ResourceFootprint ( ), . ...

. ResourceFootprint - (, const) :

class HydroElectricPlant : public Building {
    public:
        HydroElectricPlant(const ResourceFootprint& r)
         : resources(r) {}

        virtual ResourceFootprint currentResourceLevel() { return resources; }

    private:
        const ResourceFootprint resources;
};

IceWell - :

class IceWell : public Building {
    public:
        IceWell(const ResourceFootprint& initial)
         : resources(initial) {}

        virtual ResourceFootprint currentResourceLevel() { return resources; }

        void useWater(int amount) { resources.water -= amount; }

    private:
        ResourceFootprint resources;

};

SolarArray :

class SolarArray : public Building {
    public:
        SolarArray(const ResourceFootprint& r)
         : day_resources(r), night_resources(r)
        {
            night_resources.power = 0;
        }

        virtual ResourceFootprint currentResourceLevel()
        {
            if (is_day())
            {
                return day_resources;
            }
            else
            {
                return night_resources;
            }
        }

    private:
        ResourceFootprint day_resources;
        ResourceFootprint night_resources;

};

:

  • : , ( ).
  • : , Building, , .

?

  • . , ResourceFootprint, , ResourceFootprint . , , , .
  • . , . std::vector, (, res [0] - , res [1] - ) , , .
  • , !

, , , , , ++. , , , , , , . . , , .

+8

, : , . , 2.0, , .. , .

, ? , . , , .

, , , - :

class Building {  //might be abstract
    public:
        virtual int getResourceDiff(std::string resource);  //might be pure virtual
};

-

//the header
class SomeGameWorldClass {
    private:
        std::unordered_map<std::string, int> resources;
        std::vector<std::shared_ptr<Building> > buildings;
    public:
        void updateResources();
};

//the .cpp file
void SomeGameWorldClass::updateResources() {
    for(resourcePair : resources) {
        for(buildingPtr : buildings) {
            resourcePair.second += buildingPtr->getResourceDiff(resourcePair.first);
        }
    }
}

, . Heck, Building -subclass, , .

, string/integer - . , , , , .. , , , , :

class Resource {
    public:
        ...
    private:
        int curAmount, maxAmount;
        std::string name;
        ...
};
+2

, :

struct Building
{
    Building(std::string n, int p, int w) : name(n), power(p), water(w) {}
    std::string name;
    int power;
    int water;
};

int main()
{
    Building iceWell("Ice Well", -100, 50);
    Building solar("Solar Array", 150, 0);
    Building buildings[4] = { iceWell, iceWell, solar, solar };
    int totalPower = 0;
    int totalWater = 0;
    for (int i = 0; i < 4; ++i)
    {
        totalPower += buildings[i].power;
        totalWater += buildings[i].water;
    }
}

, , std::vector , , .

+1

All Articles