qq_28043113 2017-06-05 10:29 采纳率: 0%
浏览 1485

C++编程题,期末要答辩!

写一个有关集合的类实现缺省构造、复制构造、集合元素限定在整数、小数、字符以及字符串。同时实现一些基本运算图片说明

  • 写回答

3条回答

  • Azhu阿朱 2017-06-05 14:45
    关注

    #include

    #include

    #include

    using namespace std;
    template
    void destroy(T* pointer)
    {
    pointer->~T();
    }
    template
    void destroy(Iterator1 first, Iterator1 last)
    {
    for (Iterator1
    it = first; it != last; ++it)
    {
    destroy(&*it);
    }
    }
    template
    class Vector
    {
    public:
    typedef T value_type;
    typedef T* Iterator;
    typedef const T*const_Iterator;
    typedef T* pointer;
    typedef const T* const_pointer;
    typedef size_t size_type;
    Vector()
    : start(NULL), end(NULL), resum(NULL)
    {}
    ~Vector()
    {
    ::destroy(start, end);
    alloc.deallocate(start, resum - start);
    }
    Iterator Begin() { return start; }
    Iterator End() { return end; }
    void insert(Iterator position, size_type n, const T& value);
    void Push_back(const T& value)
    {insert(End(), 1, value);}
    protected:
    Iterator start;

    Iterator end;

    Iterator resum;
    private:
    static std::allocator alloc;

    };
    template
    std::allocator Vector::alloc;
    template
    void Vector::insert(Iterator position, size_type n, const T& value)
    {
    if (n <= resum - end)
    {
    if (n <= end - position)
    {
    std::uninitialized_copy(end - n, end, end);
    std::copy(position, end - n, position + n);
    std::fill_n(position, n, value);
    }
    else
    {
    std::uninitialized_fill_n(end, n - (end - position), value);
    std::uninitialized_copy(position, end, position + n);
    std::fill(position, end, value);
    }
    end += n;
    }
    else
    {
    pointer new_start(NULL), new_end(NULL);
    size_type old_type = resum - start;
    size_type new_size = old_type + std::max(old_type, n);
    new_start = alloc.allocate(new_size);
    new_end = std::uninitialized_copy(start, position, new_start);
    std::uninitialized_fill_n(new_end, n, value);
    new_end += n;
    new_end = std::uninitialized_copy(position, end, new_end);
    alloc.deallocate(start, resum - start);
    start = new_start;
    end = new_end;
    resum = new_start + new_size;
    }
    }
    int main()
    {
    Vector a;
    for(int i=1;i<=5;i++)
    a.Push_back(i);
    Vector::Iterator it;
    for(it=a.Begin();it!=a.End();++it)
    {
    cout<<*it<<endl;
    }
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog