網頁

2016年11月17日 星期四

Class Templates array

Introduction 

data structure-collection: array and vector
1. array: fix-size collection and same type
2. vector: collections grow and shrink dynamically and  same type

Declaring

array< type, arraySize > arrayName;

1. arraySize must be  an unsigned integer
2. type string can be used

advantage: bound check for array subscripts

ex.
array< int, 5 > n;
for ( size_t i = 0; i < n.size(); ++i )
  n[ i ] = 0;

size_t (unsigned integral type): 在32位元系統中size_t是4位元組的,而在64位元系統中,size_t是8位元組的。C++ head file <cstddef>、C head file <stddef.h> 。

application: 增強程式的可攜性。

Initializing

ex.
array< int, 5 > n = {};
array< int, 5 > n = { 1, 2, 3, 4, 5 };

Constant variable

ex.
const size_t arraySize = 5; // must initialize in declaration
array< int, arraySize > s;

Summing the Element of an array

ex.
for ( size_t i = 0; i < a.size(); ++i )
  tatal += a[ i ];

Using Bar Charts to Display array Data Graphically

ex.
array< unsigned int, arraySize > n={ 0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1 }

for ( size_t i = 0; i < n.size(); ++i )
  for ( unsigned int stars = 0; stars < n.size(); ++stars )
    cout << '*' ;

Using the Elements of an array as Counters

default_random_engine engine( static_cast< unsigned int >( time(0) ) );
uniform_int_distribution< unsigned int  > randomInt( 1,6 );

const size_t arraySize = 7;
array < unsigned int, arraySize > frequency = {};

for ( unsigned int roll <= 6000000; ++roll )
  ++frequency[ randomInt( engine )];

Using arrays to Summarize Survey Results

const size_t responseSize = 10;
const size_t frequencySize = 6;

const array< unsigned int, responseSize > responses = { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };
array < unsigned int, frequencySize > frequency = {};

for ( size_t answer =0; answer < responses.size; ++answer )
  ++frequency[ responses[ answer ] ];

Static Local arrays and Automatic Local arrays

1. The static local array is initialized to zero by the compiler the first time the function is called.
2. The second time the function is called, the static array contains the modified values stored during the first function call

ex.
static array< int, arraySize > array1;

Range-Based for Statement

1.for ( rangeVariableDeclaration : expression )
  statement

2. int reference (&) can change the corresponding element value in the array

ex.
array< int, 5 > items = { 1, 2, 3, 4, 5 };
for (int item : items)

array< int, 5 > items = { 1, 2, 3, 4, 5 };
for (int &itemRef : items)
  itemRef * = 2;

Sorting and Searching arrays

ex.
1. Standard library class templates

array < string, arraySize > colors = { "red", "orange", "yellow", "green", "blue", "indigo" }

sort( colors.begin(), colors.end() ); // sort contents of colors

bool found = binary_search( colors.begin(), colors.end(), "indigo" );
  cout << "\n\n\"indigo\" << ( found ? "was" : "was not" ) << " found in colors" << endl;
found = binary_search( colors.begin(), colors.end(), "cyan" );
  cout << "\n\n\"cyan\" << ( found ? "was" : "was not" ) << " found in colors" << endl;

2. Built-in Arrays

#include< iterator >

int n[5] = { 50, 20, 30, 10, 40 };

sort( begin( n ), end( n ) ); // sort contents of built-in array n ( type arrayName[ arraySize ] )

Multidimension arrays

ex.
array< array< int, columns >, rows > array1 = { 1, 2, 3, 4, 5, 6 }

1. Nested Range-Based for Statements

void printArray( const array< array< int, columns >, rows > & a )
{
  for ( auto const &row : a )
  {
     for ( auto const &element : row )
    {
       cout << element << ';
     cout << endl;
    }
  }
}

2. Nest Counter-Controlled for statements

for ( size_t row = 0; row < a.size(); ++row )
{
  for ( size_t column = 0; column < a[row].size(); ++column )
    cout << a[ row ][ column ] << ' ';
  cout << endl;
}

3. Other Common array Manipulations

for ( size_t row = 0; row < a.size(); ++row )
  for ( size_t column = 0; column < a[row].size(); ++column )
    total += a[ row ][[ column ];

for ( auto row : a )
  for ( auto column : row )
    total += column;

Outputting Built-in Array Contents with Pointers

ex.

#include< iterator >

int value[ SIZE ] = { 1, 2, 3, 4, 5, 6 }

for ( const int *ptr = begin( values ); ptr != end( values ); ++ptr )
  cout << *ptr << ' ';


沒有留言:

張貼留言