问题
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?
回答1:
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.
回答2:
Complementing Naveen's answer, using std::copy()
is also much more versatile, as this way you can easily write to any output iterator. This could be a stream or something entirely custom.
回答3:
In the general case, prefer member functions to functionally-equivalent algorithms. Scott Meyers discusses this in depth in Effective STL.
回答4:
In essence, they are the same. Vector's reallocation behavior (defined in terms of how it behaves with push_back) avoids too many reallocations by progressively using more memory.
If you need identical code to work with multiple container types (i.e. you're writing a template), including those containers not in the stdlib, then generally prefer free functions.
If you don't mind rewriting this code if/when container types change, then you can prematurely optimize if you like, or simply use whichever is more convenient.
And just for completeness' sake, copy+back_inserter is equivalent to vector::insert at the end(), while vector::clear + copy+back_inserter is equivalent to vector::assign.
回答5:
Your question can be generalized as follows:
When dealing with STL containers, should I prefer to use member functions or free-standing functions from
<algorithm>
when there are functional equivalents?
Ask 10 programmers, and you'll get 12 responses. But they fall in to 2 main camps:
1) Prefer member functions. They are custom-designed for the container in question and more efficient than the <algorithm>
equivalent.
2) Prefer free-standing functions. They are more generic and their use is more easily maintained.
Ultimately, you have to decide for yourself. Any conclusion you come to after giving it some reasoned, researched thought is better than following anyone else's advice blindly.
But if you just want to blindly follow someone's advice, here's mine: prefer the free-standing functions. Yes, they can be slower than the member functions. And "slow" is such a dirty word. But 9 times out 10 you just don't (or shouldn't) care how much more efficient one method is than the other. Most of the time when you need a collection, you're going to periodically add a few elements, do something, and then be done. Sometimes you need ultra-high performance lookup, insertion or removal, but not normally. So if you're going to come down one one side or the other with a "Prefer Method X" approach, it should be geared for the general case. And the approach that prefers the member methods seems to be slanted towards optimization -- and I call that a premature micro-optimization.
来源:https://stackoverflow.com/questions/4152815/stl-use-member-functions-or-functions-in-algorithm