为什么 std::for_each 或类似形式的任何其他 std::algorithm 函数没有重载,例如:
template<class Container, class UnaryFunction>
UnaryFunction for_each(Container c, UnaryFunction f);
这基本上就可以了:
template<class Contianer, class UnaryFunction>
UnaryFunction for_each(Container c, UnaryFunction f){
return std::for_each(c.begin(), c.end(), f);
}
在我看来,我经常在容器的整个范围上使用这些类型的函数,并且键入似乎不方便,尤其是对于长容器名称:
std::for_each(myLongContainerNameThatIsLong.begin(), myLongContainerNameThatIsLong.end(), f);
我是否遗漏了任何细节来阻止这种情况,或者是否有某种机制来获取我想要的签名?
最佳答案
不,没有根本原因你不能这样做,事实上,从 C++20 开始,你可以使用 std::ranges::for_each 来做到这一点。 :
std::ranges::for_each(myLongContainerNameThatIsLong, f);
关于c++ - 整个范围内的 std::for_each 没有方便的重载吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65163406/