c++ - Use nullptr instead of end iterator -
i know if possible use nullptr or kind of generic end iterator.
for example:
// a.b->cdef().g->bdf() std::map<int, int> std::unordered_map<int, int> copy(a.b->cdef().g->bdf().begin(), a.b->cdef().g->bdf().end());
is write specify begin , end initialize new map.
why need specify end tag if want copy whole map.
i prefer like:
std::unordered_map<int, int> copy(a.b->cdef().g->bdf().begin(), nullptr);
or
std::unordered_map<int, int> copy(a.b->cdef().g->bdf().begin());
or
std::unordered_map<int, int> copy(a.b->cdef().g->bdf().begin(), std::unordered_map::end);
edit: changed example std::list<int> std::unordered_map<int, int>. there no copy constructor std::map std::unordered_map.
to answer question asked, "is possible use nullptr instead of end()?"
no.
please think how containers , iterators implemented. container knows begin , end iterators. iterator knows how increment, decrement , compare iterator.
so take vector example. how begin iterator know stop incrementing? how iter == nullptr
know when return true stop?
or take circular linked list. has no real end. stops when iterator equals begin iterator again. nullptr mean end there?
if think implementation you'll see why answer "no."
Comments
Post a Comment