您的位置:首页 > Web前端

Prefer member functions to algorithms with the same names

2006-01-11 15:56 302 查看
有些容器拥有和STL算法同名的成员函数。关联容器提供了count、find、lower_bound、upper_bound和equal_range,而list提供了remove、remove_if、unique、sort、merge和reverse。大多数情况下,你应该用成员函数代替算法。这样做有两个理由。首先,成员函数更快。其次,比起算法来,它们与容器结合得更好(尤其是关联容器)。那是因为同名的算法和成员函数通常并不是是一样的。
Some containers have member functions with the same names as STL algorithms. The associative containers offer count, find, lower_bound. upper_bound, and equal_range, while list offers remove, remove_if, unique, sort, merge, and reverse. Most of the time, you'll want to use the member functions instead of the algorithms. There are two reasons for this. First, the member functions are faster. Second, they integrate better with the containers (especially the associative containers) than do the algorithms. That's because algorithms and member functions that share the same name typically do not do exactly the same thing.
Some containers have member functions which have the same name with STL algorithms. Associated containers provide count, find, lower_bound. upper_bound, and equal_range, while list provides remove, remove_if, unique, sort, merge, and reverse. In most cases, you should use member functions instead of algorithms. There are two reasons for doing this. For one thing, member functions are faster. For another, comparing to algorithms, they are better-combined with contianers(especially associated containers). Algorithms and member functions with the same name are usually not the same.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐