Deklarimi i vektoreve duke perdorur class template array
array< type, arraySize > arrayName;
// array eshte nje class template
type - tipi i elementeve te vektorit
arraySize - madhesia e vektorit
p.sh:
array< int, 12 > c; // c eshte nje vektor me 12 vlera int
array eshte nje class template qe ben pjese ne header file-in <array>
Inicializimi i vlerave ne nje vektor duke perdorur <array>:
array< int, 5 > n = {}; // inicializon me 0 elementet e vektorit n
array< int, 5 > n = { 32, 27, 64, 18, 95 }; // inicializon vektorin n me 5 vlerat ne listen e inicializimit
Shembull i mbushjes se vektorit me vlera 0:
// Fig. 7.3: fig07_03.cpp
// Initializing an array's elements to zeros and printing the array.
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
int main()
{
array< int, 5 > n; // n is an array of 5 int values
// initialize elements of array n to 0
for ( size_t i = 0; i < n.size(); ++i )
n[ i ] = 0; // set element at location i to 0
cout << "Element" << setw( 13 ) << "Value" << endl;
// output each array element's value
for ( size_t j = 0; j < n.size(); ++j )
cout << setw( 7 ) << j << setw( 13 ) << n[ j ] << endl;
} // end main
Ne kete shembull jane perdorur:
size_t perfaqeson nje tip te dhenash per numra te plote pa shenje (unsigned).
size() funksion qe kthen numrin e elementeve ne vektorin ku perdoret
eshte perfshire header file <array>
Printon:
Shembull i mbushjes se vektorit me numra cift:
// Fig. 7.5: fig07_05.cpp
// Set array s to the even integers from 2 to 10.
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
int main()
{
// constant variable can be used to specify array size
const size_t arraySize = 5; // must initialize in declaration
array< int, arraySize > s; // array s has 5 elements
for ( size_t i = 0; i < s.size(); ++i ) // set the values
s[ i ] = 2 + 2 * i;
cout << "Element" << setw( 13 ) << "Value" << endl;
// output contents of array s in tabular format
for ( size_t j = 0; j < s.size(); ++j )
cout << setw( 7 ) << j << setw( 13 ) << s[ j ] << endl;
} // end main
Printon:
Shembull i mbushjes se vektorit me inicializim:
// Fig. 7.4: fig07_04.cpp
// Initializing an array in a declaration.
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
int main()
{
// use list initializer to initialize array n
array< int, 5 > n = { 32, 27, 64, 18, 95 };
cout << "Element" << setw( 13 ) << "Value" << endl;
// output each array element's value
for ( size_t i = 0; i < n.size(); ++i )
cout << setw( 7 ) << i << setw( 13 ) << n[ i ] << endl;
} // end main
Printon:
Shembull i gjetjes se shumes se elementeve ne nje vektor:
// Fig. 7.8: fig07_08.cpp
// Compute the sum of the elements of an array.
#include <iostream>
#include <array>
using namespace std;
int main()
{
const size_t arraySize = 4; // specifies size of array
array< int, arraySize > a = { 10, 20, 30, 40 };
int total = 0;
// sum contents of array a
for ( size_t i = 0; i < a.size(); ++i )
total += a[ i ];
cout << "Total of array elements: " << total << endl;
} // end main
printon:
Total of array elements: 100