0% found this document useful (0 votes)
53 views

Basic Information About STL

STL stands for Standard Template Library and is an important part of the C++ development environment. It provides ready-to-use solutions for common programming problems through containers like arrays and algorithms. The document discusses improving upon basic arrays by creating a class with dynamic size and boundary checking. It then shows how STL's vector class provides the same functionality more efficiently and as a template for any data type.

Uploaded by

Jesse Jaucian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Basic Information About STL

STL stands for Standard Template Library and is an important part of the C++ development environment. It provides ready-to-use solutions for common programming problems through containers like arrays and algorithms. The document discusses improving upon basic arrays by creating a class with dynamic size and boundary checking. It then shows how STL's vector class provides the same functionality more efficiently and as a template for any data type.

Uploaded by

Jesse Jaucian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Basic information about STL

STL stands for Standard Template Library. STL is an important part of the C++ development
environment, and if you intend to be a C++ programmer, you should certainly learn how to use it.

What is STL good for? Generally speaking, it provides ready-to-use solutions for many programming
problems. The word “standard” is also meaningful. It means that every C++ compiler is accompanied
by this software package. So, as long as you use STL, your program portability is high (portability of
C++ programs is a huge but unrelated topic).

What problems can we solve with the help of the STL? Many, but not all – otherwise we wouldn’t
need Boost libraries (free and peer-reviewed portable C++ source libraries) and all of the language
improvements.

Basically, STL can be divided into two parts:

 Containers,
 Algorithms.

There are more features in the library, and we’ll explain them later in the course – don’t worry. We
just think that containers and algorithms are most important, and that’s why we’re going to focus on
them first and most extensively.

Quick introduction to containers

Containers, or collections, as they are sometimes called, are meant to contain something inside. This
something is various kinds of data. Different types of containers store data in different manners.

This is good, because it allows programmers to choose the best collection for the task they face. We’ll talk
more about this later. The simplest container you can think of is an array, for example:

int a[10];
The array above is of type  int . An array is a container which stores elements of the same type, and usually
those elements occupy a continuous area of memory.

Each element is placed one after the other, starting from a certain location in the memory. You can access a
particular element of an array by its index using the  []  operator – the square brackets operator.

For example:

a[ 3] = 3;
cout << a[3] << endl;

An array is a very basic container. It has a few limitations:

 the size of an array cannot be changed once instantiated;


 an array does not know its size – this information has to be stored in another variable;
 as a result of the previous point, an array also cannot check if it is properly accessed – the index used
in the operator cannot be checked;
 an array (one dimension) is organized as one big memory block.

The last point is not always a limitation; it all depends on the particular array usage scenario.
Improving arrays

Let us think for a while about how to improve our array and remove some of its limitations.

What could you do?

Let’s design a class named  Array , which will be our new better array. This class will remove the
first two limitations of the original array: the inability to change the size, and the lack of knowledge of
its size. The class in its basic form might look like the one in the editor.

On the next slide, the newly created class will be put to use.

#include <exception>

#include <iostream>

class Array

int *_array;

unsigned int _size;

public:

Array (unsigned size = 0);

void add (int value);

void delItem (unsigned index);

virtual ~ Array ();

unsigned int getSize () const;

int &operator[] (unsigned index);


};

Array::Array (unsigned size):

_array (0),

_size (size)

if (size > 0)

_array = new int[this->_size];

void

Array::add (int value)

if (_size == 0)

_array = new int[1];

else

int *tmp = new int[_size + 1];

for (unsigned i = 0; i < _size; i++)

tmp[i] = _array[i];
}

delete[]_array;

_array = tmp;

_array[_size++] = value;

void

Array::delItem (unsigned index)

if (_size == 1)

delete[]_array;

_array = 0;

else

int *tmp = new int[_size - 1];

for (unsigned i = 0, j = 0; i < _size; i++, j++)

if (i == index)

j--;

continue;
}

tmp[j] = _array[i];

delete[]_array;

_array = tmp;

_size--;

unsigned int

Array::getSize () const const

return _size;

int &

Array::operator [] (unsigned index)

if (index > _size - 1)

std::exception e;

throw

e;

return _array[index];
}

Array::~Array ()

delete[]_array;

Using our improved array

The first step is to declare an object of type  Array .

Its initial size is set to  10 . The constructor allocates an array of ten elements inside object  A .

The second interesting thing is that we use this object in the same way as a regular array –  operator[]  is
called for access. Such behavior is possible because the class implements  operator[] .

The loops illustrated in the code are using the  getSize()  method to obtain the actual size of
the  Array  object. This is another improvement over a standard C++ array.

Code in the editor will output:

0 1 2 3 4 5 6 7 8 9

output







int

main ()

Array A (10);

for (unsigned i = 0; i < A.getSize (); ++i)

{
A[i] = i;

for (unsigned i = 0; i < A.getSize (); ++i)

std::cout << A[i] << " ";

std::cout << "\n";

return 0;

Showing more features

Let’s go further. You’ve probably noticed two methods which have not been discussed
yet:  add  and  delItem() . Now we’re going to put them to work. Take a look at the example in the editor.

This is the  main()  function, which we’ve seen before, with additional code. The code takes advantage of the
methods  add  and  delItem . These two methods allow our new array to grow and shrink as needed. Another
array limitation seems to have been overcome.

The last important modification is related to access control, which is provided inside  operator[] . In case a
program tries to access an element by an index which is out of scope, an exception is thrown.

The code will output:

0 1 2 3 4 5 6 7 8 9

10

11

10

output
int

main ()

Array A (10);

for (unsigned i = 0; i < A.getSize (); ++i)

A[i] = i;

for (unsigned i = 0; i < A.getSize (); ++i)

std::cout << A[i] << " ";

std::cout << "\n";

std::cout << A.getSize () << std::endl;

A.add (100);

std::cout << A.getSize () << std::endl;

A.delItem (A.getSize () - 1);

std::cout << A.getSize () << std::endl;

return 0;

Better array – STL way

Of course, the  Array  class is far from perfect. There’s a lot of other functionality that could be implemented
here. First of all, the class could be created as a template class. It would allow other types than just integer.
Secondly, some performance issues are present in the previous code. But this is just a brief introduction to the
world of containers.

Now let’s take a look at how STL fits into this picture. In order to do that, we’ll rewrite the  main()  function
using a very basic STL container – a  vector .
As you can see, the  vector  class allows for the same behavior as our handmade  Array  class. Moreover,
the  vector  is a template class and can work with basically any type of element. That is a huge advantage.

In this example, we’re using two very common  vector  methods:  push_back()  – which adds a new
element to the end of a  vector , and  pop_back()  – this one removes the last element from a  vector .

As the  Array  class, the  vector  also has  operator[]  overloaded. And finally, the method  size() ,
whose task is rather obvious. This is a  vector , in a nutshell, ladies and gentlemen. Let’s see what else can be
found inside the Standard Template Library.

With the introduction of C++11, we can use a very compact way to initialize vector elements – the initializer
list. We’ll present it here shortly; it’s just a comma-separated list enclosed in braces:

vector < int >v1 = { 5, 6, 9 };

The code from the editor will output:

0 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

10

11

10

output








  Sandbox

Code
#include <vector>
#include <iostream>
using namespace std;
int
main ()
{
vector < int >v1 (10);
vector < int >v2 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (unsigned i = 0; i < v1.size (); ++i)
{
v1[i] = i;
}
for (unsigned i = 0; i < v1.size (); ++i)
{
cout << v1[i] << " ";
cout << v2[i] << " ";
}
cout << endl;

cout << v1.size () << endl;


v1.push_back (100);
cout << v1.size () << endl;
v1.pop_back ();
cout << v1.size () << endl;
return 0;
}

#include <vector>

#include <iostream>

using namespace std;

int

main ()

vector < int >v1 (10);

vector < int >v2 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

for (unsigned i = 0; i < v1.size (); ++i)

v1[i] = i;
}

for (unsigned i = 0; i < v1.size (); ++i)

cout << v1[i] << " ";

cout << v2[i] << " ";

cout << endl;

cout << v1.size () << endl;

v1.push_back (100);

cout << v1.size () << endl;

v1.pop_back ();

You might also like