In this tutorial, we’ll look into the common functions related to arrays in C++. Arrays are one of the most powerful and frequently used data structures in programming. If you’re new to C++ and wish to learn about arrays in C++, make sure to check out our detailed tutorial here. Without further ado, let’s proceed with our learning journey. 

What are Functions?

What are functions in programming? The concept of functions in programming is not different than what we have already studied in basic mathematics. In fact, it is exactly the same. We create functions and define a certain set of rules such that they can be reused according to the need of the program. The idea is to cluster the main logic at one place and reuse that logic by calling a function instead of replicating the logic again and again. This way, our code is readable, maintainable, and free of redundancy.

List of C++ Array Methods

Now that we are clear about the concept of functions in programming, let’s move towards exploring some of the commonly used array functions in C++.

1. at

In C++, we use the at function to find out the value at any specific index in an array. If i is a specific index within the array, at(i) will give the value at index i. Note that the at function does not make a copy of the value but directly returns the reference to the value.

Usage

int sampleArray[5] = {10, 20, 30, 40, 50};
cout << sampleArray.at(2);      // Output -> 30

The above code snippet will print 30 to the console window because the second index in sampleArray points to the value of 30.

Likewise, you can access elements using the [] syntax. The above output code can be also written as:

cout << sampleArray[2];      // Output -> 30

2. front

The front function is used to access the first element in the array. The function returns the reference to the first element instead of copying it to some other memory location and then returning a copy.

Usage

string namesArray[4] = {"John", "Alex", "Peter", "Joseph"};
cout << namesArray.front();      // Output -> John

The above code will print John on the console because it is the first element in the array namesArray.

Since this function returns the reference of the first element, we can use it to modify the value in array as well.

namesArray.front() = "Emad";
cout << namesArray.front();      // Output -> Emad

Now the value which gets printed on the console window is Emad

3. back

The back function provides access to the last element in the array. The function returns a reference to the last element instead of a copy. Since it returns a reference, we can also use this function to modify the last value of the array.

Usage

string namesArray[4] = {"John", "Alex", "Peter", "Joseph"};
cout << "Value before: " << namesArray.back() << endl;      // Output -> Value before: Joseph
namesArray.back() = "Emad";
cout << "Value after: " << namesArray.back() << endl;      // Output -> Value after: Emad

Following is the output of the code snippet:

Value before: Joseph
 Value after: Emad

4. fill

The fill function is another commonly used function while programming with arrays in C++. It populates the array with the same value which we pass to it. In other words, it sets a particular value (which we provide as a parameter) at each index of the array.

Usage

int fillArray[8];
fillArray.fill(1);

The above code sets the value of each of the eight elements in the array fillArray as 1.

Using fill(1) is similar to using the following code:

int fillArray[8] = {1, 1, 1, 1, 1, 1, 1, 1};

5. size

The size function returns the total number of elements in an array. In other words, this function returns the length of an array.

Usage

int sampleArray[6] = {1, 2, 3, 4, 5, 6};
cout << sampleArray.size();      // Output -> 6

Since the length of our array sampleArray is 6, the above code prints 6 in the console window. 

6. empty

The empty function associated with C++ arrays returns true if the size of an array is 0. Otherwise, it returns false.

Usage

int firstArray[0];
int secondArray[5];
cout << firstArray.empty() << endl;      // Output -> 1
cout << secondArray.empty() << endl;      // Output -> 0

Output:

1
0

7. swap

The swap function is a very interesting function that can be used in a variety of applications in practical programming. It works on two arrays and primarily serves the purpose of element-wise swapping of each value in the first array with the value in the second array. Note that, in order to use the swap function, both the arrays must be of the same size

Usage

Following is an example that demonstrates the usage of swap function in an array.

int firstArray[5] = {0, 2, 4, 6, 8};
int secondArray[5] = {1, 3, 5, 7, 9};
firstArray.swap(secondArray);

The above code snippet will swap the values in firstArray with the values in secondArray. So, the values in firstArray will now be 1, 3, 5, 7, 9 while the values in secondArray will be 0, 2, 4, 6, 8.

Other C++ Array Functions

Function Description
begin Returns an iterator to the beginning of an array
end Returns an iterator to the end of an array
rbegin Reverse iterator pointing to the end of an array
rend Reverse iterator pointing to the beginning of an array
cbegin Constant iterator pointing to the beginning of an array
cend Constant iterator pointing to the end of an array
crbegin Constant reverse iterator pointing to the end of an array
crend Constant reverse iterator pointing to the beginning of an array
max_size Returns the maximum size of an array
get<i>(array) Returns a reference to the value of index i in an array

Wrapping Up

These are some of the most important array functions which are frequently in C++. If you wish to learn more about C++, you can check out our collection of C++ tutorials.

Feel free to write in the comment section in case of any confusion or queries.