Hyrje ne librarine standarte vector

 

Te gjitha difektet e vektoreve te tipit C mund te rregullohen duke perdorur librarine standarte <vector>.

  • elementet e nje vektori nga libraria <vector> inicializohen automatikisht me 0.
  • funksioni size kthen madhesine e vektorit.
  • nje element mund te aksesohet ne formen v[i]
  • permban funksionin at() i cili nuk lejon aksesimin e elementeve jashte kufijve te vektorit

deklarimi:

  • objektet e kesaj librarie mund te krahasohen direkt duke perdorur operatoret e krahasimit == dhe !=.

vector <type> name (size);

Programi i meposhtem demostron aftesite e klases template <vector>.

Per te perdorur funksionet e kesaj klase duhet te shtojme ne filim te programit rreshtat:

   #include <vector>

   using std::vector;

// Fig. 7.26: fig07_26.cpp

// Demonstrating C++ Standard Library class template vector.

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

 

#include <iomanip>

using std::setw;

 

#include <vector>

using std::vector;

 

void outputVector( const vector< int > & ); // display the vector

void inputVector( vector< int > & ); // input values into the vector

 

int main()

{

   vector< int > integers1( 7 ); // 7-element vector< int >

   vector< int > integers2( 10 ); // 10-element vector< int >

 

   // print integers1 size and contents

   cout << "Size of vector integers1 is " << integers1.size()

      << "\nvector after initialization:" << endl;

   outputVector( integers1 );

 

   // print integers2 size and contents

   cout << "\nSize of vector integers2 is " << integers2.size()

      << "\nvector after initialization:" << endl;

   outputVector( integers2 );

 

   // input and print integers1 and integers2

   cout << "\nEnter 17 integers:" << endl;

   inputVector( integers1 );

   inputVector( integers2 );

 

   cout << "\nAfter input, the vectors contain:\n"

      << "integers1:" << endl;

   outputVector( integers1 );

   cout << "integers2:" << endl;

   outputVector( integers2 );

 

   // use inequality (!=) operator with vector objects

   cout << "\nEvaluating: integers1 != integers2" << endl;

 

   if ( integers1 != integers2 )

      cout << "integers1 and integers2 are not equal" << endl;

 

   // create vector integers3 using integers1 as an

   // initializer; print size and contents

   vector< int > integers3( integers1 ); // copy constructor

 

   cout << "\nSize of vector integers3 is " << integers3.size()

      << "\nvector after initialization:" << endl;

   outputVector( integers3 );

 

   // use assignment (=) operator with vector objects

   cout << "\nAssigning integers2 to integers1:" << endl;

   integers1 = integers2; // integers1 is larger than integers2

 

   cout << "integers1:" << endl;

   outputVector( integers1 );

   cout << "integers2:" << endl;

   outputVector( integers2 );

 

   // use equality (==) operator with vector objects

   cout << "\nEvaluating: integers1 == integers2" << endl;

 

   if ( integers1 == integers2 )

      cout << "integers1 and integers2 are equal" << endl;

 

   // use square brackets to create rvalue

   cout << "\nintegers1[5] is " << integers1[ 5 ];

 

   // use square brackets to create lvalue

   cout << "\n\nAssigning 1000 to integers1[5]" << endl;

   integers1[ 5 ] = 1000;

   cout << "integers1:" << endl;

   outputVector( integers1 );

 

   // attempt to use out-of-range subscript

   cout << "\nAttempt to assign 1000 to integers1.at( 15 )" << endl;

   integers1.at( 15 ) = 1000; // ERROR: out of range

   return 0;

} // end main

 

// output vector contents

void outputVector( const vector< int > &array )

{

   size_t i; // declare control variable

  

   for ( i = 0; i < array.size(); i++ )

   {

      cout << setw( 12 ) << array[ i ];

 

      if ( ( i + 1 ) % 4 == 0 ) // 4 numbers per row of output

         cout << endl;

   } // end for

 

   if ( i % 4 != 0 )

      cout << endl;

} // end function outputVector

 

// input vector contents

void inputVector( vector< int > &array )

{

   for ( size_t i = 0; i < array.size(); i++ )

      cin >> array[ i ];

} // end function inputVector

/* Printon:

Size of vector integers1 is 7

vector after initialization:

           0           0           0           0

           0           0           0

 

Size of vector integers2 is 10

vector after initialization:

           0           0           0           0

           0           0           0           0

           0           0

 

Enter 17 integers:

1 5 4 7 8 9 5 7 5 6 4 7 54 6 4 74 7

 

After input, the vectors contain:

integers1:

           1           5           4           7

           8           9           5

integers2:

           7           5           6           4

           7          54           6           4

          74           7

 

Evaluating: integers1 != integers2

integers1 and integers2 are not equal

 

Size of vector integers3 is 7

vector after initialization:

           1           5           4           7

           8           9           5

 

Assigning integers2 to integers1:

integers1:

           7           5           6           4

           7          54           6           4

          74           7

integers2:

           7           5           6           4

           7          54           6           4

          74           7

 

Evaluating: integers1 == integers2

integers1 and integers2 are equal

 

integers1[5] is 54

 

Assigning 1000 to integers1[5]

integers1:

           7           5           6           4

           7        1000           6           4

          74           7

 

Attempt to assign 1000 to integers1.at( 15 )

terminate called after throwing an instance of 'std::out_of_range'

  what():  vector::_M_range_check: __n (which is 15) >= this->size() (which is 1

0)

 

This application has requested the Runtime to terminate it in an unusual way.

Please contact the application's support team for more information.

 

--------------------------------

Process exited after 12.95 seconds with return value 3

Press any key to continue . . .

*/