
Author
Dipen Dadhaniya
Engineering Manager at Interview Kickstart
The Standard Library or std in C++ is a collection of classes, functions, and definitions as per the C++ standard. The Standard Library or std is sometimes confused with the Standard Template Library or STL. STL contains many other data structure containers like queue, list, deque, and stack. However, array is a part of std and our topic of interest for this article.
If you are preparing for a tech interview, check out our technical interview checklist, interview questions page, and salary negotiation e-book to get interview-ready! Also, read Vector in C++ STL and Switch Statement in C/C++ to brush up on C++ concepts.
Having trained over 11,000 software engineers, we know what it takes to crack the toughest tech interviews. Our alums consistently land offers from FAANG+ companies. The highest ever offer received by an IK alum is a whopping $1.267 Million!
At IK, you get the unique opportunity to learn from expert instructors who are hiring managers and tech leads at Google, Facebook, Apple, and other top Silicon Valley tech companies.
Want to nail your next tech interview? Sign up for our FREE Webinar.
In this article, we’ll cover:
In C++, std::array represents a sequential container that stores arrays with fixed or static sizes. Array is a collection of homogeneous objects, and we can find its container within the Standard Library or std.
The std::array is not incredibly popular in usage, but its member functions can sometimes provide an edge over our normal array usage methods. We’ll show you how to use these important array member functions in this article.
Here are the several methods associated with array in STD:
In the next section, we’ll look at the most commonly used array methods and how to use them via an example.
Here, we take a look at how you can use array as a C++ STL container for a smoother coding experience:
#include
#include
#include
#include
using namespace std;
int main() {
  // Some different ways to declare an array using std::array in C++
  // Basic integer array of size 5, indices from 0 to 4.
  array arrayExample1 = {1, 3, 5, 7, 9};
  // We need double brace brackets below as the construction uses aggregate initialization
  // Aggregate initialization refers to the use of brace-enclosed initializer lists for initializing all members of an aggregate
  array arrayExample2 {{2, 4, 6, 8, 10}};
  // String array of size 2, indices from 0 to 1.
  array  arrayExample3 = {"Interview", "Kickstart"};
  cout << "arrayExample1: ";
  for (auto number: arrayExample1) {
    cout << number << " ";
  }
  cout << endl;
  // Array size
  cout << "Array size using array.size() gives size in terms of the number of elements: " << arrayExample1.size() << endl;
  cout << "Array size using sizeof(array) gives size in terms of the bytes of memory: " << sizeof(arrayExample1) << endl;
  cout << "Maximum array size: " << arrayExample1.max_size() << endl;
  // Front and back most elements. If the array is empty, both
  // front and back functions would cause undefined behavior
  cout << "Array front-most element: " << arrayExample1.front() << endl;
  cout << "Array back-most element: " << arrayExample1.back() << endl;
  cout << "Array's front-most element is located at the address: " << arrayExample1.cbegin() << endl;
  cout << "Array's front-most element has the value " << *arrayExample1.cbegin() << endl;
  cout << "Array's back-most element is located at the address: " << arrayExample1.cend() - 1 << endl;
  cout << "Array's back-most element has the value " << *(arrayExample1.cend() - 1) << endl;
  cout << "Is the array empty? (boolean, 0 = No, 1 = Yes): " << arrayExample1.empty() << endl;
  // Value at specific index
  cout << "Array element at index 3 using *at()* is: " << arrayExample1.at(3) << endl;
  cout << "Array element at index 3 using [] is: " << arrayExample1[3] << endl;
  // The beginning of an array (address and value)
  cout << "The array starts at the memory address/address of the first element of the array: " << arrayExample1.data() << endl;
  cout << "The array starts with the value: " << *arrayExample1.data() << endl;
  // Fill method example
  cout << "Two arrays filled using fill method:n";
  array arrayExample4;
  arrayExample4.fill("aa");
  for (int index = 0; index < arrayExample4.size(); index++) {
    cout << arrayExample4[index] << " ";
  }
  cout << endl;
  array arrayExample5;
  arrayExample5.fill(3);
  for (int index = 0; index < arrayExample5.size(); index++) {
    cout << arrayExample5[index] << " ";
  }
  cout << endl;
  // Swap method example
  array arrayExample6 = {1, 2, 3};
  array arrayExample7 = {4, 5, 6};
  cout << "arrayExample6: ";
  for (int index = 0; index < arrayExample6.size(); index++) {
    cout << arrayExample6[index] << " ";
  }
  cout << endl;
  cout << "arrayExample7: ";
  for (int index = 0; index < arrayExample7.size(); index++) {
    cout << arrayExample7[index] << " ";
  }
  cout << endl;
  arrayExample6.swap(arrayExample7);
  cout << "arrayExample6 after swapping it with arrayExample7: ";
  for (int index = 0; index < arrayExample6.size(); index++) {
    cout << arrayExample6[index] << " ";
  }
  cout << endl;
  return 0;
}
arrayExample1: 1 3 5 7 9
Array size using array.size() gives size in terms of the number of elements: 5
Array size using sizeof(array) gives size in terms of the bytes of memory: 20
Maximum array size: 5
Array front-most element: 1
Array back-most element: 9
Array's front-most element is located at the address: 0x7fff787a4590
Array's front-most element has the value 1
Array's back-most element is located at the address: 0x7fff787a45a0
Array's back-most element has the value 9
Is the array empty? (boolean, 0 = No, 1 = Yes): 0
Array element at index 3 using *at()* is: 7
Array element at index 3 using [] is: 7
The array starts at the memory address/address of the first element of the array: 0x7fff787a4590
The array starts with the value: 1
Two arrays filled using fill method:
aa aa aa aa
3 3 3 3 3
arrayExample6: 1 2 3
arrayExample7: 4 5 6
arrayExample6 after swapping it with arrayExample7: 4 5 6
â€
Q1. How do you initialize a single std::array in C++?
We can initialize std::array in two common ways. One method is to simply assign values to the array during declaration. For example, std::array<int, 3> intArray = {1, 3, 5}; initializes an integer type std::array with the name “intArray†of length three. Another method involves declaring the contents of the array during the declaration like this: array<int, 6> arrayExample{{2, 4, 6, 8, 10}};
Q2. Is an array an STL container?
No, array is present in the Standard Library or std, not within the Standard Template Library or STL. Arrays in std are fixed-size sequence containers that hold a fixed number of elements in a strictly linear sequence.
Q3. What is the difference between std array and C array?
std::array acts as a zero-overhead wrapper for C arrays. It allows arrays the standard value like semantics we get in the other C++ containers. So there’s no difference in runtime performance, but there are still the extra features we get in std::array.
Q4. Can we have a C++ std array of unknown size?
No, we can’t have a C++ std array of unknown size.
Q5. Can we return an entire array in C++ as an argument to a function?
In C++, we cannot return an entire array as an argument to a function. However, by specifying the array’s name without an index, we can return a pointer to an array.
Whether you’re a coding engineer gunning for a software developer or software engineer role, a tech lead, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!
If you’re looking for guidance and help with getting started, sign up for our FREE webinar. As pioneers in the field of technical interview preparation, we have trained thousands of software engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!
â€
Time Zone:
Get your enrollment process started by registering for a Pre-enrollment Webinar with one of our Founders.
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
The 11 Neural “Power Patterns” For Solving Any FAANG Interview Problem 12.5X Faster Than 99.8% OF Applicants
The 2 “Magic Questions” That Reveal Whether You’re Good Enough To Receive A Lucrative Big Tech Offer
The “Instant Income Multiplier” That 2-3X’s Your Current Tech Salary
Just drop your name and email so we can send your Power Patterns PDF straight to your inbox. No Spam!
By sharing your contact details, you agree to our privacy policy.
Time Zone: Asia/Dhaka
We’ve sent the Power Patterns PDF to your inbox — it should arrive in the next 30 seconds.
📩 Can’t find it? Check your promotions or spam folder — and mark us as safe so you don’t miss future insights.
We’re hosting a private session where FAANG insiders walk through how they actually use these Power Patterns to crack interviews — and what sets top performers apart.
🎯 If you liked the PDF, you’ll love what we’re sharing next.
Time Zone: