Is it normal practice or very bad to nest maps several levels?
// name property // name singal // interval of Ranges // values
typedef std::map<std::string, std::map<std::string, std::map<RangeSignalValue, std::string > > > sgHashSi开发者_运维知识库gnals;
I've certainly done this. However, I'd break down the definition into digestible chunks:
typedef std::map<RangeSignalValue, std::string> RangeIntervalMap;
typedef std::map<std::string, RangeIntervalMap> NameSignalMap;
typedef std::map<std::string, NameSignalMap> NamePropertyMap;
If it makes sense in the context you're working in then I don't see any problems, other than it might be hard to read. Might it be possible to use typedef
to make some of these nested maps easier to understand?
For instance:
typedef std::map<RangeSignalValue, std::string> RangesValues;
typedef std::map<std::string, std::map<std::string, RangesValues > > sgHashSignals;
The biggest issue is that lookup is slightly slower than the alternative:
// property+signal, concatenated // interval of Ranges // values
typedef std::map<std::string, std::map<RangeSignalValue, std::string > > sgHashSignals;
Precisely how you should concatenate the first two strings depends on their syntax, though.
This replaces two string lookups by one. The downside is that it takes more memory.
精彩评论