Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++
  • Standard Template Library
  • STL Vector
  • STL List
  • STL Set
  • STL Map
  • STL Stack
  • STL Queue
  • STL Priority Queue
  • STL Interview Questions
  • STL Cheatsheet
  • C++ Templates
  • C++ Functors
  • C++ Iterators
Open In App
Next Article:
array::begin() and array::end() in C++ STL
Next article icon

STD::array in C++

Last Updated : 09 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. 
In order to utilize arrays, we need to include the array header: 
 

 #include <array> 

Let’s see an example.  
 

CPP




// CPP program to demonstrate working of array
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <string>
using namespace std;
 
int main() {
 
  // construction uses aggregate initialization
  // double-braces required
  array<int, 5> ar1{{3, 4, 5, 1, 2}};
  array<int, 5> ar2 = {1, 2, 3, 4, 5};
  array<string, 2> ar3 = {{string("a"), "b"}};
 
  cout << "Sizes of arrays are" << endl;
  cout << ar1.size() << endl;
  cout << ar2.size() << endl;
  cout << ar3.size() << endl;
   
  cout << "\nInitial ar1 : ";
  for (auto i : ar1)
    cout << i << ' ';
 
  // container operations are supported
  sort(ar1.begin(), ar1.end());
 
  cout << "\nsorted ar1 : ";
  for (auto i : ar1)
    cout << i << ' ';
 
  // Filling ar2 with 10
  ar2.fill(10);
 
  cout << "\nFilled ar2 : ";
  for (auto i : ar2)
    cout << i << ' ';
 
 
  // ranged for loop is supported
  cout << "\nar3 : ";
  for (auto &s : ar3)
    cout << s << ' ';
 
  return 0;
}
 
 
Output: 
Sizes of arrays are 5 5 2  Initial ar1 : 3 4 5 1 2  sorted ar1 : 1 2 3 4 5  Filled ar2 : 10 10 10 10 10  ar3 : a b

 

This C++ STL array is a kind of sequential container and is not used extremely in regular programming or in competitive programming but sometimes its member function provides an upper edge to it over the regular normal array that we use in our daily life. So, we are discussing some of the important member function that is used with such kind of array:

Member Functions for Array Template are as follows:

Syntax:                    array<object_type, arr_size> arr_name;

a) [ ] Operator : This is similar to the normal array, we use it to access the element store at index ‘i’ .

Ex:   

C++




#include <iostream>
#include <array>
using namespace std;
 
int main() {
    array <char , 3> arr={'G','f','G'};
    cout<<arr[0] <<" "<<arr[2];
    return 0;
}
 
 
Output
G G

b) front( ) and back( ) function: These methods are used to access the first and the last element of the array directly.

C++




#include <iostream>
#include <array>
using namespace std;
 
int main() {
    array <int , 3> arr={'G','f','G'};  // ASCII val of 'G' =71
    cout<<arr.front() <<" "<<arr.back();
    return 0;
}
 
 
Output
71 71

c) swap( ) function: This swap function is used to swap the content of the two arrays.

Ex: 

C++




#include <iostream>
#include <array>
using namespace std;
 
int main() {
    array <int , 3> arr={'G','f','G'};  // ASCII val of 'G' =71
    array <int , 3> arr1={'M','M','P'}; // ASCII val of 'M' = 77 and 'P' = 80
    arr.swap(arr1);  // now arr = {M,M,P}
    cout<<arr.front() <<" "<<arr.back();
    return 0;
}
 
 
Output
77 80

d) empty( ) function: This function is used to check whether the declared STL array is empty or not, if it is empty then it returns true else false.

Ex: 

C++




#include <iostream>
#include <array>
using namespace std;
 
int main() {
    array <int , 3> arr={'G','f','G'};  // ASCII val of 'G' =71
    array <int , 3> arr1={'M','M','P'}; // ASCII val of 'M' = 77 and 'P' = 80
    bool x = arr.empty(); // false ( not empty)
    cout<<boolalpha<<(x);
    return 0;
}
 
 
Output
false

e) at( ) function: This function is used to access the element stored at a specific location, if we try to access the element which is out of bounds of the array size then it throws an exception. 

Ex: 

C++




#include <iostream>
#include <array>
using namespace std;
 
int main() {
    array <int , 3> arr={'G','f','G'};  // ASCII val of 'G' =71
    array <int , 3> arr1={'M','M','P'}; // ASCII val of 'M' = 77 and 'P' = 80
    cout<< arr.at(2) <<" " << arr1.at(2);
    //cout<< arr.at(3); // exception{Abort signal from abort(3) (SIGABRT)}
    return 0;
}
 
 
Output
71 80

f) fill( ) function: This is specially used to initialize or fill all the indexes of the array with a similar value.

Ex:

C++




#include <iostream>
#include <array>
using namespace std;
 
int main() {
    array <int , 5> arr;
    arr.fill(1);
    for(int i: arr)
       cout<<arr[i]<<" ";
    return 0;
}
 
 
Output
1 1 1 1 1 

g) size( ) or max_size( ) and sizeof( ) function: Both size( ) or max_size( ) are used to get the maximum number of indexes in the array while sizeof( ) is used to get the total size of array in bytes.

C++




#include <iostream>
#include <array>
using namespace std;
 
int main() {
    array <int , 10> arr;   
    cout<<arr.size()<<'\n'; // total num of indexes
    cout<<arr.max_size()<<'\n'; // total num of indexes
    cout<<sizeof(arr); // total size of array
    return 0;
}
 
 
Output
10 10 40

h) data( ): This function returns the pointer to the first element of the array object. Because elements in the array are stored in contiguous memory locations. This data( ) function return us the base address of the string/char type object.

Ex: 

C++




#include <iostream>
#include <cstring>
#include <array>
 
using namespace std;
 
int main ()
{
  const char* str = "GeeksforGeeks";
  array<char,13> arr;
  memcpy (arr.data(),str,13);
  cout << arr.data() << '\n';
  return 0;
}
 
 

 
 

Output
GeeksforGeeks

 

I) cbegin( ) and cend( ):  go to this gfg link : Click_me

 



Next Article
array::begin() and array::end() in C++ STL

S

Shantanu Sharma.
Improve
Article Tags :
  • C++
  • cpp-array
  • STL
Practice Tags :
  • CPP
  • STL

Similar Reads

  • STD::array in C++
    The array is a collection of homogeneous objects and this array container is defined for constant size arrays or (static size). This container wraps around fixed-size arrays and the information of its size are not lost when declared to a pointer. In order to utilize arrays, we need to include the ar
    5 min read
  • array::begin() and array::end() in C++ STL
    Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::begin() begin() function is used to return an iterator pointing to the first element of the array containe
    3 min read
  • array::size() in C++ STL
    The array::size() method is used to find the number of elements in the array container. It is the member method std::array class defined inside <array> header file. In this article, we will learn about the array::size() method in C++. Example: [GFGTABS] C++ // C++ Program to illustrate the use
    2 min read
  • array::empty() in C++ STL
    Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::empty() empty() function is used to check if the array container is empty or not. Syntax : arrayname.empty
    1 min read
  • array::front() and array::back() in C++ STL
    Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::front() This function is used to reference the first element of the array container. This function can be
    3 min read
  • array::at() in C++ STL
    Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::at() This function is used to return the reference to the element present at the position given as the par
    2 min read
  • array::operator[ ] in C++ STL
    Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::operator[] This operator is used to reference the element present at position given inside the operator.
    2 min read
  • array get() function in C++ STL
    The array::get() is a built-in function in C++ STL which returns a reference to the i-th element of the array container. Syntax: get[Tex][/Tex](array_name) Parameters: The function accepts two mandatory parameters which are described below. i - position of an element in the array, with 0 as the posi
    2 min read
  • array data() in C++ STL with Examples
    The array::data() is a built-in function in C++ STL which returns an pointer pointing to the first element in the array object. Syntax: array_name.data() Parameters: The function does not accept any parameters. Return Value: The function returns an pointer. Below programs illustrate the above functi
    2 min read
  • array::max_size() in C++ STL
    Array classes are generally more efficient, light-weight and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::max_size() This function returns the maximum number of elements that the array container can contain. In c
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences