
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Array Class in C++
Array Class
In C++, the array class is part of the standard library and is known for its fixed size. The C++ array class, introduced in C++11, offers a better alternative to C-style arrays.
The following are the advantages of the array class over a C-style array:
- Array class knows its size, whereas a C-style array does not have its size. So when passing to functions, we don't need to pass the size of the array as a separate parameter.
- C-style array there is more risk of array being decayed into a pointer. Array classes don't decay into pointers
- C-style arrays (such as int arr[5]) may easily transform into pointers, losing their size information. When passing to functions. Unexpected behaviour may result from this. Array classes (such as std::array<int, 5>) protect against unexpected pointer decay by maintaining their size and structure. They are safer and have built-in features.
- Compared to C-style arrays, array classes are typically reliable, lightweight, and efficient.
Operation on Array
The following are functions to operate on an array:
- size(): To return the size of array i.e. returns the no of elements of the array.
- max_size(): To return maximum number of elements of the array.
- front(): To return front element of the array.
- back(): To return last element of the array.
- empty(): Returns true if array size is true otherwise false.
- fill(): To fill the entire array with a particular value.
- swap(): To swap the elements of one array to another.
Example to Get The Size of an Array
The following C++ example gives the size of an array using the size method:
#include <iostream> using namespace std; int main() { int arr[] = {1, 2, 3, 4, 5, 6}; // use the size method int len = size(arr); cout << "Size of an Array: "<<len; return 0; }
Following is the output of the code:
Size of an Array: 6
Example to Get Front and Back Elements
This is C++ code to demonstrate working of front() and back() methods of the array class:
#include<iostream> #include<array> using namespace std; int main() { array<int,6> arr = {1, 2, 3, 4, 5, 6}; // Display first element of array cout << "First element of array is : "; int &first=arr.front(); cout <<first<< endl; // Display last element of array cout << "Last element of array is : "; int &last=arr.back(); cout << last << endl; return 0; }
Following is the output:
First element of array is : 1 Last element of array is : 6
Example to Return The Size and Max_Size
The following example demonstrates the working of the size() and the max_size() methods:
#include<iostream> #include<array> using namespace std; int main() { array<int,5> arr = {1, 2, 3, 4, 5}; cout << "Size of an array: "; cout <<arr.size()<< endl; cout << "maximum elements array can store: "; cout << arr.max_size() << endl; return 0; }
Following is the output of the above code:
Size of an array: 5 maximum elements array can store: 5
Advertisements