Prefer member functions to algorithms with the same names

来源:互联网 发布:品茗BIM软件 编辑:程序博客网 时间:2024/04/30 22:19

有些容器拥有和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.

原创粉丝点击