Javatpoint Logo
Javatpoint Logo

std::array::crbegin in C++

The std::array::crbegin function in C++ is a member function of the std::array class template, which is part of the Standard Template Library (STL). This function is used to obtain a reverse iterator pointing to the last element of the std::array. In other words, it is used to create a constant reverse iterator that allows iterating over the elements of the array in reverse order.

Overview of std::array:

The std::array is a fixed-size array containing introduced in C++11. It provides a safer and more convenient alternative to traditional arrays by encapsulating the array size within the type. The size of a std::array is known at compile time, and it cannot be changed during runtime.

Reverse Iterators in C++:

Reverse iterators, denoted by the prefix 'r', are a type of iterator that moves backward through a container. They are particularly useful when you need to traverse a container in reverse order. C++ provides rbegin() and rend() member functions for obtaining reverse iterators.

Syntax and Parameters:

The syntax std::array::const_reverse_iterator std::array::crbegin() const noexcept; tells us that crbegin is a member function of the std::array class, returning a constant reverse iterator pointing to the last element of the array. It does not modify the state of the array, and it is declared not to throw exceptions (noexcept).

std::array: This part indicates the type of the array. T is the type of elements stored in the array, and N is the size of the array. You need to replace T and N with the actual type and size when using the function.

const_reverse_iterator: It is the type of the iterator returned by the crbegin function. It is a constant reverse iterator, denoted by const, meaning that the elements it points to cannot be modified through this iterator. It allows backward traversal of the array.

std::array::crbegin(): It is the function call. It is a member function of the std::array class. It returns a constant reverse iterator pointing to the last element of the array.

const: The const qualifier after the function indicates that the crbegin function does not modify the state of the std::array instance on which it is called. It is important for cases where you want to use the function on constant arrays or in const-correct code.

noexcept: This specifier indicates that the crbegin function is declared not to throw exceptions. It is a part of the function's signature and provides information to the compiler and other developers about the function's exception-safety guarantee.

Program:

Let's take an example to illustrate the std::array::crbegin function in C++.

Output:

Original Array: 2 4 6 8 10 12 14 
Reversed Array: 14 12 10 8 6 4 2 
Sum of Array Elements: 56
Product of Array Elements: 645120
Minimum Element: 2
Maximum Element: 14

Explanation:

  • This C++ program demonstrates the use of std::array, templates, iterators, and algorithms to perform various operations on an array, including printing, reversing, calculating the sum and product, and finding the minimum and maximum elements.
  • The iostream header is included for input and output operations. The array header is included for using the std::array container.
  • The algorithm header is included for using functions like std::min_element and std::max_element.
  • These are template functions to calculate the sum and product of elements in a std::array.
  • The calculateSum and calculateProduct functions take a std::array by reference and return the calculated sum and product respectively. The main function, where the program execution begins.
  • Declares a std::array named myArray with a size of 7 and initializes it with values. Prints the original array using a range-based for a loop.
  • Obtains a const reverse iterator to the last element and prints the array in reverse order using the reverse iterator.
  • Calls the template functions calculateSum and calculateProduct to calculate and print the sum and product of array elements.
  • Uses std::min_element and std::max_element from the header to find and print the minimum and maximum elements in the array. Indicates a successful execution of the program.

Complexity Analysis:

Time Complexity:

Printing the Original Array:

The loop that prints the original array has a time complexity of O(N), where N is the size of the array. It is because the loop iterates through each element of the array once.

Reversed Array Printing:

The loop that prints the array in reverse order also has a time complexity of O(N). Like the first loop, it iterates through each element of the array once.

Sum and Product Calculations:

Both the calculateSum and calculateProduct functions iterate through the entire array once, resulting in a time complexity of O(N) for each function.

Finding Minimum and Maximum Elements:

The functions std::min_element and std::max_element iterate through the array to find the minimum and maximum elements, respectively. Both operations have a time complexity of O(N).

The total time complexity of the code is dominated by the linear operations (O(N)) involved in iterating through the array for printing, calculations, and finding minimum/maximum elements.

Space Complexity:

Original Array:

The space complexity for storing the original array is O(N), where N is the size of the array. It is because the memory required to store the array is directly proportional to its size.

Reverse Iterator:

The reverse iterator (reverseIter) requires constant space (O(1)). It doesn't depend on the size of the array but rather on the implementation of the iterator.

Sum and Product Variables:

The variables used to store the sum and product (sum and product) are constant in space (O(1)). Regardless of the array size, these variables only need a fixed amount of memory.

Minimum and Maximum Element Variables:

The variables used to store the minimum and maximum elements (minElement and maxElement) are also constant in space (O(1)). They only need a fixed amount of memory.

The total space complexity is O(N), primarily due to the space required to store the original array. The additional variables and iterators used in the code have constant space complexity, contributing minimally to the overall space complexity.

Advantages of the std::array::crbegin:

There are several advantages of the std::array::crbegin function. Some main advantages of the std::array::crbegin are as follows:

Read-Only Iteration:

The crbegin function returns a constant reverse iterator, allowing read-only access to the elements of the array. It is beneficial when you need to traverse the array in reverse without modifying its contents.

Const-Correctness:

The crbegin function enforces const-correctness by providing a const iterator. It helps in writing code that adheres to the principles of not modifying data unintentionally.

Range-Based for Loop Compatibility:

The crbegin function is compatible with range-based for loops, making it easy to iterate over the array in reverse using a clean and concise syntax.

Disadvantages of the std::array::crbegin:

There are several disadvantages of the std::array::crbegin function. Some main disadvantages of the std::array::crbegin are as follows:

Limited Mutability:

Since crbegin returns a constant iterator; it restricts the ability to modify array elements through this iterator. If modification is required, a non-const iterator (rbegin) or other methods may be more appropriate.

No Direct Modification:

It does not provide a direct way to modify the elements during iteration. If modification is necessary, an alternative iterator or loop mechanism may be preferred.

Compatibility Limitation:

While it is useful in scenarios where read-only access is sufficient, it may not be the best choice when the algorithm requires modification of array elements during reverse iteration.

Not Applicable to Other Containers:

The crbegin function is specific to std::array and is not applicable to other container types like std::vector or std::list. Different containers may have different methods for obtaining reverse iterators.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA