给定cbegin(),cend(),为什么没有cfront(),cback(),cfind(),...?

Mar*_*utz 5 c++ stl c++14

所以,为了允许代码如

auto vect = ...;
auto it = vect.begin(), end = vect.end(); // want const_iterator, getting iterator
Run Code Online (Sandbox Code Playgroud)

挑右超载begin()end(),即使对于非const的容器中,更明确的cbegin()/ cend()添加功能.

为何停在那里?

关联容器具有find()相同问题的方法.序列容器有,front()并且back()同样存在同样的问题.

这些遗漏的显式const版本是遗漏还是设计?

Yak*_*ont 7

更广泛的API有成本,甚至只是在寻找您想要的功能时跳过它.

template<class T>
T const as_const(T&& t) noexcept(noexcept(T(std::declval<T>())) {
  return std::forward<T>(t);
}
template<class T>
T const& as_const(T& t) noexcept {
  return t;
}
Run Code Online (Sandbox Code Playgroud)

做你想要的大部分.它甚至会cbegin过时.

(根据以下@TC提供的n4380对上述代码进行了修改.代码不同,因为我认为n4380在这种T&&情况下略有错误.)