哪些 C++20 标准库容器具有 .at 成员访问函数?

Which C++20 standard library containers have a .at member access function?

哪些 C++20 标准库容器具有 .at 函数?例如,至少 std::mapstd::unordered_mapstd::vector 可以。还有哪些?

有什么方法可以解决这个问题而无需手动查看 2000 页标准?

我结束了 grepping libstdc++ 包含 \bat( 的目录,这给了我:

std::basic_string
std::basic_string_view
std::array
std::vector
std::vector<bool>
std::unordered_map
std::map
std::dequeue

我不确定这是否详尽无遗。

还有ropevstring,但我不认为这些是标准的。

从评论来看,您似乎想要这样的东西:

template <typename Container, typename T>
typename Container::pointer my_at(Container&, const T&)
requires (requires(Container c, const T& key) { c.at(key); })
{
   // ...
}

Demo

我认为指针不是适合此操作的 return 类型,它应该是迭代器。此时定义分为两种情况:

随机访问容器:

iterator at_(size_type index) noexcept { return index < size() ? begin() + index : end(); }
const_iterator at_(size_type index) const noexcept { return index < size() ? begin() + index : end(); }

mapunordered_map:

template<typename T>
iterator at_(T && key) noexcept { return find(std::forward<T>(key)); }
template<typename T>
const_iterator at_(T && key) const noexcept { return find(std::forward<T>(key)); }

然后不是针对 nullptr 进行测试,而是针对容器的 end().

进行测试

关于 (unordered_)map 是否需要现有成员的别名以匹配您的命名法,这也是一个合理的问题