What#39;s the difference between setlt;pairgt; and map in C++?(setlt;pairgt; 和 setlt;pairgt; 有什么区别?并用 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 的 iterator 和 const_iterator 是等价的.因此,用 set,您不能就地修改 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<pair> 和 set<pair> 有什么区别?并用 C++ 映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:set<pair> 和 set<pair> 有什么区别?并用 C++ 映射?
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
