multiple keys for a map in C++

情到浓时终转凉″ 提交于 2019-12-22 10:24:45

问题


I have a table where the entries are something like this

Row - Column1 - Column2 - Column3 Column4

 1     0X0A      1          2      A
 2     0X0B      2          2      B
 3     0x0C      3          2      C

Now i want to use map in such that i can use column 1 or Column 2 as the key to get the row. What kind of map i should use to achieve this?

(Note- Table is just for explanation and not the exact requirement) I thought of using multimap, but that is not going to solve the prob


回答1:


try multi-index containers from boost.




回答2:


Define a class similar to pair with a custom comparator that indicates equality if either the first member or the second member match, but not necessarily both. You could then use that class as your key type. You would probably need a particular value for each member that will never be used in your data, to use as default values in your constructor, to avoid keys where only the first member has been initialized occasionally matching on the second member due to leftover data.




回答3:


You could use one map to map from column 1 to the row and another to map from column 2 to the row. Repeat for as many columns as needed



来源:https://stackoverflow.com/questions/11087068/multiple-keys-for-a-map-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!