c++ - 如何编写一个适用于一对映射和向量的 C++ 模板>
问题描述
我想编写一个模板函数,它遍历一个容器std::pair
并返回一个模板化的值,其中包含这两种类型。我已经让这个工作std::map
如下:
template <typename T1, typename T2>
std::pair<std::vector<T1>, std::vector<T2>> unzip(const std::map<T1,T2>& zipped)
{
auto unzipped = std::make_pair(std::vector<T1>(), std::vector<T2>());
for (auto& one_two : zipped)
{
unzipped.first.push_back(one_two.first);
unzipped.second.push_back(one_two.second);
}
return unzipped;
}
这工作正常,但它将容器限制为std::map
. 我想要完成的是让它也适用于类似的东西std::vector<std::pair<T1,T2>>
,因为两个容器的迭代工作方式相同。
我试图通过更改模板参数使容器成为模板:
template <typename T1, typename T2, template<typename ... Types> class Container>
std::pair<std::vector<T1>, std::vector<T2>> unzip(const Container<T1,T2>& zipped)
{
//do stuff and return
}
但在这种情况下,如果不使用,则扣除失败,std::map
因为std::vector
仅依赖于单一类型。然后我尝试变得更有创意,但编译器抱怨得更多:
template <typename PairContainerType>
std::pair<std::vector<typename PairContainerType::value_type::first_type>,
std::vector<typename PairContainerType::value_type::second_type>>
unzip(const PairContainerType& zipped)
{
typedef typename PairContainerType::value_type::first_type T1;
typedef typename PairContainerType::value_type::second_type T2;
//do the same stuff and return
}
我认为我正在尝试做的事情应该是可能的,但我不知所措。如果这很重要,我正在使用c++11
,但如果我想要的东西在未来的版本中可用,我仍然对这些解决方案感兴趣。谢谢你。
更新:感谢 RiaD,我得到了以下使用 c++11 的工作:
template <typename PairContainerType,
typename T1 = typename std::remove_const<typename PairContainerType::value_type::first_type>::type,
typename T2 = typename std::remove_const<typename PairContainerType::value_type::second_type>::type>
std::pair<std::vector<T1>, std::vector<T2>> unzip(const PairContainerType& zipped)
{
//do stuff and return
}
请注意,它与接受的答案略有不同,因为它使用std::remove_const
而不是std::remove_const_t
并且需要::type
在最后添加。
RiaD 还指出,模板类型T1
可以T2
被调用者覆盖。正如 Jarod42 所建议的,这可以通过一些额外的输入来缓解,这让我进入了最终的 c++11 解决方案:
template <typename PairContainerType>
std::pair<std::vector<typename std::remove_const<typename PairContainerType::value_type::first_type>::type>,
std::vector<typename std::remove_const<typename PairContainerType::value_type::second_type>::type>>
unzip(const PairContainerType& zipped)
{
auto unzipped = std::make_pair(
std::vector<typename std::remove_const<typename PairContainerType::value_type::first_type>::type>(),
std::vector<typename std::remove_const<typename PairContainerType::value_type::second_type>::type>());
for (const auto& one_two : zipped)
{
unzipped.first.push_back(one_two.first);
unzipped.second.push_back(one_two.second);
}
return unzipped;
}
总的来说,c++14 和 c++17 看起来很吸引人。我本可以通过auto
返回功能节省自己的时间!
解决方案
通常使用 c++ 你只允许任何类型,如果它不正确就让它失败。要获得 T1 和 T2,您可能会获得 value_type of collection(同时存在于std::map
andstd::vector<std::pair<>>
和 others 中,例如std::set<std::pair<>>
)
template <typename T, typename T1 = std::remove_const_t<typename T::value_type::first_type>, typename T2 = std::remove_const_t<typename T ::value_type::second_type>>
std::pair<std::vector<T1>, std::vector<T2>> unzip(const T& zipped)
{
auto unzipped = std::make_pair(std::vector<T1>(), std::vector<T2>());
for (auto& one_two : zipped)
{
unzipped.first.push_back(one_two.first);
unzipped.second.push_back(one_two.second);
}
return unzipped;
}
它有(轻微的)缺点,有人可能会根据需要强制执行 T1 和 T2。您可以将它们从模板参数列表中删除。
template <typename T>
auto /* or return type (but it will require copy-pasting) before c++14*/
unzip(const T& zipped)
{
using T1 = std::remove_const_t<typename T::value_type::first_type>; //may need to remove const
using T2 = std::remove_const_t<typename T::value_type::second_type>;
// impl
}
需要删除 const 因为 value_type of std::map
is std::pair<const K, V>
。std::remove_const_t
如果您需要较旧的标准支持,例如对于您需要的 c++11,它可以在没有的情况下完成typename std::remove_const<>::type
推荐阅读
- flutter - 如何允许用户为他们正在添加的内容选择图标?
- node.js - 错误 [ERR_HTTP_HEADERS_SENT]:在将标头发送到客户端后无法设置标头 - Express + Request
- nativescript - 更改状态栏字体颜色
- java - 如何在android studio中获取微调器值并将其传递给文本视图
- python - 如何使用 Tensorflow 导出简单保存的模型图以进行图像分类?
- ruby - 如何使用 yajl-ruby 解析和过滤大 json 文件中的数据(2G 大小)
- openshift - 在 OpenShift OKD 中强制使用 DNS
- python - 如何使用python从x,y,z导出到geotiff
- git - 无法在 Windows 上使用 PyCharm 终端重置 git 凭据
- android - 使用 S9 设备解析服务器 I/O 失败消息,与其他设备合作