C++ Collections

Share:
Collections are collection of variables with same data type and name.
we can iterate through collections to gain access to the inside elements of that, for that we need to use brackets ( [ ] ) after the name of collection which will hold the element index (position) in that collection and remember that index system is zero based that means first element index is zero (0) and last element index is n-1 (sizeOfCollection-1). Of course, after accessing an element we can assign that to a deferent value.


Here we will cover three kinds of collection in C++ which will solve our problems:

§  String
We already know which string is collection of characters. Here we will learn how to access each character in a string.
Syntax of strings:

                string StrName = “Value”;           // String declaration
                StrName[index];                           // Accessing string elements
                StrName[index] = ‘Value’;         // Assigning values to string elements

When we accessed to a string element that is a character so if we want to change the value of an element in a string we should assign that to a character value.

§  Array
Arrays are collection of variables which have same name and same data type and they stored in memory back to back this helps us to access next element just by adding one to the address of the first element. In the array name we will find out where the address of first element of array is stored.
Syntax of arrays:

                DataType ArrName[ArrSize] { Value , … };             // Array declaration

Data Type:
Data type in arrays can be one of the data types which we covered earlier in the variable data types (int , float , char …).

Array Name:
Array name rules are just like variable name rules which we discussed about it in variables section. we must remember that array name dose not return a value but it returns the address of the first element in array.

Array Size:
Array size shows how many variable the array can store we can use a variable instead of a constant value but once we placed the array size int its place we cannot change it on runtime.

Value:
Array values all should be the same data type as our array is. If we initialized an array in the first we can leave the array size empty, compiler will figure out the size of array based on values:

                DataType ArrName[] { Value , … };           // initializing array without size

but when we did not initialize an array the array size is not optional anymore and we can’t just leave it empty:

                DataType ArrName[ArrSize];     // uninitialized arrays

Accessing array elements:
We can access array elements by their index ( position ) inside array remember that indexes are zero based that means they starts from one and ends until n-1 we must write indexes inside brackets ( [ ] ) which comes right after array name:

                ArrName[index];

we can also change the value of each element after we gained access to it:

                ArrName[index] = Value;

Multi-dimensional array:
We can create multi-dimensional arrays just by adding a second size for it :
                DataType ArrName [size]…[size] = { { value , … } , … { value , … } };

§  Vector
Vectors are collection of variables, objects or other vectors which have same name and type the main deference of vectors with arrays is this that we can change the size of vectors on run time it makes vectors so flexible but in order to use vectors in C++ we need to first include it with the following header #include<vector>            and        using namespace std;

Vector structure in C++:

                vector<Datatype> VecName { Value , … };

vector:
we must use vector key word before we create a vector without that compiler will not understand which that is a vector.

DataType:
we already learned about data types in variable article we can use one of it for vectors but it must be inside ( < > ) symbols right after vector key word by this way we saying for compiler that this vector will store  a collection of variables with that data type.

VecName:
we can use the rules of variables name here too. With this name we can access the vector elements later.

Value:
Vector values all should be the same data type of the vector.
§  Adding values to vector on run time:
we can add values to the end of vector using the below command:

                VecName.push_back(value);

§  Removing values from vector:
We can remove the last element of the vector like below:

                VecName.pop_back();

§  Erasing vector:
We can delete all elements of vector using the below command:

                VecName.clear();

§  Size of vector
We can see the vector size with below command it is super useful with loops:

                VecName.size();

§  Accessing elements:
We can access vector elements in two styles:

                VecName[index];            or            VecName.at(index);

§  Assigning values to vector elements:
We can assign values for each element of vector after we access that again we can do it by two ways:

                VecName[index] = Value;            or            VecName.at(index) = Value;

                Vector of vectors:

                                vector<vector<DataType>> VecName { value , … };

                vector of objects:

                               vector <ClassName> VecName { value , … };

Note: Vector is part of STL ( Standard template library ) , here we just saw a few functionalities of it but vector have more functionalities.

No comments