如何实现std :: Vector(首先是InputIt,最后是InputIt,const allocator_type& alloc = allocator_type())

时间:2017-10-16 20:48:15

标签: c++11 vector stl

我正在尝试根据STL接口重写c ++ std :: vector:以下是我在vector.h文件中所做的

vector.h

#include <memory>
#include <cstddef>
#include <iostream>
#include <iterator>
#include <memory>
namespace ting{
template<class T, class Alloc = std::allocator <T>>
class Vector{
using value_type = T;
using allocator_type = Alloc;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = typename std::allocator_traits<Alloc>::pointer;
using const_pointer = typename std::allocator_traits<Alloc>::const_pointer;
using size_type = std::size_t;
using iterator = std::iterator<std::random_access_iterator_tag, value_type>;
using const_iterator = const iterator;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
using difference_type = std::ptrdiff_t;
private:
    pointer data;
    size_type size;
    size_type capacity;
    allocator_type alloc;
public:
    explicit Vector(const allocator_type& alloc = allocator_type()):alloc(alloc){size = 0; capacity = 0;}
    Vector(size_type n, const value_type& val, const allocator_type& alloc = allocator_type()):size(n),capacity(n),alloc(alloc){
        data = new T[size];
        for(size_type i =0; i<size;i++){
             data[i] = val;
        }
    }
//don't know how to implement
    template< class InputIt>
    Vector(InputIt first, InputIt last, const allocator_type& alloc = allocator_type()){
        size = capacity = difference_type(last - first);
        data = new value_type[size];
        InputIt temp;
        size_type  i;
        for(temp = first, i = 0;temp!=last; temp++,i++){
            data[i] = *temp;
        }
    }

    ~Vector(){ delete []data;}
//......
};
};

我建立时:我收到了以下编译错误:

  

/home/ting/Proj/vector_test/vector.h:44:错误:一元'*'的无效类型参数(有'int')                data [i] = * temp;                          ^ ~~~~

有人能举例说明如何实现这个构造函数吗?

0 个答案:

没有答案