C++11 : std::array Tutorial and examples

std::array<> is introduced in c++11 and it’s a wrapper around old C style array, with added advantages.
It’s is a kind of sequential container with constant size elements.

std::array is internally defined as class template i.e.

template < class T, size_t N > 
class array;

Here first template parameter T is the type of elements to be stored in array and 2nd template parameter i.e.
size_t N is a constant that represents the number of elements in array.

Header File required for std::array i.e.

#include <array>

Defining and Initializing an std::array<> object

std::array<int, 10> arr;

Here, std::array object arr represents an int array of fixed size 10 and its uninitialized, hence all 10 elements contains garbage value.

std::array<std::string, 200> arr1;

Here, std::array object arr1 represents an string array of fixed size 200.

// First 2 values will be initialized and others will be 0.
std::array<int, 10> arr3 = { 34, 45 };

If we provide less number of elements during initialization, remaining will be initialized with default value. Like in case of int its 0. So, arr3 contains,

34 , 45 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,

std::array also provides a member function fill() to set the same value for all elements in one go. Its easy when we have large array.

std::array<int, 10> arr4;
// Fill all elements in array with same value
arr4.fill(4);

So, arr4 contains,

4,4,4,4,4,4,4,4,4,4

Complete example is as follows,

#include <iostream>
#include <array>

void printArray(std::array<int, 10> & arr)
{
	// Printing array
	for (auto & elem : arr)
		std::cout << elem << " , ";
	std::cout << std::endl;
}
int main()
{
	// Uninitialized array object contains elements with
	// garbage values
	std::array<int, 10> arr1;

	printArray(arr1);

	// Initialized array object
	std::array<int, 10> arr2 ={ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

	printArray(arr2);

	// First 2 values will be initialized and others will be 0.
	std::array<int, 10> arr3 = 	{ 34, 45 };

	printArray(arr3);

	std::array<int, 10> arr4;
	// Fill all elements in array with same value
	arr4.fill(4);

	printArray(arr4);

	return 0;
}

Output:

255767104 , 32652 , 255767104 , 32652 , 0 , 0 , 6 , 0 , 6299760 , 0 , 
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 
34 , 45 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 
4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 , 4 ,

How to get the size of std::array

Size of a std::array object is always constant i.e. its 2nd template parameter. However, std::array provides a member function size() to return the size i.e.

// Returns the constant size
arr.size();

How to access elements in std::array

There are 3 ways to access elements in std::array

// Creating and initializing an array of size 10.
std::array<int , 10> arr = {1,2,3,4,5,6,7,8,9,10};

operator []: Accessing element in std::array using operator []

int x = arr[2];

Accessing out of range elements using [] operator will lead to undefined behaviour.

at() : Accessing element in std::array using at() member function

// Accessing element using at() function
int x = arr.at(2);

Accessing any out of range element using at() function will throw out_of_range exception.

std::tuple’s get<>()

int x = std::get<2>(arr);

Accessing out of range elements using get<> operator will give compile time error.

Complete example is as follows,

#include <iostream>
#include <array>

int main()
{
	// Creating and initializing an array of size 10.
	std::array<int , 10> arr = {1,2,3,4,5,6,7,8,9,10};

	// Random access operator [] to fetch any element from array
	int x = arr[2];

	std::cout<<"x = "<< x <<std::endl;

	// Accessing out of range elements using [] leads to undefined behaviour
	// i.e. it can give garbage value or crash
	x = arr[12];

	std::cout<<"x = "<< x <<std::endl;

	// Accessing element using at() function
	x = arr.at(2);

	std::cout<<"x = "<< x <<std::endl;

	// Accessing out of range elements using at() will throw exception
	try
	{
		x = arr.at(12);
	}
	catch (const std::out_of_range& exp)
	{
	    std::cerr << exp.what() << std::endl;
	}

	// Accessing elements from std::array object using std::get<>()
	x = std::get<2>(arr);

	// Accessing out of range elements using std::get<>() will throw error at compile time
	//x = std::get<12>(arr);

	std::cout<<"x = "<< x <<std::endl;

	return 0;
}

Output:

x = 3
x = -927490112
x = 3
array::at: __n (which is 12) >= _Nm (which is 10)
x = 3

How to Iterate over a std::array<> object

In the below example we will see 4 different ways to iterate over an array

  • Iterate using Range based for loop
  • Iterate using for loop
  • Iterate using Iterator
  • Iterate using for_each

Complete example is as follows,

#include <iostream>
#include <array>
#include <algorithm>

int main()
{

	// Initialized array object contains elements with
	std::array<int, 10> arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

	// Iterate over std::array using range based for loop
	for (auto & elem : arr)
	{
		std::cout << elem << " , ";
	}
	std::cout << std::endl;

	// Iterate over std::array using for loop and operator []
	for (int i = 0; i < arr.size(); i++)
	{
		std::cout << arr[i] << " , ";
	}
	std::cout << std::endl;

	// Iterate over std::array using for Iterators
	auto it = arr.begin();
	while(it != arr.end())
	{
		std::cout << *it << " , ";
		it++;
	}
	std::cout << std::endl;

	// Iterate over std::array using for_each STL algorithm
	std::for_each(arr.begin(), arr.end(), [](const int & elem){
		std::cout << elem << " , ";
	});
	std::cout << std::endl;
	return 0;
}

Output:

1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 
1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ,

 

 

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top