Przejdź do głównej zawartości

std::deque operator[]

// Non const version
reference operator[]( size_type pos );

// Const version
const_reference operator[]( size_type pos ) const;

Returns a reference

to the element at specified index pos.

important

No bounds checking is performed, using an element out of bounds is undefined behavior

.

Parameters

  • pos - position of the element to return

Return value

Reference to the requested element.

Exceptions

(none)

Complexity

Constant - O(1).

Example

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

int main()
{
std::deque<int> 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

/Columns>

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::deque operator[]

// Non const version
reference operator[]( size_type pos );

// Const version
const_reference operator[]( size_type pos ) const;

Returns a reference

to the element at specified index pos.

important

No bounds checking is performed, using an element out of bounds is undefined behavior

.

Parameters

  • pos - position of the element to return

Return value

Reference to the requested element.

Exceptions

(none)

Complexity

Constant - O(1).

Example

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

int main()
{
std::deque<int> 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

/Columns>

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.