【发布时间】:2015-02-24 20:35:29
【问题描述】:
是否可以在 C++(11) 中为函数创建一个模板来检查对象是否包含在 std::vector、std::array 或 std::list(甚至可能更多的容器类型)中?
我现在拥有的:
typedef std::shared_ptr<Tag> SharedTag;
typedef std::vector<SharedTag> TagList;
bool
Tag::isIn(const TagList& lst) {
return std::any_of(lst.begin(), lst.end(), [this](const SharedTag& t) {
return t->name == this->name;
});
}
Tag 是一个普通的class。当然,比较应该在t == this 进行,稍后将是operator==。为简单起见,我没有在此处包含此内容。
那么,是否可以只为std::vector、std::array、std::list(,也许是std::set) 等编写一次上层代码(尽管没有typedef)?
我找不到所有这些类的基本类型,...这是我的第一个想法...
【问题讨论】:
-
你能用迭代器来写你的代码吗?有随机访问迭代器、前向迭代器等类别。
-
@NeilKirk:内部位已经做到了,他似乎只是想要一个包装函数
标签: c++ arrays c++11 vector stl