如何实现类似于 std::vector 的自定义类
How to implement a custom class similar to a std::vector
我的主题问题有点误导,我不想像 std::vector 那样实现整个类,但我希望能够创建一个名为 Container 的类,这样我就可以这样声明它:
Container <unsigned int> c;
这就是我重载<>运算符的方式......
class Container
{
private:
Container()
{
...
}
public:
void operator <>( unsigned int )
{
// what do I put here in the code?
// maybe I call the private constructor...
Container();
}
};
没有operator <>
. <>
表示Container
是一个类模板。 您需要以下语法:
template <typename T>
class Container
{
...
};
最好的起点是找到一本好的C++书,但您也可以尝试阅读例如关于模板的C++常见问题解答页面。
您应该了解有关模板的更多信息。
http://www.cplusplus.com/doc/tutorial/templates/
简而言之,您想要的是:
template <class T>
class Container {
....
};
相关文章:
- 在自定义 std::vector-like 容器中处理指针和非指针模板类型的最佳方法是什么?
- 为 std::vector 编写自定义插入函数
- 如何将自定义特征张量类存储到 std::vector 中?
- 从函数为 std::vector 创建自定义扩展
- 如何在不使用 vector::erase() 的情况下编写自定义 Vector 方法来删除元素?
- 如何将带有自定义分配器的 std::vector 传递给期望带有 std::分配器的函数?
- 不调用带有 std::vector 的自定义分配器
- 如何为数据类型 vector<glm::vec3> (opengl 数据类型) 编写 std::minmax 函数的自定义比较器
- std::vector 如何支持未知大小的自定义对象的连续内存
- 如何为我的自定义向量类实现类似 vector.insert 的方法
- 如何在 std::vector 内的自定义对象上使用 std::find
- 如何生成代码以自定义零值以t :: Zero的形式初始化具有自定义零值的std :: vector
- 创建具有自动存储的自定义对象并推送回 std::vector 时访问冲突
- 为自定义类继承std::vector::迭代器
- 如何在 C++11 的用户自定义类模板中继承 std::vector 模板
- 如何将 vector 传递给将自定义对象作为参数的重载输出流
- vector.erase 和 std::remove 在自定义向量上
- 继承自 std::vector<T> 和重载运算符 [] 用于自定义索引
- 具有重复数据的自定义对象的std::vector上的std::find_if
- C++获取线分段错误,std::vector<自定义类>