Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何检查变量或结构成员的存在?_C++_Variables_If Statement_Struct_Boolean - Fatal编程技术网

C++ 如何检查变量或结构成员的存在?

C++ 如何检查变量或结构成员的存在?,c++,variables,if-statement,struct,boolean,C++,Variables,If Statement,Struct,Boolean,假设我有很多布尔变量(我正在尝试制作基于文本的冒险游戏,我需要根据所选的选项来确定发散的路径),是否有一种简单的方法来检查给定字符串是否等于初始化变量的名称或初始化结构的成员?(例如,这样我就可以将变量从false改为true?用于与怪物战斗的快速示例 std::map<std::string, bool> flags; 查看标志以查看是否存在“键”。如果是,则将返回映射值(true或false)。如果它不存在,并且如果您熟悉Java,这是一个主要区别,那么将创建“key”并将其设

假设我有很多布尔变量(我正在尝试制作基于文本的冒险游戏,我需要根据所选的选项来确定发散的路径),是否有一种简单的方法来检查给定字符串是否等于初始化变量的名称或初始化结构的成员?(例如,这样我就可以将变量从false改为true?

用于与怪物战斗的快速示例

std::map<std::string, bool> flags;
查看
标志
以查看是否存在“键”。如果是,则将返回映射值(
true
false
)。如果它不存在,并且如果您熟悉Java,这是一个主要区别,那么将创建“key”并将其设置为默认值(
false


不。一旦程序被编译,所有这些好的变量名就消失了,被内存偏移替换。也许你想要一个<代码> STD::MAP<代码>类?还是<代码> STD::SET。不,C++没有这样的工作。如果您需要很多东西,您可以创建这些东西的集合,而不是每个东西的变量。如果需要用字符串查找事物,使用一个可由字符串索引的集合,例如STD::MAP。对不起,C++不提供运行时反射。幸运的是,这太棒了!非常好的解释和一个非常相关的例子了!非常感谢。
if (flags["key"])
#include <iostream>
#include <map>

void slaymonster(std::map<std::string, bool> & flags)
{
    //check if hero has sword of monster slaying
    if (flags["has sword of monster slaying"])
    { 
        flags["monster slain"] = true; // sets key "monster slain" to true so
                                       // hero can do stuff that requires
                                       // monster to have been slain
        std::cout << "Thou hast slain the monster!\n";
    }
    else
    {
        std::cout << "Thou hast been slain by the monster!\nInsert coin to continue.\n";
    }
}

int main()
{
    std::map<std::string, bool> flags;

    std::cout << "Try to slay monster before finding sword\n";
    slaymonster(flags);

    std::cout << "\nHero finds sword of monster slaying\n";
    flags["has sword of monster slaying"] = true;
    std::cout << "Try to slay monster after finding sword\n";
    slaymonster(flags);

    std::cout << "\nHero is mugged and loses sword of monster slaying\n";
    flags["has sword of monster slaying"] = false;
    std::cout << "Try to slay monster after losing sword\n";
    slaymonster(flags);
}
Try to slay monster before finding sword
Thou hast been slain by the monster!
Insert coin to continue.

Hero finds sword of monster slaying
Try to slay monster after finding sword
Thou hast slain the monster!

Hero is mugged and loses sword of monster slaying
Try to slay monster after losing sword
Thou hast been slain by the monster!
Insert coin to continue.