Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7808361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:06:50+00:00 2026-06-02T03:06:50+00:00

#ifndef VECTOR #define VECTOR template<typename T> struct vector { private: T *buffer; unsigned long

  • 0
#ifndef VECTOR
#define VECTOR

template<typename T>
struct vector
{
private:
    T *buffer;
    unsigned long sz;
    unsigned long cap;
public:
    typedef T value_type;
    typedef value_type& reference;
    typedef const reference const_reference;
    typedef value_type* pointer;
    typedef const pointer const_pointer;
    typedef T* iterator;
    typedef const T* const_iterator;
    typedef unsigned long size_type;
    typedef ptrdiff_t difference_type;

    vector() : buffer(new int[10]), sz(0), cap(10) {}
    vector(size_t s) : buffer(new int[sz]), sz(s), cap(s){}
    vector(size_t s, const T& initial) : buffer(new int[sz]), sz(s), cap(s) { for(size_t i = 0; i < sz; i++) buffer[i] = initial; }
    template<typename container, typename It>
    vector(typename container::It beg, typename container::It end)
    {
        It iter(beg);
        sz = ptrdiff_t(beg-end);
        cap = sz;

        for(int i = 0; i < sz; i++)
        {
            buffer[i] = iter++;
        }
    }
    ~vector() {delete [] buffer;}

    iterator begin() {return buffer;}
    const_iterator begin() const {return buffer;}

    iterator end()   {return buffer+sz;}
    const_iterator end() const {return buffer+sz;}

    void reserve(size_t newCap)
    {
        if(newCap <= cap) return;
        T *oldBuffer = buffer;

        buffer = new T[newCap];

        for(int i = 0; i < cap; i++)
        {
            buffer[i] = oldBuffer[i];
        }

        cap = newCap;
        delete [] oldBuffer;
    }
    void resize(size_t newSz, const T& initial = T())
    {
        if(newSz > cap)
            reserve((newSz*2+1)%(max_size()+1));
        if(newSz > sz)
        {
            for(int i = sz; i < newSz; i++)
                buffer[i] = initial;
        }
        sz = newSz;
    }
    size_t size() const {return sz;}
    size_t capacity() const {return cap;}
    bool empty() const {return sz == 0;}
    size_t max_size() const {return 1073741823;}
    void push_back(T toP)
    {
        if(sz >= cap)
            reserve((cap*2+1)%(max_size()+1));
        buffer[sz++] = toP;
    }
    T pop_back()
    {
        T ret = buffer[sz-1];
        buffer[sz-1] = T();
        sz--;
        return ret;
    }
    reference front() { return buffer[0]; }
    const_reference front() const { return buffer[0]; }

    reference back() { return buffer[sz-1]; }
    const_reference back() const { return buffer[sz-1]; }

    T& operator[](size_t index) {if(index >= sz) throw std::out_of_range("out_of_rane"); return buffer[index]; }
    const T& operator[] (size_t index) const {if(index >= sz) throw std::out_of_range("out_of_rane"); return buffer[index];}

    T& at(size_t index) { return (*this)[index]; }
    const T& at(size_t index) const { return (*this)[index]; }  
};

#endif

This is an implementation of the vector class. It uses a dynamic array of type T, which grows bigger with the function reserve(). How it would be like if I used the allocator class to implement it? (the whole vector class, not just the reserve function)

 template<class T, class Allocator = allocator<T> >

This is how it looks like on the file stl_vector.h

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T03:06:51+00:00Added an answer on June 2, 2026 at 3:06 am

    The default allocator of the STL effectively calls the new operator to do its work. So I assume that won’t change much. But, performance questions can never be sufficiently answered by looking at the code. Instead you should implement both versions and measure with some typical test data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#ifndef ASSETS_H_INCLUDED #define ASSETS_H_INCLUDED #include <vector> #include string.h> const int ID_Max = 100; typedef
#ifndef IMAGEDATA_H #define IMAGEDATA_H #include <iostream> #include <vector> class ImageData { public: std::string foo;
The class corresponding to this crash is: #ifndef IMAGE_DATA_ #define IMAGE_DATA_ #include <stdexcept> template
Header file is graph.h #ifndef _GRAPH_H_ #define _GRAPH_H_ #include <map> #include <vector> using namespace
Suppose I have the following files: lib/A.h #ifndef A_H #define A_H #include <vector> class
I have a struct declared as follows: #ifndef PLAYLIST_H #define PLAYLIST_H #include <iostream> #include
My code is as below: Interpolation.h #ifndef INTERPOLATOR #define INTERPOLATOR #include <vector> #include <utility>
Everything was compiling until I added vector<Move> bestLine; search.h #ifndef SEARCH_H #define SEARCH_H #include
I have the following header file: #ifndef DATABASE_H #define DATABASE_H #include <vector> #include <iostream>
#ifndef MACROS_NULLCHECK_H_ #define MACROS_NULLCHECK_H_ #include <assert.h> #define NULLCHECK(x) assert(x != (void *) 0); #endif

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.