使用sets作为使用类初始化的属性

时间:2014-05-15 17:33:07

标签: c++ set

我一直在尝试将集合用作类的属性。在初始化类时,需要初始化这些集合。我一直在尝试使用预先初始化的数组,然后将集合的值设置为数组的内容。但是,我的问题是,当声明set时,你似乎只能将一个set的内容设置为数组的内容,如果它是一个class属性,我就不能这样做。我宁愿不单独添加每一组。有什么建议?这是一些示例代码: Creature.cpp:

        Creature :: Creature()
{
    Skill Init[] = {Balance,EscapeArtist,Hide,MoveSilently,OpenLock,Ride,SleightOfHand,Tumble,UseRope};
    //DexSkills = ??? (contents of init)
    Skill Init[] = {Climb,Jump,Swim};
    //StrSkills = ???
    Skill Init[] = {Concentration};
    //ConSkills = ???
    Skill Init[] = {Appraise,Craft,DecipherScript,DisableDevice,Forgery,Knowledge,Psicraft,Search,Spellcraft};
    //IntSkills = ???
    Skill Init[] = {Autohypnosis,ControlShape,Heal,Listen,Profession,SenseMotive,Spot,Survival};
    //WisSkills  = ???
    Skill Init[] = {Bluff,Diplomacy,Disguise,GatherInformation,HandleAnimal,Intimidate,Perform,UseMagicDevice,UsePsionicDevice};
    //ChaSkills = ???
}

并在Creature.h中:

#pragma once
#define roll20 (rand()%20) + 1
#define Mod(stat) ((stat-10)/2)
#include <vector>
#include <set>
#include "Global.h"


class Creature
{
protected:
std::set<Skill> DexSkills;
std::set<Skill> StrSkills;
std::set<Skill> ConSkills;
std::set<Skill> IntSkills;
std::set<Skill> WisSkills;
std::set<Skill> ChaSkills;
//static const Skill StrSkills[3];
//static const Skill ConSkills[1];
//static const Skill IntSkills[9];
//static const Skill WisSkills[8];
//static const Skill ChaSkills[9];
const int maxHP;
int HP;
int hitDie;
int speed;
int babBase;
int fortBase;
int refBase; 
int willBase;
int AC;
int STR;
int CON;
int DEX;
int INT;
int WIS;
int CHA;
DamageResist DR;
std::vector<Feat> Feats;
Alignment alignment;
//std::set<DamageType> Weaknesses;
//std::set<DamageType> Resistances;
std::set<DamageType> Immunities;
int Resistances [15];
int SkillRanks[40];

public:
//Creature();
int Damage(int damage, DamageType type);
void getDamaged(int damage);
int StatRoll(int stat);
//int StrRoll();
//int ConRoll();
//int DexRoll();
//int IntRoll();
//int WisRoll();
//int ChaRoll();
int SkillCheck(Skill skill);

};

在我的global.h中:

typedef enum {
Appraise,Autohypnosis,Balance,Bluff,Climb,Concentration,ControlShape,Craft,DecipherScript,Diplomacy,DisableDevice,Disguise,EscapeArtist,Forgery,
GatherInformation,HandleAnimal,Heal,Hide,Intimidate,Jump,Knowledge,Listen,MoveSilently,OpenLock,Perform,Profession,Psicraft,Ride,Search,SenseMotive,
SleightOfHand,SpeakLanguage,Spellcraft,Spot,Survival,Swim,Tumble,UseMagicDevice,UsePsionicDevice,UseRope
} Skill;

1 个答案:

答案 0 :(得分:0)

如果您正在寻找一种将C数组复制到集合中的简单方法,您可以尝试这样的方法:

#include <iterator>

...

int a[] = { 1, 2, 3, 4, 5, 6 };
int n = sizeof(a) / sizeof(a[0]);

std::set<int> s;
std::copy(a, a + n, std::inserter(s, s.end()));