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

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj /  Zmień )

Zdjęcie na Google+

Komentujesz korzystając z konta Google+. Wyloguj /  Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj /  Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj /  Zmień )

w

Connecting to %s