Array and vector in C++

Array and vector in C++

ยท

4 min read

Variables

In real life, we need a container to hold or store something. Suppose we want to hold one kg of apples then we need some kind of container, the container can be translated as variable since it can store one kg of any fruit.

apple box.png

Later when we want the apple we will look for the box where it was stored.

Similarly to store any value(nums/char) inside a computer we need a container (free memory inside RAM) or variable in technical words.

ram.png

Let's code

Suppose we want to store my age, we will write in c/c++ as below

int age = 20;
//  age is variable here

age inside memory.png While executing this code computer will find free and available space then it will label that space as age and then 20 will be placed there. Later when we will need the value of age, the computer will fetch it from memory where it was stored (computer finds the stored value by symbol table).

Arrays

We have discussed so far about single value, what if we need to store a group of values, like age of 5 people together, we may write

//  5 different variables of same type, int
int age1 = 20;
int age2 = 21;
int age3 = 24;
int age4 = 37;
int age5 = 40;

We achieved our goal above but, we have to declare 5 different variables. Imagine the extra lines of code when we need 100s of age. To solve this problem we need arrays. Array is a collection of variables of the same type, it is contiguous in the memory means all the box needs to be side-by-side.

(In statically typed language like c/c++ group of values should be of the same type, although we can group different types of values using struct)

Now to store the age of 5 people we can use an array since all ages are of the same types, int. We will write

int age[5];    //  5 int type boxs will be created side-by-side
age[0] = 20;  // [i] access the (i+1)th box
age[1] = 21;
age[2] = 24;
age[3] = 37;
age[4] = 40;
//  In computer indexing starts from 0, so five box will range 0 to 4

//  Accessing the value
cout<<age[3];  // 37 will be printed

array.png

Vector

We have seen how to declare arrays, assign values, and access them. But we need to tell the size of the array at the time of declaration. What if we need to increase the array size while the program is running, we need DMA, Dynamic Memory Allocation to achieve so.

vector does DMA for us and also provides a lot of properties and functions, which makes it easier to work with arrays. Some e.g.

  • resize the array as necessary
  • keeps the count of array elements
  • fun to check if it is empty?

vector is nothing but syntactic sugar coat over arrays. It is a standard library by c++.

#include <vector> // header file to use vector

vector<int> age; // think similar as int age[]
//  age is variable name
//  <int>tells what type of value will be stored
//  vector keyword makes age an array
//  <int> called as generics or template
age.push_back(20); //   push_back function adds a value at end of the array
age.push_back(21);

cout << age[0];    // 20, we can access the value with square bracket notation
cout << age.at(1);  //  21,  stl(std lib) way to access the value

vector<char> vowels{'a', 'e', 'i', 'o', 'u'};  //   can be intianlised this way also
cout<<vowels[1];  // e

vector of strings

Here is the final example of the vector of strings. We will store names and then gonna print each name.

#include <iostream>
#include <string>  // to use string
#include <vector>

using namespace std;

int main()
{
    vector<string> names{"Jhon", "Peter", "Patrik"};
    names.push_back("onkesh");
    for (string name : names)
    {
        cout << name << endl;
    }
    return 0;
}

Congrats ๐ŸŽŠ, you learned variables and array(vector).

ย