如果我打算使用任意类对象作为键,我可以使用 stl 映射吗?

发布于 2024-08-04 08:10:41 字数 293 浏览 3 评论 0原文

我是STL新手。使用地图来存储任意对象让我感到困惑的是:

std::map<MyClassObj, MyDataObject> MyMap;

我如何找到对象。例如,MyMap.find (MyClassObjInstance) 如何工作?我是否需要实现自己的迭代器并提供一些标准函数(其中包括一些等价函数)?任何例子将不胜感激。

是否有另一种方法可以使用标准库存储任意对象的关联列表?我已经在使用 stl 来维护平台的可移植性,并且不希望添加另一个库依赖项,例如 BOOST。

I'm new to STL. The thing stumping me about using a map to store arbitrary objects:

std::map<MyClassObj, MyDataObject> MyMap;

is how I find objects. How would MyMap.find (MyClassObjInstance) work for instance? Do I need to implement my own iterator and provide some standard functions which would include some equivalence function? Any examples would be appreciated.

Is there another method to store an associated list of arbitrary objects using standard libraries? I'm already using stl to maintain platform portability, and would prefer not to add another library dependency like BOOST.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦途 2024-08-11 08:10:41

std::map 在键和值之后有第三个模板参数,用于指示将使用哪个函数来比较键。默认情况下,它是 std::less,它又使用 operator<。因此,如果您的类有一个运算符<,那就没问题,否则您可以提供自己的比较器。

std::map has a third template argument, after key and value, to denote what function is going to be used to compare keys. By default, it is std::less, which in it's turn uses operator<. So if your class has an operator<, it's ok, else you can provide a comparator of your own.

风尘浪孓 2024-08-11 08:10:41

您只需为 MyClassObj 定义 operator< 即可。有关 std::map 的更多信息,您可以阅读此处

根据 C++ 标准 23.1.2:

短语“键的等价性”表示比较所强加的等价关系,而不是
键上的运算符==。也就是说,如果进行比较,两个键 k1 和 k2 被认为是等效的
对象 comp, comp(k1, k2) == false && comp(k2, k1) == false。

默认情况下,compstd::less

根据 C++ 标准 20.3.3:

模板  struct less :binary_function {
bool 运算符()(const T&x, const T&y) const;
};

// 运算符() 返回 x < y。

当然,您可以定义独立函子 comp 进行比较。

All of you need is to define operator< for MyClassObj. For more information about std::map you could read here.

According to C++ Standard 23.1.2:

The phrase ‘‘equivalence of keys’’ means the equivalence relation imposed by the comparison and not the
operator== on keys. That is, two keys k1 and k2 are considered to be equivalent if for the comparison
object comp, comp(k1, k2) == false && comp(k2, k1) == false.

By default comp is std::less.

According to C++ Standard 20.3.3:

template <class T> struct less : binary_function<T,T,bool> {
bool operator()(const T& x, const T& y) const;
};

// operator() returns x < y.

Surely, you could define stand alone functor comp for comparison.

影子是时光的心 2024-08-11 08:10:41

map 的完整类型是

template < class Key, class T, class Compare = less<Key>,
       class Allocator = allocator<pair<const Key,T> > > class map;

它默认使用 less,但只要你传入一个重载了operator()的类来获取对象的两个实例并返回一个bool,一切都很好。请注意,如果您给它 comp(a,b) 并且它返回 true,那么 a 在排序中应该位于 b 之前。

The full type for map is

template < class Key, class T, class Compare = less<Key>,
       class Allocator = allocator<pair<const Key,T> > > class map;

It uses less than by default but as long as you pass in a class that has operator () overloaded to take two instances of the object and returns a bool all is well. note if you give it comp(a,b) and it returns true, then a should come before b in the ordering.

倥絔 2024-08-11 08:10:41

是的,您可以使用自己的类型/对象作为键。他们必须实现小于运算符 (operator<),因为所有有序标准 C++ 容器都使用此运算符来测试排序和相等性。

Yes, you can use your own type/object as a key. They'll have to implement the less-than operator (operator<) as all ordered standard C++ containers do use this operator to test for ordering and equality.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文