c++ - Ordered associative containers of pointers -
this question has answer here:
is okay practice define ordered associative containers of pointers (either built-in or class-type ones) despite fact that, far can tell standard, relational operators on (non-array objects et al.) pointers undefined?
from 5.9 of c++11 standard:
- if 2 pointers p , q of same type point different objects not members of same object or elements of same array or different functions, or if 1 of them null, results of
p<q,p>q,p<=q, ,p>=qunspecified.
since ordered containers (map, set, multimap , multiset) use < operator default provide total order compiles fine create set of pointers. again if relational operators pointers unspecified p<q , p>q both test false or true meaning may not idea ordering of set. would've accepted bad idea if wasn't fact example in book (c++ primer 5th ed chapter 13.4) defines set of pointers class type folder*, i.e. set<folder*> seemingly no comment on concern.
the ordered associative containers not use operator< directly, use std::less, defined , establishes total ordering pointer types, when operator< isn't.
so, fine instantiate std::set or std::map pointer types keys.
Comments
Post a Comment