Container is a collection which help us to store data of different types of data structure.

There are only two types of container in C++/C :

  • array
  • structure

C++ could provide more container but it didn’t.

Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime; knowledge is the best charity;

:)

Here we are gona to design a container which is like array but not the same.

images

You could find the implementation of this container on my github.

Our Container Implementation

You could test out container by this program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "con_array.h"
int main()
{
Array<int> *ap = new Array<int> (10);
Pointer<int> p(*ap, 5);
//delete ap;
for (int i = 0; i < 10; i++)
{
(*ap)[i] = i;
}
*p = 42;
cout << "The size of Array " << ap->size() <<endl;
cout << (*ap) << endl;
ap->resize(20);
cout << "After resize(), the size of Array " << ap->size() <<endl;
cout << (*ap) << endl;
return 0;
}

You could also see the output beblow there:

images


Photo by Jason Leaster in ChangDe, China

images