Code:
#ifndef COMPILER_H
#define COMPILER_H
#include <iostream>
#include <string>
using namespace std;
template<typename T, typename A=allocator<T> >
class basic_vector
{
// typedef typename A::pointer iterator;
// typedef typename A::reference reference;
1 typedef typename A::size_type size_type;
iterator First, Last;
A alloc;
iterator begin()
{
return (First);
}
iterator end()
{
return (Last);
}
// void Destroy(iterator F, iterator L)
// {
2 for(; F != L; ++F)
{
alloc.destroy(F);
}
}
void insert( iterator P, size_type M, const T& X)
{
// iterator S = alloc.allocate(Last-First+M);
//
3 uninitialized_fill_n(S+(P-First), M, X);
if (Last-First != 0)
{
uninitialized_copy(First, P, S);
uninitialized_copy(P, Last, S);
Destroy(First, Last);
alloc.deallocate(First, Last-First);
}
Last = S+(Last-First)+M;
First = S;
}
public:
basic_vector() : First(0), Last(0) { }
~basic_vector()
{
Destroy(First, Last);
alloc.deallocate(First, Last-First);
First = 0;
Last = 0;
}
// reference operator[](size_type P)
// {
4 return (*(begin()+P));
}
// const T& operator+=(const T& s)
// {
5 insert( Last, 1, s);
return s;
}
};
#endif
Code:
#include "basic_vector.h"
using namespace std;
int main( )
{
//Example 1
basic_vector<string> cont;
cont += "Hello";
cont += "World";
cont += "!";
cont[2] = "!!!";
cout << "0) " << cont[0] << endl;
cout << "1) " << cont[1] << endl;
cout << "2) " << cont[2] << endl;
}
When we run this we should see on the screen