【发布时间】:2013-12-18 12:18:15
【问题描述】:
我正在尝试编写一个简单的通用函数来迭代容器元素。每个元素都被转换为std::string(无论如何)并存储在另一个地方。基本版本很简单:
template<class Container>
void ContainerWork(const Container& c)
{
for(const auto& elem : c) {
/* convert to string and store*/
}
}
然后有必要为具有值类型std::string 的容器添加专门化,并将代码转换为:
template<typename T, template<typename, typename> class Container, class Allocator>
void ContainerWork(Container<T, Allocator> c)
{
for(const T& elem : c) {
/* convert to string and store*/
}
}
template<template<typename, typename> class Container, class Allocator>
void ContainerWork(Container<std::string, Allocator> c)
{
for(const std::string& elem : c) {
/* frame elem in quotes*/
}
}
效果很好,但现在我只能使用已排序的容器(vector、list 等),但我还想使用 set 和 unordered_set。任何想法如何在没有 4 个参数的容器的“复制粘贴”实现的情况下做到这一点?我正在尝试与decltype(Container)::value_type 一起玩,但没有运气。
我可能会使用 c++11 的大部分功能(编译器 - VS2012 或 GCC 4.8.x)
【问题讨论】: