STL Container Class Introduction

STL Container Class Introduction

Before we start dig deep into STL, it is mandatory that we learn about Container classes.

A Container class is defined as a class that gives you the power to store any type of data. There are two type of container classes available in STL in C++, namely “Simple Container Classes” and “Associative Container Classes”. An Associative Container class associates a key to each object to minimize the average access time.

Simple Container Classes

  • vector<>
  • lists<>
  • stack<>
  • queue<>
  • deque<>

Associative Container Classes

  • map<>
  • set<>
  • multimap<>
  • multiset<>

This article will discuss about the Simple Container Classes in detail. This will show you how to use the different methods of these classes to achieve your goal. Let’s Start with vector<>

vector<>   The Open Array

Constructor of Vector class

There are 3 overloaded constructors in the vector class excluding the default one. We will discuss about them one by one. You can create a vector object with predefined size and values for them. Here is how you can do that. Suppose you want to create a vector<int> object whose initial capacity will be 10 and all the values will be set to 2. Then you can create such an object using one of the overloaded versions of the vector constructor

vector<int> nums(10,2);

If you just want to specify the capacity but don’t want to assign values for them then you can use the other constructor which accepts an integer to set the capacity of the vector like

vector<int> nums(100);

The last constructor allows you to initialize the vector with data from other vectors or arrays or other collections. Suppose you want to copy one vector to the other then this constructor proves to be very handy. Here is the code snippet.

vector<int> nums; for(int i=1;i<5;i++) nums.push_back(i); vector<int> codes(nums);

Here codes is a vector. We have copied the contents of nums in codes using the constructor.

Methods of vector class

 

  • _Destroy()
  • _Eq()
  • _Lt()
  • _Ucopy()
  • _Ufill()
  • assign()
  • at()
  • begin()
  • back()
  • capacity()
  • clear()
  • empty()
  • end()
  • erase()
  • front()
  • get_allocator()
  • max_size()
  • insert()
  • operator=
  • operator[]
  • pop_back()
  • push_back()
  • rbegin()
  • rend()
  • reserve()
  • resize()
  • size()
  • swap()
  • ~vector()

Here we will discuss about each method, will show you what it does and then at the end we will give example where you can use these methods together….

The methods that start with an underscore are protected methods and can’t be accessed using object of the class. These methods are used by other method internally.

assign() and size()


This method is used to assign an initial size to the vector. This method has 2 overloaded versions. Here we have used the first overloaded method. This method takes an integer argument to initialize the size of the vector.

#include <iostream>
#include <vector>
#include <conio.h>

using namespace std;

int main()
{
vector<char> codes;
codes.assign(10);//Assigning the size to 10.
cout<<codes.size();//prints the size of the vector
getch();
return 0;
}
The next overloaded match takes 2 pointer as arguments. Here is a sample code
#include <iostream>
#include <vector>
#include <conio.h>
using namespace std;

int main()
{
char a[3]={'a','b','c'};
vector<char> orders;
orders .assign(a,a+3);
cout<<orders.size();
getch();
return 0;
}
Here the pointer used are nothing but the array name. This concept owes to C. Basically array name is a pointer to that array.

at()


No matter how sophisticated tool we get, accessing the old C style array using the index is really popular. This thing was kept in mind when STL was designed. As vector behaves like an open array, people expect it to show other behaviors of the array. at() is a method that takes an integer as argument and return the value at that location. So in a way it simulates the age-old array indexing. Here is a code snippet.
#include <iostream>
#include <vector>
#include <conio.h>

using namespace std;

int main()
{
vector<int> codes;
for(int i=0;i<10;i++)
codes.push_back(i);
cout<<codes.at(9)<<endl;
getch();

return 0;
}
The output of this code will be 9.

begin() , end()


In STL within containers the elements are accessed using iterators. Iterators are something like pointers. begin() and end() returns the iterator to the beginning and to the end of the container. Here one thing should be noted end() points to a location which is one after the physical end of the container. These two methods are the most used ones in STL, because to traverse a container, you need to create an iterator. And then to initialize it’s value to the beginning of the container. Here is the code to traverse a vector<>. This code make use of begin() and end() methods.

#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> numbers;
for(int i=0;i<10;i++)
numbers.push_back(i);
//Assiging to the beginning
vector<int>::iterator k = numbers.begin();
//please note that k is kept less than
//numbers.end() because end() points to somewhere which is beyond physical end.
for(;k<numbers.end();k++)
cout<<*k<<endl;
return 0;
}

 

front(), back()


front() method returns the element at the front of the vector. back() method returns the element at the back of the vector. Here is the code to understand its operations better.

#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> m;
for(int i=0;i<10;i++)
m.push_back(i);
cout<<m.front()<<endl;
cout<<m.back()<<endl;
return 0;
}

The output of this program is
0
9

capacity() , size()


capacity() returns the number of elements the vector can hold initially assigned. For a vector although it is not very important method. On the other hand the method size() returns the number of elements currently in the vector. Here is the code that will help you understand the difference between capacity() and size().

#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> m(100);
cout<<m.capacity()<<endl;
for(int i=0;i<10;i++)
m.push_back(i);
cout<<m.size()<<endl;
return 0;
}

The output of the program is

100 110

Because initially using the constructor we created a vector whose capacity is 100. Then we are pushing 10 elements to its back. Therefore the size increases to 110.

1 2 3

Close    To Top
  • Prev Article-Programming:
  • Next Article-Programming:
  • Now: Tutorial for Web and Software Design > Programming > cplus > Programming Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction