This not a cheat sheet nor it is a tutorial, it’s a mixture of both, in a friendly way. It will review some concept of C++ without explaining them like if you would have never wrote any code.

Please note that it’s also a reference where I put all my explorations, reading and whatever related to c++.

Arrays and string

What is an array: It’s a like a collection of elements of the same type.

What an array look like: It looks like a group of same items, such as integer.

Array declaration:

Declare a 12 size array with basic value 0.

int main()
{

	int simpleShitArray[12] = { 0 };
	return 0;
}

Array of char declaration:

char myName[5];

Array declaration basic:

type nom[nb de element] = {les valeurs}

We can see it like:

	int arrayInt[2] = { 69 };
	int arrayInt2[2] = { 69, 68 };
	float arrayFloat[2] = { 69.0f};
	float arrayFloat2[2] = { 69.0f, 68.0f};

Array per element declaration:

Let’s declare a simple array containing the number of banana I eated every day.

int MyDailyBananaEating[5] = { 2, 12, 23, 1, 0 };

Array per element declaration for only the N first:

While it’s awesome to declare predfined value for the 5 days of the week, what about changing only the first 3 value of an array? We can do this using this at decleration.,

	
	int ThreeFirstDayBanana[5] = { 1, 123, 24398587};

Declare Constant array size:

	const int arrayLength = 5;
	int MyNumbers[arrayLength] = { 34, 56, -21, 5002, 365 };

Memory reserved by the compiler for an array?:

An array of size 12 of int is reserved by the compiler using this simple formula.

sizeof(int) * 12 

The bytes thats an array use is:

sizeof(element-type) * Number of Elements in the array

Accessing array data?:

One of the question we might ask ourselves is how-the-fuck do we actually access items in the array we created?

To get the value of a certain item in the array we use the position of the value in the array.

	int anArray[2] = { 69, 68 };
	int firstItem = anArray[0]; // 69

How does the compiler handles access to that value?:

To access a value of an array, the compiler will start by the 0 item of the array, the compiler will use the address of the first item in the array, then it will process the offset to reach that indexed value using:

N*sizeof(element)

The compiler does not give a freaking-ish about the size of the array, so please note that you can call value of indexes that are more than the array size:(This is shit)

When asked to access element at index N, the compiler uses the memory address of the first element (positioned at index zero) as the starting point and then skips N elements by adding the offset computed as N*sizeof(element) to reach the address containing the (N+1)th element. The C++ compiler does not check if the index is within the actual defined bounds of the array. You can try fetching the element at index 1001 in an array of only 10 elements, putting the security and stability of your program at risk.

Multidimension array ?:

To declare a multi-dimension array and to access element from it:

#include <iostream>

int main()
{
	/* Multi Dimension array*/
	int Mat4x4[4][4] =
	{
		{0, 1, 2, 3},
		{4, 5, 6, 7},
		{8, 9, 10, 11},
		{12, 13, 14, 15}
	};

	std::cout << Mat4x4[0][0]; // show 0
	std::cout << Mat4x4[1][0]; // show 4
	std::cout << Mat4x4[2][0]; // show 8
	return 0;
}

Dynamic array and std::vector overview ?:

Arrays store data in contiguous way, so when we declare a basic array we make a fixed size memory allocation, then we can play around with this chunk of memory, change some value and so on.

Let’s assume I have a bag that can only contain 12 records… I go to the record store and put 12 records in my bad. Not let’s assume I want to switch one record for another, we can do this with array, no problem.

But what about dynamic array? Let’s start with a basic problem:

I want more than 12 records, as along the record-digging sessions, I realized that I need something like 23 records. That would not work using array, because I can only store them in my bag(the array of size 12).

This is where comes std::vector or dynamic arrays!

Let start with a simple 3 size bag and try to push one item into it:

#include <iostream>

int main()
{
	// Bag only can contain 3 vynils
	int myRecordsBag[3] = { 1,2,43};
	// We can play around them
	myRecordsBag[1] = 4377;
	myRecordsBag[0] = myRecordsBag[1];

	// But we cannot add one :( 
	myRecordsBag[4] = 12;

	return 0;
}

This would give us that error…

We need a way to store data dynamically into a bag that can grow and resize. Here comes our good friend: The std::vector.

Let’s start with the very basic idea of pushing more data with the same kind of writting as if we were dealing with an array.

#include <iostream>
#include <vector>
int main()
{
	
	std::vector<int> recordBag(3);

	recordBag[0] = 69;
	recordBag[1] = 834;
	recordBag[2] = 1269;
	recordBag[3] = 43523;


	return 0;
}

Bruh we fuck, let’s see how to use std::vector and add element to them.

Add item to std::vector ?:

#include <iostream>
#include <vector>
int main()
{
	
	std::vector<int> recordBag(3);

	recordBag[0] = 69;
	recordBag[1] = 834;
	recordBag[2] = 1269;

	/*
	*	Break the shit
		recordBag[3] = 43523;
	*/

	// Works
	recordBag.push_back(2222);


	return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *