c++ - Is it possible to modify STL and make customized member functions? -
for 1 of projects need pop first element in vector. can use .erase() member function, , have been advised change container deque.
i thinking, naively, great add member function pop_front(). case, function few operations. first adds 1 head iterator (if there's private member of type iterator called "head") .begin() returns new beginning iterator, , subtract 1 size (if there's private member of type unsigned called "size"), , keep modifying other members affected. whenever call .pop_front(), these few operations, sounds efficient?
is can done or idea going result in big mess? if it's possible do, far bad side of can think of boundary problems happen when doing "vector in vector style", in project never show up.
i noticed .resize()'s complexity linear on number of elements inserted/erased if no reallocation happens. wondering why it's not constant? why doesn't .resize() reset ending iterator(if there's private member of type iterator named "end")?
of course it's possible wrong understanding how stl containers implemented..
thanks!
you have 2 unrelated questions in question, not good.
if want fiddle internals of std::vector - can copy source code of std::vector implementation, rename myvector (and move out of namespace std) , modify internals in whatever way want. many discoveries awaiting on path.
easier way make custom container around std::vector - either subclassing it, or better composition (see subclass/inherit standard containers?)
regarding resize() complexity - erased elements must destroyed (that is, destructors must called), inserted elements must constructed (constructors called), hence linear complexity in case of non-trivial destruction/construction respectively.
Comments
Post a Comment