【发布时间】:2011-06-26 19:48:19
【问题描述】:
我正在尝试编写一个模板函数,该函数将采用 STL 容器并显示其中元素的所有出现以及它们出现的数量。我打算使用地图,遍历容器并添加一个新元素(如果它不存在)或增加该元素的出现次数。
声明:
template < typename Container_t >
void findOccurrences (const Container_t& inContainer);
我的问题是:我能以某种方式获得容器所包含元素的类型说明符吗?
因此,当我创建地图时,键值将是inContainer 中的元素。
类似的东西:
map < typeid ( * inContainer.begin()), int > occurrences;
或者我是否必须将我的模板更改为这样的:
template < typename Container_t , typename Element_t >
void findOccurrences ( const Container_t & inContainer , Element_t dummy )
{
map < Element_t , int > occurrences;
}
谢谢
【问题讨论】: