64,642
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <vector>
using namespace std;
template<class T>
class MyVector: public vector<T>
{
public:
const_reference operator[](size_type _P) const
{
return (*(begin() + _P)); //随便写
}
reference operator[](size_type _P)
{
return (*(begin() + _P)); //随便写
}
protected:
private:
};
void main()
{
MyVector<int> mv;
mv.push_back(1);
mv.push_back(2);
cout<<mv[0];
}
#include <iostream>
#include <vector>
using namespace std;
template<class T>
class MyVector: public vector<T>
{
public:
const_reference operator[](size_type _P) const
{
return (*(begin() + _P+1));//重载找下一个!
}
protected:
private:
};
void main()
{
MyVector<int> mv;
mv.push_back(1);
mv.push_back(2);
cout<<mv[0];
}
const_reference operator[](size_type _Off) const
{ // subscript nonmutable sequence
return (*(begin() + _Off));
}
reference operator[](size_type _Off)
{ // subscript mutable sequence
return (*(begin() + _Off));
}