Using the STL Library in C++, part 2

Hi again!

Today something about advantages of using STL to create dynamic arrays. It is more commonly used, than normal dynamic arrays, because it is more readable.

Example:

#include <iostream>
#include <vector> //it is an inseparable directive if we want to use STL for dynamic arrays

using namespace std;

int main()

{
vector <float> myDynamicArray; // we create our array

//then we can: (1) put our float type numbers into an array in sequence or (2)we can initialize it with one variable

//(1) - there are 2 differnet ways to make it - first of all we will use better (in my opinion) - the push_back method:

myDynamicArray.push_back(1);

myDynamicArray.push_back(13);

//etc.

// We can see that we don't have to set an array size, so we can put as many values as we want, whereas in the second method we need to define size of our array:

//(2) method

vector <float> myDynamicArray(10);

myDynamicArray [0] = 1;

myDynamicArray [1] = 13;

//etc. till [9]

return 0;

}

Without using STL we have to do this:

float *myDynamicArray = new float [10];

and then we have to delete it from a program area:

delete [] = myDynamicArray;

MJ

Zostaw komentarz

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s