Why should I define container classes in C++

What's a container class?

It's a class which is used to hold objects in memory or external storage to clumping variables of the same type to make the sorting, searching, … easier. A container could store many entities and provide direct access to them like string class.  Needless to say, The size of the container depends on the number of the objects it contains. A Container usually defined as a class that gives you the power to store any type of data. A container class acts as a generic holder.

a container class is in two kinds: Heterogeneous and homogeneous. Heterogeneous container class can hold mixed objects in memory whereas when it is holding same objects, it is called as homogeneous container class. A simple container class example, I define an array with initialized members:

  1. vector<int> numbers(5);

Now you can define an access to the members of this container:

  1. #include <vector>
  2. using namespace std;
  3. int numbers[]={1, 4 , 4 , 7 , 8 };
  4. int main () {
  5.   vector<int> numbers(numbers, numbers+6);
  6. }

You can also access to the members of this container:

  1.  v[1]=4;


Tags: C++


comments powered by Disqus