对于std::map<K, V>默认值value_type是std::pair<const K, V>。有没有使用自定义的方法value_type?据我所知你做不到。
编辑:要清楚,自定义value_type可能是这样的:
struct Edge {
K from;
V to;
int calculate_thing();
void print_debug();
};
Run Code Online (Sandbox Code Playgroud)
例如,假设我有一些不想更改的现有功能:
template<typename It>
void processEdges(It begin, It end) {
for(auto it = begin; it != end; ++it) {
do_stuff(it->from);
do_more_stuff(it->calculate_thing());
}
}
Run Code Online (Sandbox Code Playgroud)
总是这样std::pair<const K, V>,您无法更改。
如果您需要一个custom value_type,则可以使用std::set(最好与透明的comaprator一起使用)。