我正在进行POC实现,并且根据要求我需要扩展std::vector插入API,它只需要单个参数(要插入的值),并且内部代码会在容器的末尾添加它.
我创建了一个自定义类(ValVector)派生自std::vector并定义了一个自定义插入API,它接受单个参数但在编译时抛出错误.
以下是带有错误消息的代码段代码:
#include <iostream>
#include <vector>
using namespace std;
typedef bool BOOL;
template<class T, class Allocator = allocator<T>>
class ValVector : public std::vector<T, Allocator> {
public:
BOOL insert(const T& elem) { return (this->insert(this->end(),elem)!=this->end()); }
};
int main ()
{
std::vector<int> myvector (3,100);
std::vector<int>::iterator it;
myvector.push_back (200 );
ValVector<int> mKeyAr;
mKeyAr.insert(10); //
std::cout << "myvector contains:";
for (auto it=mKeyAr.begin(); it<mKeyAr.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
In instantiation …Run Code Online (Sandbox Code Playgroud)