STD::array in C++ Standard Library

Last updated by Dipen Dadhaniya on Oct 9, 2024 at 12:30 PM
Contents

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:

  • Introduction to std::array in C++ Standard Library
  • C++ std Array vs. Array
  • Methods of std::array in C++
  • How to Use the C++ std array
  • FAQs on C++ std array

Introduction to std::array in C++ Standard Library

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. 

Usage of std::array in C++

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.

C++ std Array vs. Array

  • std::Array has friendly value semantics that helps us easily pass it to or return it from a function.
  • std::Array has a convenient interface to find the size.
  • std::Array is good to use with STL-style iterator-based algorithms
  • Defining multi-dimensional array with std:: is more verbose than the conventional method.

Methods of std::array in C++

Here are the several methods associated with array in STD:

  • [ ] Operator 
  • front( )
  • back( ) 
  • swap( ) 
  • empty( )
  • at( )
  • fill( )
  • size( )
  • max_size( )
  • sizeof( )
  • data( )
  • cbegin( )
  • cend( )

In the next section, we’ll look at the most commonly used array methods and how to use them via an example.

How to Use the C++ std array

Here, we take a look at how you can use array as a C++ STL container for a smoother coding experience:

Code


#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; }

Output


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

‍

FAQs on C++ std array

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.

Ready to Nail Your Next Coding Interview?

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!

Sign up now!

‍

Last updated on: October 9, 2024
Author
Dipen Dadhaniya
Engineering Manager at Interview Kickstart
Register for our webinar

Uplevel your career with AI/ML/GenAI

Loading_icon
Loading...
1 Enter details
2 Select webinar slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Spring vs Spring Boot

Spring vs Spring Boot: Simplifying Java Application Development

Reinforcement Learning: Teaching Machines to Make Optimal Decisions

Reinforcement Learning: Teaching Machines to Make Optimal Decisions

Product Marketing vs Product Management

Product Marketing vs Product Management

Java Scanner reset()

The upper() Function in Python

Insertion Sort Algorithm

Ready to Enroll?

Get your enrollment process started by registering for a Pre-enrollment Webinar with one of our Founders.

Next webinar starts in

00
DAYS
:
00
HR
:
00
MINS
:
00
SEC

Register for our webinar

How to Nail your next Technical Interview

Loading_icon
Loading...
1 Enter details
2 Select slot
By sharing your contact details, you agree to our privacy policy.

Select a Date

Time slots

Time Zone:

Get tech interview-ready to navigate a tough job market

Best suitable for: Software Professionals with 5+ years of exprerience
Register for our FREE Webinar

Next webinar starts in

00
DAYS
:
00
HR
:
00
MINS
:
00
SEC