Checked and Unchecked Access of a Vector : vector access « vector « C++ Tutorial






#include <iostream>
#include <vector>

using namespace std;

// comment next line out when debugging
#define NDEBUG

class Ship
{
   public:
   Ship( const float cargo_weight[], int length );

   float operator[]( int index ) const;
   // REQUIRE: 0 <= index < cargo_loads()
   // RETURN: weight of load with specified index

   int cargo_loads() const;
      private:
      vector<float> weight_;
   };

   inline
   Ship::Ship( const float weight[], int length )
      : weight_( weight, weight+length )
   {} // empty

   inline
   float Ship::operator[]( int index ) const
   {
   #ifdef NDEBUG
      return weight_[index]; // don't check range when not debugging
   #else
      return weight_.at( index ); // check range when debugging
   #endif
   }

   inline
   int Ship::cargo_loads() const
   { return static_cast<int>( weight_.size() ); }

   int main( )
   {
      const int num_loads = 3;
      const float weights[num_loads] = { 40.8f, 35.2f, 22.1f };

      // make a ship and load it with cargo
      Ship ship( weights, num_loads );

   }








16.25.vector access
16.25.1.Random access of a vector
16.25.2.the use of the subscripting operator
16.25.3.Checked and Unchecked Access of a Vector
16.25.4.access field for vector
16.25.5.Bidirectional random access iterators