Przejdź do głównej zawartości

std::array operator[]

// Non const version
constexpr reference at( size_type pos );

// Const version
constexpr const_reference at( size_type pos ) const;

Returns a reference

to the character at specified index pos. No bounds checking is performed.

Parameters​

  • pos - position of the character to return

Return value​

Reference to the requested character.

Exceptions​

None.

Complexity​

Constant.

Example​

Main.cpp
#include <array>
#include <iostream>

int main()
{
std::array<int,4> numbers {2, 4, 6, 8};

std::cout << "Second element: " << numbers[1] << '\n';

numbers[0] = 5;

std::cout << "All numbers:";
for (auto i : numbers) {
std::cout << ' ' << i;
}
std::cout << '\n';
}
Output
Second element: 4
All numbers: 5 4 6 8
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::array operator[]

// Non const version
constexpr reference at( size_type pos );

// Const version
constexpr const_reference at( size_type pos ) const;

Returns a reference

to the character at specified index pos. No bounds checking is performed.

Parameters​

  • pos - position of the character to return

Return value​

Reference to the requested character.

Exceptions​

None.

Complexity​

Constant.

Example​

Main.cpp
#include <array>
#include <iostream>

int main()
{
std::array<int,4> numbers {2, 4, 6, 8};

std::cout << "Second element: " << numbers[1] << '\n';

numbers[0] = 5;

std::cout << "All numbers:";
for (auto i : numbers) {
std::cout << ' ' << i;
}
std::cout << '\n';
}
Output
Second element: 4
All numbers: 5 4 6 8
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.