c++ - 采用容器类型和比较类型的函数模板
问题描述
下面是一些我想要的代码:
- 拿一个容器
- 打个类型
- 返回一个相同类型的新容器,其中包含原始容器中与比较类型的类型不匹配的对象(ptrs)
#include <typeinfo>
// Algorithm wrapper around find_if that takes an STL collection and returns
// the subset in that collection not matching the compare. Rely on return-value
// optimization for the returned container.
//
template< template<class, class> class TContainer, class TObject> //, class TCompare>
TContainer FindItemsNotMatching(const TContainer& container) //, TObject object)
{
TContainer found;
auto itemIt = container.begin(), end = container.end();
auto compare = [](const TObject& item) -> bool
{
return typeid(*item) != typeid(TCompare);
};
while (itemIt != end)
{
itemIt = std::find_if(itemIt,
allEntities.end(),
compare);
if (itemIt != end)
{
found.push_back(*itemIt);
}
}
return found;
}
调用者希望看起来像这样:
std::vector<MyType*> vectorFullOfPolymorphicTypes;
std::vector<MyType*> notMatching = FindItemsNotMatching<std::vector<MyType*>, TypeDerivedFromMyType> (vectorFullOfPolymorphicTypes);
不幸的是,我目前仅限于使用 Visual Studio 2012,因此不可能使用可变参数模板。任何帮助表示赞赏。这里有一些我还没有弄清楚的错误。
UdfAlgorithm.cpp:9: error: C3205: argument list for template template parameter 'TContainer' is missing
解决方案
不需要模板模板参数,只需使用typename TContainer
:
您可以更改模板参数的顺序以避免提供可推导的类型。
您可以使用常规函子来模拟通用 lambda(因此避免提供额外的类型),或者value_type
从容器中使用。
C++11 引入了std::copy_if
.
template <class TObject, typename TContainer>
TContainer FindItemsNotMatching(const TContainer& container)
{
TContainer found;
auto predicate = [](const typename TContainer::value_type& item) -> bool
{
return typeid(*item) != typeid(TObject);
};
std::copy_if(container.begin(), container.end(), std::back_inserter(found), predicate);
return found;
}
用法:
std::vector<MyType*> myypes;
std::vector<MyType*> notMatching = FindItemsNotMatching<SomeDerivedType>(myTypes);
推荐阅读
- python - 如何在 VS Code 中为选择的文件、glob 或类似文件关闭排序导入?
- python - 人脸识别opencv得到类型元组错误
- matlab - 带有 textdat 的文本文件包含字符的单元格
- c# - 使用 ExcelPackage 导入 XLSX 文件会引发错误 .NET CORE
- django - 将两个值传递给模板标签
- python - 如何从带有变量的 sqlite 中选择
- openstack - openstack中实例的降级风格
- python - 如何从视图或中间件获取模型的 IP 地址?
- python - 在具有不同分隔符和值内容的标签/值列表中拆分字符串
- google-cloud-platform - 如何在托管实例组上指定容器 ID