检查stl容器中元素的类型 - C++

2022-01-24 00:00:00 containers types c++ stl

如何获取 STL 容器所包含的元素的类型?

how can i get the type of the elements that are held by a STL container?

推荐答案

对于一般容器,它将是 X::value_type.对于关联容器,它将是 X::mapped_type(X::value_type 对应于 pair).符合 C++ 标准第 23 章.

For containers in general it will be X::value_type. For associative containers it will be X::mapped_type (X::value_type corresponds to pair<const Key,T>). It is according to Chapter 23 of C++ Standard.

要检查类型是否相等,您可以使用 boost::is_same.从 C++11 开始 ― std::is_same.

To check that types are equal you could use boost::is_same. And since C++11 ― std::is_same.

相关文章