Sequence Containers


As the name implies, these containers will have elements arranged in a linear manner. There are 3 types of sequence containers:

There are many functions which are common to all sequence containers while a few are specific to some of them.


Vectors

A vector is basically an array, which can grow dynamically in size as and when required. The programmer does not need to worry much about memory management since this is taken care of automatically. The following are some interesting points to note regarding vectors:

Function

Use

*front( )

Returns the value of the first element stored in the vector.

*back ( )

Returns the last element value stored in the vector.

*push_back( )

Pushing a new element (specified by the argument) into the container

*pop_back( )

Pops out the element at the end of the container (no arguments for this function).

*assign( )

Assign values to elements of the container

capacity( )

Returns the maximum elements that can be stored in the vector before automatic resizing

Note: * denotes that these functions are available in all sequence containers.


Capacity and Size of a vector:

Vectors keep resizing automatically as they increase in size. Thus memory is allocated automatically when needed as demonstrated in the program below:

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

int main( )
{
vector<int> intvector;
intvector.push_back(1);
cout<<"Size is:"<<intvector.size( )<<" Capacity : "<<intvector.capacity( );

intvector.push_back(2);
cout<<"\nSize is:"<<intvector.size( )<<" Capacity : "<<intvector.capacity( );

intvector.push_back(3);
cout<<"\nSize is:"<<intvector.size( )<<" Capacity : "<<intvector.capacity( );

intvector.push_back(4);
cout<<"\nSize is:"<<intvector.size( )<<" Capacity : "<<intvector.capacity( );

intvector.push_back(5);
cout<<"\nSize is:"<<intvector.size( )<<" Capacity : "<<intvector.capacity( );

intvector.push_back(6);
cout<<"\nSize is:"<<intvector.size( )<<" Capacity : "<<intvector.capacity( );

return 0;
}

The output is:

Size is:1 Capacity : 1

Size is:2 Capacity : 2

Size is:3 Capacity : 4

Size is:4 Capacity : 4

Size is:5 Capacity : 8

Size is:6 Capacity : 8

Size denotes the amount of memory space currently being used by the vector and capacity denotes the amount of memory allocated for the vector (as more space is needed this will keep increasing). It can be observed

Thus memory is allocated as follows:


Using overloaded operators on STL containers:

Let’s take a look at the overloaded operators which are available in all containers using a simple program.

//illustrating the use of overloaded operators in STL vectors

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

int main( )
{
vector<int> intvec1;
vector<int> intvec2;
vector<int>::const_iterator ptr; //declaring a constant iterator

intvec1.push_back(10);
intvec1.push_back(20);
intvec2.push_back(20);

if (intvec1<intvec2)
{
cout<<endl<<"Vector 1 is less than vector 2."<<endl;
intvec1=intvec2;
}

cout<<endl<<"Vector 1 now contains: ";

for ( ptr=intvec1.begin( ); ptr!=intvec1.end( ); ptr++ )
{
cout<<" "<<*ptr; //dereferencing the iterator
}

return 0;
}

The output will be:

Vector 1 is less than vector 2.

Vector 1 now contains: 20

The code

            vector<int>::const_iterator ptr;

is used to declare a constant iterator which can be used to only read the elements of a container.

When using the overloaded operators on containers, take note of the following points:

 

 

 

 

Even though the size of vector 1 is more than vector 2, the first element of vector 2 is greater than that of vector 1 (20 > 10).

Text Box: VECTOR 2
Text Box: VECTOR 1
Text Box: 20
Text Box: 20
Text Box: 10


Go to the next section on Subscripts and Vectors

Go back to Contents page 2.


Copyright © 2004 Sethu Subramanian All rights reserved.