-1

I am writing a parser, and I decided to save type information in a std::map. When I use the [] operator to access it, I always get the 0 value for the enum. The map is declared as such:

enum type {Bool, Bool_a, Int_4, Inta_4, Int_8, Inta_8, Float_s, Floata_s, Float_d, Floata_d, Ch_s, Ch_a, Str, Invalid};  

class  kparse_ret{
...  
    std::map<std::string, type  
...  
} ret_data;

And is then set using something like

ret_data.type_list[itemname] = Int_4;  

(Where itemname is a std::string)
The problem I'm having is that when I use

ret_data.type_list[data_name]  

I always get Bool, or 0. (again, data_name is a std::string). I know that itemname and data_name have exactly the same contents when their respective contexts are reached. Furthermore, if I use itemname again to access it, I get the value I just set it to.

4

1 回答 1

0

当比较字符串作为映射的键时,不仅会比较字符串的内容,还会比较字符串的大小。

以下将帮助您识别问题:

std::map<std::string, std::string>

std::string key = "KEY";
std::string key2 = "KEY";

key2.resize(100);

现在,如果您在“KEY”上映射某些内容并使用 key2 访问它,您将无法获得您期望的值。

于 2013-07-24T13:36:25.923 回答