gpt4 book ai didi

C++ 判断一个容器是否有::find()

转载 作者:可可西里 更新时间:2023-11-01 18:31:26 28 4
gpt4 key购买 nike

<分区>

我有一个仿函数,它对类型为 T 的元素的类型为 U 的容器进行操作,就像这样

template<typename T, template<typename...> class U>
class asserter
{
public:
asserter(U<T> &c) : container(c) { };
void operator()(T lhs)
{
CU_ASSERT(container.find(lhs) != container.end());
};
private:
U<T> &container;
};

我可能会用它作为

std::set<std::string> a, c;
...
asserter<std::string, std::set> ass(c);
for_each(a.begin(), a.end(), ass);

我们暂时忽略 std::includes() 的地方。

如果容器是定义了 U::find() 的容器,这会很好用。如果不是,我想回退到 std::find()。另一方面,如果可用,我宁愿使用 U::find() 而不是 std::find()

在 C++11(或必要时为 17)中,我可以确定 U::find() 是否对 U 可用(可能限制为 STL),如果可用,则使用它,否则使用std::find()

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com