c++ - Containers in C++11 STL -
struct solution { double power_peak; valarray<int> assignment; }; list<solution> list; list.pop_back(); list<solutions *> list2; list2.pop_back(); function(list2);
hello still don't understand how these containers work respect content. assume filled before. when call pop_back on first list. assume gets destroyed (including int's in valarray) when call pop_back on second list. content still live.
when function call. list behave pointer array? pointer copied still point same content.
is correct?
would vector or valarray behave other in terms of copying?
when pop_back()
element list
, or of sequence containers support pop_back()
, always result in destruction of last element. difference in second case list
contains pointers, , destructor of pointer nop. that's why content pointed pointer still lives on.
this reason sticking raw pointers containers frowned upon. means you'll have manage memory manually, can error prone. using list<unique_ptr<t>>
, or other smart pointer, means no longer need manual memory management.
when function call. list behave pointer array? pointer copied still point same content.
it depends on how function defined. if 1 of following:
return_type function(list_type&); return_type function(list_type const&);
no copy made. reference list passed function. on other hand, if defined as:
return_type function(list_type);
then list copied.
Comments
Post a Comment