×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

array::max_size() function with Example in C++ STL

C++ STL | array::max_size() function: Here, we are going to learn about the max_size() function of Array in C++ STL.
Submitted by IncludeHelp, on March 01, 2019

C++ STL array::max_size() function

max_size() function is a library function of array and it is used to get maximum size of an array. It returns the total number of elements that an array can hold.

Syntax

array_name.max_size();

Parameter(s)

None

Return value

Total number of elements that an array can hold.

Sample Input and Output

Input or array declaration:
array<int,5> values {10, 20, 30, 40, 50};

Function call:
values.max_size();

Output:
5

Example

C++ STL program to get the maximum number of elements that an array can hold using array:max_size():

#include <array>
#include <iostream>
using namespace std;

int main() {
  array<int, 5> arr1{10, 20, 30, 40, 50};
  array<int, 10> arr2{10, 20, 30};
  array<int, 0> arr3;  // array of 0 size

  // printing size of arrays
  cout << "size of arr1: " << arr1.max_size() << endl;
  cout << "size of arr2: " << arr2.max_size() << endl;
  cout << "size of arr3: " << arr3.max_size() << endl;

  return 0;
}

Output

size of arr1: 5
size of arr2: 10
size of arr3: 0

Ref: std::array::max_size()

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.