本文介绍了set< pair>和地图在C + +?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种方法可以轻松地创建一个键,C ++ STL中的值归属:映射和对的集合。例如,我可能有

There are two ways in which I can easily make a key,value attribution in C++ STL: maps and sets of pairs. For instance, I might have

map<key_class,value_class>

set<pair<key_class,value_class> >

在算法复杂性和编码风格方面,这些用法之间有什么区别?

In terms of algorithm complexity and coding style, what are the differences between these usages?

推荐答案

设置元素在集合中时无法修改。 set 迭代器 const_iterator 是等效的。因此,用 set< pair< key_class,value_class> > ,您不能原地修改 value_class 。您必须从集合中删除旧值,并添加新值。但是,如果 value_class 是一个指针,这不会阻止您修改它指向的对象。

Set elements cannot be modified while they are in the set. set's iterator and const_iterator are equivalent. Therefore, with set<pair<key_class,value_class> >, you cannot modify the value_class in-place. You must remove the old value from the set and add the new value. However, if value_class is a pointer, this doesn't prevent you from modifying the object it points to.

使用 map< key_class,value_class> ,您可以修改 value_class 参考地图。

With map<key_class,value_class>, you can modify the value_class in-place, assuming you have a non-const reference to the map.

这篇关于set&lt; pair&gt;和地图在C + +?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 10:19
查看更多