#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
template <template <typename...> class ContainerType, typename ItemType>
bool has_item(ContainerType<ItemType> items, ItemType target_item)
{
for (auto x : items)
{
if (x == target_item) return true;
}
return false;
}
template <template <typename...> class ContainerType, typename ItemType>
ContainerType<ItemType> filter(ContainerType<ItemType> items, const std::function <bool (ItemType)>& f)
{
ContainerType<ItemType> filtered_items;
std::copy_if(items.begin(), items.end(), std::back_inserter(filtered_items), f);
return filtered_items;
}
template <template <typename...> class ContainerType, typename ItemType>
bool is_vector(ContainerType<ItemType> items)
{
if (std::is_same<std::vector<ItemType>, ContainerType<ItemType>>::value)
return true;
else
return false;
}
int main()
{
std::vector<int> v = {1, 2, 3, 4, 5};
std::cout << "v has 1? " << has_item(v, 1) << std::endl; // Compiler can deduce ContainerType = vector, ItemType = int
std::vector<int> v2 = filter<std::vector, int>(v, [](int x) { return x < 4; } ); // Compiler can not deduce types here
std::cout << "v2 = ";
for (const auto& x : v2)
std::cout << x << ", ";
std::cout << std::endl;
std::cout << "v2 is vector? " << is_vector(v2) << std::endl; // Compiler can deduce here
return 0;
}