简体   繁体   English

抽象的地图和stl中的向量

[英]abstract map and vector in stl

If I have three types of maps and vectors: 如果我有三种类型的地图和矢量:

map1<CString, int>
map2<CString, int, std::function1>
map3<some_wrapper_of_CString, int, std::function2>

In stl is there an abstract container or a way to write my own abstract container for the sole purpose of doing polymorphism: 在stl中,有一个抽象容器或一种编写我自己的抽象容器的方法,其唯一目的是实现多态:

abstract_map = map1 or map2 or map3 abstract_map = map1或map2或map3

My class contains map1 which I cannot modify because it will crash other classes that my colleges at work use. 我的课程包含无法修改的map1,因为它会使我的大学在工作中使用的其他课程崩溃。 I thought by creating an abstract map to wrap map1 I can pass map2 and 3 without other modifications... 我认为通过创建一个抽象的地图来包装map1,我可以通过map2和3而无需进行其他修改...

I hope it makes sense..... 我希望这是有道理的.....

No, there isn't. 不,没有。 What would be the purpose of it when the elements are not convertible to each other? 当元素不能彼此转换时,它的目的是什么?

You can always do: 您可以随时这样做:

containera c;
containerb c2(c.begin(), c.end());

which will convert all elements from one to the other container. 它将所有元素从一个容器转换到另一个容器。

If you actually have containers with the same underlying value_type the usual abstraction over the container type are iterators and not abstract containers. 如果您实际上拥有的容器具有相同的基础value_type则通常对容器类型的抽象是迭代器,而不是抽象容器。

After you have clarified that you only want type-erasure on the functor type: 弄清楚只需要对仿函数类型进行类型擦除之后:

class X {
  // constructor taking the full type
  X(const std::map<CString, int, std::function<bool(CString, int)>& x) : my_map(x) {}

  // constructor taking the default std::less
  X(const std::map<CString, int>& x) : my_map(x.begin(), x.end(), std::less<CString>()) {}

private:
  std::map<CString, int, std::function<bool(CString, int)> my_map;
};

In stl is there an abstract container or a way to write my own abstract container for the sole purpose of doing polymorphism 在stl中,有一个抽象容器或一种编写我自己的抽象容器的方法,其唯一目的是实现多态性

In the standard C++ library, there isn't. 在标准C ++库中没有。

Templates and dynamic polymorphism don't mix all that well. 模板和动态多态性不能很好地融合在一起。

Not only there are no abstract containers in the STL: you should never inherit from STL containers because they are not intended to behave polymorphically (no virtual destructor, for instance). STL中不仅没有抽象容器:您永远不应继承STL容器,因为它们不旨在表现多态(例如,没有虚拟析构函数)。 So you have to look for a different solution. 因此,您必须寻找其他解决方案。

In this case, you have to favour composition over inheritance. 在这种情况下,您必须更偏重于继承而不是继承。 You may write your own abstract map class that contains an std::map , such as: 您可以编写自己的包含std::map的抽象地图类,例如:

template<typename T>
class BaseMap
{
  // Whatever you need here

private:

  std::map<T> internalMap;
}

and then inherit from this class as needed: 然后根据需要从此类继承:

template<typename T>
class Map1 : public BaseMap<T>
{
  // Specialise your class here
}

Of course, BaseMap should offer the interface you need for your maps, since it does not inherit std::map interface. 当然, BaseMap应该提供地图所需的接口,因为它不继承std::map接口。 You can write any virtual, pure virtual or non-virtual methods you need here. 您可以在此处编写所需的任何虚拟,纯虚拟或非虚拟方法。

Though maybe the first question you have to ask yourself is: do you really need different map classes? 尽管您可能要问自己的第一个问题是:您是否真的需要不同的地图类? And, if so, do you really need them to be polymorphic? 而且,如果是这样,您真的需要它们具有多态性吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM