Arrays


    

Table of Contents

  1. Arrays
  2. Structures
  3. Strings

Arrays

Arrays are used to store multiple values in a single variable.

Define an Array

Syntax1 : array_type array_name[] = {content1, content2, content3};

Example:

int myNumber[] = {1,2,3,4,5};

Syntax2: array_type array_name[array_size];

Example:

int yourNumber[6];

You can also specify the size and the elements of the array at once when declaring or defining an array.



        
Access and Set the Values of Elements of an Array

To access an array's element, we need to refer to its index number. Index number 0 is for the first element, index number 1 is for the second element, and so on.

Syntax: array_name[index_number] = new_value;

Example:

myNumber[0] = 10;

If the array's elements are still not defined like in the yourNumber array in syntax2 in defining array, then the system will automatically set the values according to the new value.

Example:

#include <iostream> int main() { int myNumber[] = {25, 50, 75, 100}; int yourNumber[4]; //yourNumber doesn't contain any value //you can't change the size of the array after creation myNumber[0] = 30; //change the value 25 to 30 //myNumber will contain 30,50,75,100 now yourNumber[0] = 40; yourNumber[1] = 50; yourNumber[2] = 45; yourNumber[3] = 55; //yourNumber will contain 40,50,45,55 now return 0; }


        
Loop through an Array

You can loop through the array elements with the for loop. for loop will be explained in the conditional and loops page.

Example:

#include <iostream> int main() { int myNumbers[] = {25, 50, 75, 100}; int i; for (i = 0; i < 4; i++) { std::cout << myNumbers[i] << std::endl; } return 0; /* The output will be 25 50 75 100 */ }

In C++ 2011, there is also a new kind of for loops for array.
Syntax :

for (variable_type random_variable_name : array_name){ //code block }

For example, the for loop in previous example,

for (i = 0; i < 4; i++) { std::cout << myNumbers[i] << std::endl; }

Can be simplified to this and they still have the same output.

for (int i : myNumbers) { std::cout << i << std::endl; }


        

We can also use the sizeof() function to return the size of an array in byte, so we have to divide the sizeof(array) with sizeof(array_type) to know how many elements that exist in the array. If you want to use easier method, you can directly use std::size(array) to know the amount of the elements in an array.

#include <iostream> int main() { int myNumbers[5] = {20, 30, 40, 50, 60}; int byte_size = sizeof(myNumbers); //value is 20 int array_size = sizeof(myNumbers)/sizeof(int); //value is 5 int array_size2 = std::size(myNumbers); //value is 5 return 0; }

Structures

Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. The variables in struct can have several different data types.

To access the structure, you must create a variable of it. Use the struct keyword and build your structure inside the { } and then followed by the structure's variable names that you want to use in your program.

Syntax:

int main(){ struct { Variable_type1 variable_name1; Variable_type2 variable_name2; Variable_typen variable_namen; }struct_variable_name1, struct_variable_name2, struct_variable_namen; }

        

You can also declare the structure outside the main function and give it a name, so it can be treated as a datatype.

Syntax:

struct struct_name{ Variable_type1 variable_name1; Variable_type2 variable_name2; Variable_typen variable_namen; }; int main(){ struct_name struct_variable_name; }

To access the members of a structure, you have to use dot (.) in int main() after you declare the name of the structure variable.
Syntax: struct_variable_name.variable_name1 = value;

We can also assign values to multiple variables at once with this syntax.
Syntax: struct_variable_name = {value1, value2, …, valuen};

We can also assign the values of a structure variable to another structure variable.

Syntax:

struct_name structure_variable_name1 = {value1, value2, …, valuen}; struct_name structure_variable_name2; structure_variable_name2 = structure_variable_name1;

Example code:

#include <iostream.h> #include <string.h> struct Phone { int price; std::string brand; int year; }; int main() { struct Phone phone1 = {5000, "Aster", 2019}; struct Phone phone2 = {3000, "Bianca", 2022}; struct Phone phone3 = {2000, "Charlie", 2021}; struct Phone phone4; phone4 = phone3; //assign the value of phone3 to phone4 phone4.year = 2022; //change phone4's year to 2022 std::cout<< phone1.price<< " " << phone1.brand << " " << phone1.year; std::cout<< "\n" << phone2.price<< " " << phone2.brand << " " << phone2.year; std::cout<< "\n" << phone3.price<< " " << phone3.brand << " " << phone3.year; std::cout<< "\n" << phone4.price<< " " << phone4.brand << " " << phone4.year; return 0; }

Output:

5000 Aster 2019 3000 Bianca 2022 2000 Charlie 2021 2000 Charlie 2022

Strings

Unlike C, C++ have a string type variable and we can operate C++ string like how we operate other types of variable.

Concatenation

C++ uses the operator + to concatenate strings. Another way is to use append() function. The different is append can only take one string or parameter at a time.

Syntax:

variable_result = string1 + string2 + stringn; variable_result = string1.append(string2);

Access String Elements

String in C++ can be accessed just like an array by using the index number. We can then change the value of an element of the string.
Syntax: string1[index_number] = value;


Length of the String

To know a string length, we can use size() function or length() function.
Syntax: string1.size() or string1.length()

Example code:

#include <iostream> #include <string> int main(){ std::string string1= "hello"; std::string string2 = "world!"; std::string concat1 = string1 + " " + string2; std::string concat2 = string1.append(string2); concat2[1] = 'a'; //change e in hello to a std::cout<< concat1 << " has a length of " << concat1.size() << std::endl; std::cout<< concat2 << " has a length of " << concat2.length() << std::endl; return 0; }


        

Output:

hello world! has a length of 12 halloworld! has a length of 11