Partager via


checked_iterator Class

The checked_iterator class allows you to transform an iterator into a checked iterator.

For a list of all members of this type, see checked_iterator Members.

Notes

This class is a Microsoft extension to the Standard C++ Library. Code implemented using this algorithm will not be portable.

template <class _Cont, class _Iter = typename _Cont::iterator>
    class checked_iterator : public iterator<
        typename iterator_traits<_Iter>::iterator_category, 
        typename iterator_traits<_Iter>::value_type, 
        typename iterator_traits<_Iter>::difference_type, 
        typename iterator_traits<_Iter>::pointer, 
        typename iterator_traits<_Iter>::reference>
;

Remarks

This class is defined in the stdext namespace.

This iterator adapter should only be used when you have a non-checked iterator. All Standard C++ Library containers will return a checked iterator if _SECURE_SCL is set to 1, so there is no need to use checked_iterator with std::vector<T>::iterator, for example. However, if you have a custom container which returns iterators that are non-checked, then use checked_iterator.

If your code relies heavily on non-Standard C++ Library containers and iterators and you do not want to use checked_iterator, then consider disabling these warnings using _SCL_SECURE_NO_WARNINGS or the other methods described in that topic.

For more information, see Checked Iterators.

Example

The following code sample shows how to use a function that enforces the use of checked iterators without defining _SECURE_SCL. This requires you to explicitly define checked iterators, for all iterators used by checked functions.

// checked_iterators.cpp
// compile with: /EHsc
#include <vector>
#include <list>
#include <numeric>
#include <functional>
#include <iostream>

int main() {
   using namespace std;   
   vector<int> V1( 10 ), V2( 10 );
   vector<int>::iterator VIter1, VIter2, VIterend, VIterend2;
   stdext::checked_iterator< vector<int> > VChkIterend(V1, V1.begin());
   stdext::checked_iterator< vector<int> > VChkIterend2(V2, V2.begin());


   list<int> L1;
   stdext::checked_iterator< list<int> > LChkIterend(L1, L1.begin());
   list<int>::iterator LIter1, LIterend;

   int t;
   for ( t = 1 ; t <= 10 ; t++ )
      L1.push_back( t );

   cout << "The input list L1 is:\n ( " ;
   for ( LIter1 = L1.begin() ; LIter1 != L1.end() ; LIter1++ )
      cout << *LIter1 << " ";
   cout << ")." << endl;

   // The first member function for the partial sums of
   // elements in a list output to a vector
   stdext::checked_iterator< vector<int> > V1chkbegin(V1, V1.begin());
   VChkIterend = stdext::checked_partial_sum ( L1.begin(), L1.end(), V1chkbegin );

   cout << "The output vector containing the partial sums is:\n ( " ;
   for ( VIter1 = V1.begin( ) ; VIter1 != VChkIterend.base() ; VIter1++ )
      cout << *VIter1 << " ";
   cout << ")." << endl;

   // The second member function used to compute
   // the partial product of the elements in a list
   stdext::checked_iterator< vector<int> > V2chkbegin(V2, V2.begin());
   VChkIterend2 = stdext::checked_partial_sum ( L1.begin(), L1.end(), V2chkbegin , 
      multiplies<int>( ) );
   
   cout << "The output vector with the partial products is:\n ( " ;
   for ( VIter2 = V2.begin() ; VIter2 != VChkIterend2.base() ; VIter2++ )
      cout << *VIter2 << " ";
   cout << ")." << endl;

   // Computation of partial sums in place
   stdext::checked_iterator< list<int> > L1chkbegin(L1, L1.begin());
   LChkIterend = stdext::checked_partial_sum ( L1.begin ( ) , L1.end ( ) , L1chkbegin );
   cout << "The in place output stdext::checked_partial_sum list L1 is:\n ( " ;
   for ( LIter1 = L1.begin( ) ; LIter1 != LChkIterend.base() ; LIter1++ )
      cout << *LIter1 << " ";
   cout << ")." << endl;
}

The input list L1 is:
 ( 1 2 3 4 5 6 7 8 9 10 ).
The output vector containing the partial sums is:
 ( 1 3 6 10 15 21 28 36 45 55 ).
The output vector with the partial products is:
 ( 1 2 6 24 120 720 5040 40320 362880 3628800 ).
The in place output stdext::checked_partial_sum list L1 is:
 ( 1 3 6 10 15 21 28 36 45 55 ).

Requirements

Header: <iterator>

Namespace: stdext

See Also

Concepts

<iterator>

checked_iterator Members

Standard Template Library