C++ STL checked_array_iterator class program sample

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ checked_array_iterator which is a class that accesses an array using a random access, checked iterator in C++ programming

To show: How to use the C++ checked_array_iterator class in C++ programming

 

// C++ STL checked_array_iterator class

// This class is a Microsoft extension to the Standard C++ Library

// compile with: /EHsc

#include <algorithm>

#include <iostream>

using namespace std;

using namespace stdext;

 

int main()

{

// unsized and sized arrays

int a[ ]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int b[10];

 

// do the copy() operation

copy(a, a + 10, checked_array_iterator<int*>(b, 10));

 

// print the data

cout<<"The b array is: ";

for (int i = 0; i < 10; i++)

cout<<" "<<b[i];

cout <<endl;

 

// constructor example

checked_array_iterator<int*> checked_out_iter(b, 10);

copy(a, a + 10, checked_out_iter);

cout<<"The b array is (constructor version): ";

for (int i = 0 ; i < 10 ; i++)

cout<<" "<<b[i];

cout<<endl;

}

 

Output examples:

 

The b array is: 0 1 2 3 4 5 6 7 8 9

The b array is (constructor version): 0 1 2 3 4 5 6 7 8 9

Press any key to continue . . .

 

To avoid the need for the checked_array_iterator class when using Standard C++ Library algorithms, consider using a vector instead of a dynamically allocated array. The following example demonstrates how to do this.

 

// C++ STL, checked_array_iterator

#include <algorithm>

#include <iostream>

#include <vector>

using namespace std;

using namespace stdext;

 

int main(void)

{

// vector container

vector<int> v(10);

// vector iterator

vector <int>::iterator vIter;

// instantiate integer object

int *arr = new int[10];

 

// assign data to the vector and array

for(int i = 0; i < 10; ++i)

{

v[i] = i;

arr[i] = i;

}

 

// print the data

cout<<"The arr array data: ";

for (int i = 0; i < 10; ++i)

{

cout<<" "<<arr[i];

}

cout<<endl;

cout<<"The v vector data: ";

for (vIter = v.begin() ; vIter != v.end(); vIter++)

cout<<*vIter<<" ";

cout<<endl;

// copy(v.begin(), v.end(), arr); will result in warning C4996. To avoid

// this warning while using int *, use the Microsoft extension checked_array_iterator.

copy(v.begin(), v.end(), checked_array_iterator<int *>(arr, 10));

// instead of using stdext::checked_array_iterator and int *, consider using vector to encapsulate

// the array. This will result in no warnings, and the code will be portable.

vector<int> arr2(10); // similar to int *arr = new int[10];

// copy the vector to the arr2

copy(v.begin(), v.end(), arr2.begin());

// print the data

cout<<"The arr2 array data: ";

for (int j = 0; j < arr2.size(); ++j)

{

cout<<" "<<arr2[j];

}

cout<<endl;

cout<<"The v vector data: ";

for (vIter = v.begin() ; vIter != v.end(); vIter++)

cout<<*vIter<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

The arr array data: 0 1 2 3 4 5 6 7 8 9

The v vector data: 0 1 2 3 4 5 6 7 8 9

The arr2 array data: 0 1 2 3 4 5 6 7 8 9

The v vector data: 0 1 2 3 4 5 6 7 8 9

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index