多态处理向量和hash_set的函数
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
他在制作直接模板时遇到了一个小问题。 Vector通常使用push_back,而hash_set则使用insert。
使用模板,但使用插入函数并在 .end() 元素处插入,以便向量保持快速操作 - 这是您的最佳选择。如果它将插入位置作为位置提示,您仍然会稍微减慢哈希集的速度(尽管它会起作用)。
使用 insert(iter, val),通过在迭代器位置的元素之前插入新元素来扩展容器。对于序列容器(向量)来说是这样,而关联容器(hash_set)只会使用位置作为提示 - 但插入仍然可以正常工作。下面的函数可用于将值 U 插入任何支持插入的容器(所有 STL 都这样做),假设 U 与向量元素类型相同或隐式可转换为向量元素类型。
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.
如果我理解正确,您可以使用 Boost::Variant
If I understand correctly, You can use Boost::Variant
函数模板可以让您使其更加通用:
这里的默认容器是
std::vector
,但如果您愿意,您仍然可以更改容器。A function template would allow you to make it more generic:
Here the default container would be
std::vector<T>
, but you could still change the container if you wanted to.如果您需要使用两个容器之间不常见的方法,则可以使用纯虚拟基类创建两个派生包装类,该基类为您想要使用 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 avector
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
andunordered_set
with your function without worrying about implementation differences and interface mis-matches between linear containers and associative containers.