STL - 在 <algorithm> 中使用成员函数或函数? [英] STL - Use member functions or functions in <algorithm>?
问题描述
我想到了这个问题,因为我对以下问题的一些答案表明我可以使用 vector<T>::assign
或 copy
back_insert_iterator
此处.我的问题是,使用一种方法相对于另一种方法有哪些优缺点?
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?
推荐答案
assign
覆盖 vector
的内容,其中 copy
用 back_insert_iterator
对向量执行 push_back
从而保留其内容.
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:如果问题是通用的(即是使用容器中定义的成员函数还是算法),我更喜欢使用成员函数,因为它可能已针对特定问题进行了优化容器与通用算法相比.
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.
这篇关于STL - 在 <algorithm> 中使用成员函数或函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!