使用const变量作为键时,std :: map初始化不起作用

时间:2020-01-09 16:10:25

标签: c++ stdmap

我有一个这样初始化的全局变量:

main.cpp

map<const string, vector<string>> cmdOutputMap({ {cmd1, output1},
                                                 {cmd2, output2},
                                                 {cmd3, output3} });

commands.h

extern const std::string cmd1;
extern const std::string cmd2;
extern const std::string cmd3;

commands.cpp

std::string cmd1 = "-text1";
std::string cmd2 = "-text2";
std::string cmd3 = "-text3";

它可以很好地进行编译,但是,在调试中,cmdOutputMap的大小为1,唯一的键是“”。

如果我不是使用变量cmd1,cmd2,cmd3,而是使用它们的值(请参见下文),则cmdOutputMap的大小为3,并填充了右键。

map<const string, vector<string>> cmdOutputMap({ {"-text1", output1},
                                                 {"-text2", output2},
                                                 {"-text3", output3} });

用变量初始化std :: map是否错误?

谢谢

弗雷德

0 个答案:

没有答案
相关问题