Access






Access

reference array<Ty, N>::operator[](size_type off);
const_reference
  array<Ty, N>::operator[](size_type off) const;

The member functions return a reference to the element of the controlled sequence at position off. If the position is invalid, the behavior is undefined.

These functions provide the usual indexed access to elements of an array. Note that the implementation is not required to check the index value against the bounds of the array. If there is a possibility of getting an index value that is out of bounds, the calling code should check the value before calling these operators.

Figure. Checking Index Values (contarray/indexing.cpp)

#include <array>
#include <iostream>
using std::tr1::array;
using std::cin; using std::cout;

const int ELEMS = 10;
array<int, ELEMS> squares = { 0, 1, 4, 9, 16,
  25, 36, 49, 64, 81 };

int main()
  { // demonstrate array indexing
  int idx = -1;
  while (idx <0 || ELEMS <= idx)
    { // check index value before using
    cout << "Value to square: ";
    cin >> idx;
    }
  cout << idx << " squared is "
    << squares [idx] << "\n\n";

  // no check needed:
  for (idx = 0; idx < ELEMS; ++idx)
    cout << idx << " squared is "
      << squares[idx] << '\n';
  return 0;
  }

reference array<Ty, N>::at(size_type off);
const_reference array<Ty, N>::at(size_type off) const;

The member functions return a reference to the element of the controlled sequence at position off. If the position is invalid, the function throws an object of class std::out_of_range.

These functions provide indexed access to elements of an array. The index value is checked against the bounds of the array; if the value is out of bounds, the function throws an exception. It's tempting to use this function to avoid validating input, but it's usually better to make the range check explicit. Exceptions should be reserved for truly exceptional situations and not used for control flow.

Figure. Implicit Checking of Index Values (contarray/at.cpp)

#include <array>
#include <iostream>
#include <stdexcept>
using std::tr1::array;
using std::cin; using std::cout; using std::out_of_range;

const int ELEMS = 10;
array<int, ELEMS> squares = { 0, 1, 4, 9, 16,
  25, 36, 49, 64, 81 };

int main()
  { // demonstrate index checking with array::at
  int idx = -1;
  for (;;)
    { // don't do this:
    try { // show the value
        cout << idx << " squared is "
          << squares .at(idx) << '\n';
        break;
        }
    catch(const out_of_range&)
      { // prompt for new index value
      cout << "Value to square: ";
      cin >> idx;
      }
    }
  return 0;
  }

reference array<Ty, N>::front();
const_reference array<Ty, N>::front() const;

The member functions return a reference to the first element of the controlled sequence. If the controlled sequence is empty, the behavior is implementation-defined.

reference array<Ty, N>::back();
const_reference array<Ty, N>::back() const;

The member functions return a reference to the last element of the controlled sequence. If the controlled sequence is empty, the behavior is implementation-defined.

T *array<Ty, N>::data();
const T *array<Ty, N>::data() const;

The member functions return a pointer to the first element of the controlled sequence or, for an empty sequence, a non-null pointer that cannot be dereferenced.

As mentioned earlier, the elements of an array object are contiguous. As a result, the pointer returned by data() is a pointer to a C-style array of N objects of type Ty.

Figure. C-Style Array of Objects (contarray/data.cpp)

#include <array>
#include <stdlib.h>
#include <iostream>
using std::tr1::array;
using std::cout;

int lt(const void *left, const void *right)
  { // compare int values pointed to by left and right
  int il = *(int*)left;
  int ir = *(int*)right;
  return il < ir ? -1 : il == ir ? 0 : 1;
  }

const int ELEMS = 6;

int main()
  { // demonstrate use of array::data() as C-style pointer
  array<int, ELEMS> values = { 3, 1, 4, 2, 9, 8 };
  for (int i = 0; i < ELEMS; ++i)
    cout << values [i] << ' ';
  cout << '\n';
  qsort(values.data(), ELEMS, sizeof(int), lt);
  for (int i = 0; i < ELEMS; ++i)
    cout << values[i] << ' ';
  cout << '\n';
  return 0;
  }



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows