C++ primer 5th 课后习题
将以前的StrVec类重写为模板
实现类似vector的一些简易操作
//16.16 将StrVec类重写成模板 命名为Vec
//前置声明
//.h
template <typename T> class Vec;
template <typename T> bool operator==(const Vec<T>&,const Vec<T>&);
template <typename T> bool operator!=(const Vec<T>&,const Vec<T>&);
template <typename T> bool operator< (const Vec<T>&,const Vec<T>&);
template <typename T> bool operator> (const Vec<T>&,const Vec<T>&);
template <typename T> bool operator<=(const Vec<T>&,const Vec<T>&);
template <typename T> bool operator>=(const Vec<T>&,const Vec<T>&);
template <typename T>
class Vec {
friend bool operator==<T>(const Vec<T>&, const Vec<T>&);
friend bool operator!=<T>(const Vec<T>&, const Vec<T>&);
friend bool operator< <T>(const Vec<T>&, const Vec<T>&);
friend bool operator> <T>(const Vec<T>&, const Vec<T>&);
friend bool operator<=<T>(const Vec<T>&, const Vec<T>&);
friend bool operator>=<T>(const Vec<T>&, const Vec<T>&);
public:
typedef size_t size_type;
typedef T value_type;
Vec() :elements(nullptr), first_free(nullptr), cap(nullptr) {
}
Vec(initializer_list<T> il);
Vec(const Vec<T>&);
Vec(Vec<T>&&) noexcept;//移动构造函数
Vec& operator=(const Vec<T>&);
Vec& operator=(Vec<T>&&) noexcept;//移动赋值运算符
~Vec();
size_t size()const {
return first_free - elements; }
size_t capacity() const {
return cap - elements; }
void push_back(const T& t) ;
void push_back(T&& t) noexcept;
void pop_back();
T& at(size_t i) {
return *(elements