如何扩展vector

degree_37 2009-04-09 08:20:27
我想用STL中的容器vector 但需要增加一些自己的函数 比如Contains Remove等等 就是在不修改vector文件的前提下,如何把自己新增加的函数写到vector中
...全文
219 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
degree_37 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 mengde007 的回复:]
藐视……
[/Quote]

好吧...
mengde007 2009-04-09
  • 打赏
  • 举报
回复
藐视……
degree_37 2009-04-09
  • 打赏
  • 举报
回复
知道了 没写using namespace std;

还忘了写函数前面的void int等...
mengde007 2009-04-09
  • 打赏
  • 举报
回复
……
degree_37 2009-04-09
  • 打赏
  • 举报
回复

#ifndef VECTORCHILD_H
#define VECTORCHILD_H

#include <vector>
template <class Type> class vectorChild : public vector<class Type>
{
public:
int Contains(Type);
void AddUnique(Type);
void Remove(Type);
};

template <class Type>
vectorChild<Type>::Contains(Type t)
{
int i;
int count=0;
for (i=0;i<this->size();i++)
{
if(this->at(i)==t) count++;
}
return count;
}

template <class Type>
vectorChild<Type>::AddUnique(Type t)
{
if(!Contains(t)) push_back(t);
}

template <class Type>
vectorChild<Type>::Remove(Type t)
{
int i;
for (i=0;i<this->size();i++)
{
if(this->at(i) == t)
break;
}
this->erase(i);
}


#endif //VECTORCHILD_H


请问我这么写错误怎么一堆呢:
1>f:\programs\trys\simpgroupmesh\simpgroupmesh\vectorchild.h(5) : error C2504: 'vector' : base class undefined
1> f:\programs\trys\simpgroupmesh\simpgroupmesh\vectorchild.h(11) : see reference to class template instantiation 'vectorChild<Type>' being compiled
1>f:\programs\trys\simpgroupmesh\simpgroupmesh\vectorchild.h(5) : error C2143: syntax error : missing ',' before '<'
1>f:\programs\trys\simpgroupmesh\simpgroupmesh\vectorchild.h(5) : error C2518: keyword 'class' illegal in base class list; ignored
1>f:\programs\trys\simpgroupmesh\simpgroupmesh\vectorchild.h(23) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>f:\programs\trys\simpgroupmesh\simpgroupmesh\vectorchild.h(29) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>f:\programs\trys\simpgroupmesh\simpgroupmesh\vectorchild.h(41) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
sayoath 2009-04-09
  • 打赏
  • 举报
回复
通过别代器
mengde007 2009-04-09
  • 打赏
  • 举报
回复
楼上不错
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 degree_37 的回复:]
引用 2 楼 Loaden 的回复:
不推荐从vector派生。否则容易内存泄露!
还是自己实现吧。


自己实现是说要自己重新写一个类似于vector的类?
[/Quote]

自己写太麻烦了,还是派生子类吧
degree_37 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 Loaden 的回复:]
不推荐从vector派生。否则容易内存泄露!
还是自己实现吧。
[/Quote]

自己实现是说要自己重新写一个类似于vector的类?
「已注销」 2009-04-09
  • 打赏
  • 举报
回复
不推荐从vector派生。否则容易内存泄露!
还是自己实现吧。
mengde007 2009-04-09
  • 打赏
  • 举报
回复
那就从vector派生出来一个新的类,然后加入自己代码

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧