Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL - Use member functions or functions in <algorithm>?

Tags:

c++

stl

I thought of this problem due to some of the answers I got to the following problem suggesting that I could use vector<T>::assign or copy with a back_insert_iterator here. My problem is, what are the drawbacks and advantages of using one method over another?

like image 993
nakiya Avatar asked Dec 22 '22 21:12

nakiya


1 Answers

assign overwrites the content of the vector where as copy with back_insert_iterator does a push_back on the vector thus preseving its content.

EDIT: If the question is generic (i.e. whether to use a member function defined in the container or an algorithm), I prefer to use the member function as it might have been optimized for the particular container compared to a generic algorithm.

like image 179
Naveen Avatar answered Dec 24 '22 10:12

Naveen