多态处理向量和hash_set的函数

发布于 2024-12-03 10:43:54 字数 308 浏览 0 评论 0原文

我在 C++ 中有一个函数,它接受一个向量并将一些项目推到它上面。例如:

void MyFunction(vector<int>* output);

我想修改它以便现在能够获取向量 hash_set。在 Java 中,这很容易,只需更改函数以获取 Collection(公共接口)即可。 MyFunction 所做的就是将元素放入给定的容器中,因此它不必关心该容器是向量还是 hash_set,只需要关心“插入”元素的一些概念。

感谢您的帮助!

I have function in C++ that takes a vector and pushes some items onto it. For example:

void MyFunction(vector<int>* output);

I want to modify it to be able to now take a vector or a hash_set. In Java, this would be easy, simply change the function to take a Collection (the common interface). All MyFunction does is put elements into the container it is given, so it shouldn't have to care whether that container is a vector or a hash_set, only that there is some notion of "inserting" an element.

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

动次打次papapa 2024-12-10 10:43:54

他在制作直接模板时遇到了一个小问题。 Vector通常使用push_back,而hash_set则使用insert。

使用模板,但使用插入函数并在 .end() 元素处插入,以便向量保持快速操作 - 这是您的最佳选择。如果它将插入位置作为位置提示,您仍然会稍微减慢哈希集的速度(尽管它会起作用)。

使用 insert(iter, val),通过在迭代器位置的元素之前插入新元素来扩展容器。对于序列容器(向量)来说是这样,而关联容器(hash_set)只会使用位置作为提示 - 但插入仍然可以正常工作。下面的函数可用于将值 U 插入任何支持插入的容器(所有 STL 都这样做),假设 U 与向量元素类型相同或隐式可转换为向量元素类型。

template <typename T, typename U>
void InsertToContainer(T& container, U val)
{
    container.insert(container.end(), val);
}

He'd have a small issue doing a straight-up template. Vector typically uses push_back while hash_set would use insert.

Use a template, but use the insert function and insert at the .end() element so that the vector maintains fast operations - its your best bet. You'll still slow down your hash-set a little though if it takes the insertion position as a hint of location (it'll work though).

Using insert(iter, val), the contianer is extended by inserting new elements before the element at the iterator position. This is true for sequence containers (vector) while associative containers (hash_set) will just use the position as a hint - but the insertion will still work fine. The function below could be used to insert value U into any container supporting insert (all STL ones do) assuming U is the same as or implicitly convertible to the vector element type.

template <typename T, typename U>
void InsertToContainer(T& container, U val)
{
    container.insert(container.end(), val);
}
与风相奔跑 2024-12-10 10:43:54

如果我理解正确,您可以使用 Boost::Variant

If I understand correctly, You can use Boost::Variant

生生漫 2024-12-10 10:43:54

函数模板可以让您使其更加通用:

 template<typename T, typename Cont = std::vector<T> > void MyFunction(T t, Cont c);

这里的默认容器是 std::vector,但如果您愿意,您仍然可以更改容器。

A function template would allow you to make it more generic:

 template<typename T, typename Cont = std::vector<T> > void MyFunction(T t, Cont c);

Here the default container would be std::vector<T>, but you could still change the container if you wanted to.

半步萧音过轻尘 2024-12-10 10:43:54

如果您需要使用两个容器之间不常见的方法,则可以使用纯虚拟基类创建两个派生包装类,该基类为您想要使用 unordered_set 执行的操作定义通用接口> 和一个没有相似方法名称的向量,然后将基类的指针或引用传递给函数。

例如,您的纯虚拟基类可以定义用于插入、查找、迭代等的方法,并且可以掩盖执行这些类型操作的这两个容器之间的方法名称和方法使用方面的差异。您的派生类型将是基类中纯虚拟方法的实际实现。您的函数将采用指向基类型的引用或指针,并且由于基类是多态的,因此您只需调用基类定义的接口方法,并同时使用 vector 和 < code>unordered_set 与您的函数一起使用,无需担心线性容器和关联容器之间的实现差异和接口不匹配。

If you need to use methods that are not common between the two containers, you can create two derived wrapper classes with a pure virtual base class that defines a common interface for operations you would like to-do with a unordered_set and a vector that do not have simliar method names, and then pass pointers or references to the base-class to the function.

For instance, your pure virtual base-class can define methods for inserting, finding, iterating, etc., and can mask the differences in the method names and uses of the methods between these two containers that perform these types of operations. Your derived types would be the actual implementation of the pure virtual methods in the base-class. Your function would take a reference or pointer to the base-type, and since the base-class is polymorphic, you can just call the interface methods that the base-class defines, and use both vector and unordered_set with your function without worrying about implementation differences and interface mis-matches between linear containers and associative containers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文