简体   繁体   中英

Is std::map<std::set<long>, double> AND std:map< std::pair<long, long>, double> a valid data type in C++?

I know that in std::map we have a key and a corresponding mapped value.

Now in the Data Type that i mentioned, key would be of type std::set<long> or std::pair<long, long> . So, is that valid keeping in mind that in map values are stored on the bases on sorted value of keys. So, is std::map<std::set<long>, double> AND std:map< std::pair<long, long>, double> valid ?

std::set has an operator< which performs a lexicographical comparison between two sets of the same type. So yes, an std::set<T> can be a valid key for a map.

An std::pair<T1,T2> also has an operator< implementing a lexicographical comparison, so std::map<std::pair<T1, T2>, T3> would also be valid iff both T1 and T2 have a less-than comparison operator< implementing strict weak ordering. So the requirements are tighter. Both T1 and T2 need the comparison to be keys of a map, but they do not need it to form a valid pair. So a valid pair does not necessarily form a valid key for a map. On the other hand, you can instantiate the map with your own comparison criteria.

std::map<std::pair<T1, T2>, T3, Comp> m; 

No, the syntax is wrong. What you want is std::map<std::set<a_type_here>, double> , so add the template parameter for set.

Note a_type_here should have operator< or you need to use std::set<a_type_here, compare_function>

What you seem to want is: std::map<std::pair<long, long>, double> mapping

Example of how to use; mapping[std::make_pair(1,2)] = 0.1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM