Condividi tramite


Classe checked_array_iterator

La classe checked_array_iterator consente di trasformare una matrice o un puntatore in un iteratore verificato. Usare questa classe come wrapper (con la funzione make_checked_array_iterator) per i puntatori o le matrici non elaborati in modo da fornire un controllo e gestire gli avvisi relativi ai puntatori non verificati anziché disattivarli globalmente. Se necessario, è possibile usare la versione non verificata della classe unchecked_array_iterator.

Nota

Questa classe è un'estensione Microsoft della libreria standard C++. Il codice implementato mediante questa funzione non può essere trasferito negli ambienti di compilazione standard di C++ che non supportano questa estensione Microsoft. Per un esempio su come scrivere codice che non richieda l'utilizzo di tale classe, vedere il secondo esempio riportato di seguito.

Sintassi

template <class _Iterator>
class checked_array_iterator;

Osservazioni:

Questa classe è definita nello spazio dei nomi stdext.

Per altre informazioni e il codice di esempio per la funzionalità relativa agli iteratori verificati, vedere Iteratori verificati.

Esempi

Nell'esempio seguente viene illustrato come definire e utilizzare un iteratore di matrice verificato.

Se la destinazione non è sufficientemente grande da contenere tutti gli elementi che vengono copiati, come succederebbe se la riga venisse modificata:

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

a

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

Si verificherà un errore di runtime.

// compile with: /EHsc /W4 /MTd
#include <algorithm>
#include <iostream>

using namespace std;
using namespace stdext;

int main() {
   int a[]={0, 1, 2, 3, 4};
   int b[5];
   copy(a, a + 5, checked_array_iterator<int*>(b, 5));

   cout << "(";
   for (int i = 0 ; i < 5 ; i++)
      cout << " " << b[i];
   cout << " )" << endl;

   // constructor example
   checked_array_iterator<int*> checked_out_iter(b, 5);
   copy(a, a + 5, checked_out_iter);

   cout << "(";
   for (int i = 0 ; i < 5 ; i++)
      cout << " " << b[i];
   cout << " )" << endl;
}
/* Output:
( 0 1 2 3 4 )
( 0 1 2 3 4 )
*/

Per eliminare la necessità della classe checked_array_iterator quando si usano gli algoritmi della libreria standard C++, è possibile usare un vector anziché una matrice allocata in modo dinamico. Nell'esempio riportato di seguito viene illustrato come procedere.

// compile with: /EHsc /W4 /MTd

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    std::vector<int> v(10);
    int *arr = new int[10];
    for (int i = 0; i < 10; ++i)
    {
        v[i] = i;
        arr[i] = i;
    }

    // std::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.
    std::copy(v.begin(), v.end(),
              stdext::checked_array_iterator<int *>(arr, 10));

    // Instead of using stdext::checked_array_iterator and int *,
    // consider using std::vector to encapsulate the array. This will
    // result in no warnings, and the code will be portable.
    std::vector<int> arr2(10);    // Similar to int *arr = new int[10];
    std::copy(v.begin(), v.end(), arr2.begin());

    for (int j = 0; j < arr2.size(); ++j)
    {
        cout << " " << arr2[j];
    }
    cout << endl;

    return 0;
}
/* Output:
0 1 2 3 4 5 6 7 8 9
*/

Costruttori

Costruttore Descrizione
checked_array_iterator Costruisce un checked_array_iterator predefinito o un checked_array_iterator da un iteratore sottostante.

Typedef

Nome tipo Descrizione
difference_type Tipo che fornisce la differenza tra due checked_array_iterator che fanno riferimento agli elementi all'interno dello stesso contenitore.
pointer Tipo che fornisce un puntatore a un elemento a cui punta un checked_array_iterator.
reference Tipo che fornisce un riferimento a un elemento a cui punta un checked_array_iterator.

Funzioni membro

Funzione membro Descrizione
base Recupera l'iteratore sottostante dal relativo checked_array_iterator.

Operatori

Operatore Descrizione
operator== Verifica l'uguaglianza di due checked_array_iterator.
operator!= Verifica la disuguaglianza di due checked_array_iterator.
operator< Verifica se checked_array_iterator sul lato sinistro dell'operatore è minore di checked_array_iterator sul lato destro.
operator> Verifica se checked_array_iterator sul lato sinistro dell'operatore è maggiore di checked_array_iterator sul lato destro.
operator<= Verifica se checked_array_iterator sul lato sinistro dell'operatore è minore o uguale di checked_array_iterator sul lato destro.
operator>= Verifica se checked_array_iterator sul lato sinistro dell'operatore è maggiore o uguale a checked_array_iterator sul lato destro.
operator* Restituisce l'elemento a cui punta un checked_array_iterator.
operator-> Restituisce un puntatore all'elemento a cui punta un checked_array_iterator.
operator++ Incrementa checked_array_iterator all'elemento successivo.
Operatore-- Decrementa checked_array_iterator all'elemento precedente.
operator+= Aggiunge un offset specificato a un checked_array_iterator.
operator+ Aggiunge un offset a un iteratore e restituisce il nuovo checked_array_iterator che punta all'elemento inserito in corrispondenza della nuova posizione dell'offset.
operator-= Decrementa un offset specificato da un checked_array_iterator.
operator- Decrementa un offset da un iteratore e restituisce il nuovo checked_array_iterator che punta all'elemento inserito in corrispondenza della nuova posizione dell'offset.
operator[] Restituisce un riferimento all'offset di un elemento dall'elemento a cui punta un checked_array_iterator di un numero specificato di posizioni.

Requisiti

Header:<iterator>

Spazio dei nomi: stdext

checked_array_iterator::base

Recupera l'iteratore sottostante dal relativo checked_array_iterator.

_Iterator base() const;

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

Esempio

// checked_array_iterators_base.cpp
// compile with: /EHsc
#include <iterator>
#include <vector>
#include <iostream>

int main() {
   using namespace std;

   int V1[10];

   for (int i = 0; i < 10 ; i++)
      V1[i] = i;

   int* bpos;

   stdext::checked_array_iterator<int*> rpos(V1, 10);
   rpos++;

   bpos = rpos.base ( );
   cout << "The iterator underlying rpos is bpos & it points to: "
        << *bpos << "." << endl;
}
/* Output:
The iterator underlying rpos is bpos & it points to: 1.
*/

checked_array_iterator::checked_array_iterator

Costruisce un checked_array_iterator predefinito o un checked_array _iterator da un iteratore sottostante.

checked_array_iterator();

checked_array_iterator(
    ITerator ptr,
    size_t size,
    size_t index = 0);

Parametri

ptr
Puntatore alla matrice.

size
Dimensione della matrice.

index
(Facoltativo) Elemento nella matrice, per inizializzare l'iteratore. Per impostazione predefinita, l'iteratore viene inizializzato al primo elemento nella matrice.

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

Esempio

// checked_array_iterators_ctor.cpp
// compile with: /EHsc
#include <iterator>
#include <iostream>

using namespace std;
using namespace stdext;

int main() {
   int a[] = {0, 1, 2, 3, 4};
   int b[5];
   copy(a, a + 5, checked_array_iterator<int*>(b,5));

   for (int i = 0 ; i < 5 ; i++)
      cout << b[i] << " ";
   cout << endl;

   checked_array_iterator<int*> checked_output_iterator(b,5);
   copy (a, a + 5, checked_output_iterator);
   for (int i = 0 ; i < 5 ; i++)
      cout << b[i] << " ";
   cout << endl;

   checked_array_iterator<int*> checked_output_iterator2(b,5,3);
   cout << *checked_output_iterator2 << endl;
}
/* Output:
0 1 2 3 4
0 1 2 3 4
3
*/

checked_array_iterator::d ifference_type

Tipo che fornisce la differenza tra due checked_array_iterator che fanno riferimento agli elementi all'interno dello stesso contenitore.

typedef typename iterator_traits<_Iterator>::difference_type difference_type;

Osservazioni:

Il tipo di differenza checked_array_iterator corrisponde al tipo di differenza dell'iteratore.

Per un esempio di codice, vedere checked_array_iterator::operator[].

Per altre informazioni, vedere Checked Iterators.

checked_array_iterator::operator==

Verifica l'uguaglianza di due checked_array_iterator.

bool operator==(const checked_array_iterator<_Iterator>& right) const;

Parametri

right
Oggetto checked_array_iterator in base al quale verificare l'uguaglianza.

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

Esempio

// checked_array_iterators_opeq.cpp
// compile with: /EHsc
#include <iterator>
#include <iostream>

using namespace std;
using namespace stdext;

int main() {
   int a[] = {0, 1, 2, 3, 4};
   int b[5];
   copy(a, a + 5, checked_array_iterator<int*>(b,5));
   copy(a, a + 5, checked_array_iterator<int*>(b,5));

   checked_array_iterator<int*> checked_output_iterator(b,5);
   checked_array_iterator<int*> checked_output_iterator2(b,5);

   if (checked_output_iterator2 == checked_output_iterator)
      cout << "checked_array_iterators are equal" << endl;
   else
      cout << "checked_array_iterators are not equal" << endl;

   copy (a, a + 5, checked_output_iterator);
   checked_output_iterator++;

   if (checked_output_iterator2 == checked_output_iterator)
      cout << "checked_array_iterators are equal" << endl;
   else
      cout << "checked_array_iterators are not equal" << endl;
}
/* Output:
checked_array_iterators are equal
checked_array_iterators are not equal
*/

checked_array_iterator::operator!=

Verifica la disuguaglianza di due checked_array_iterator.

bool operator!=(const checked_array_iterator<_Iterator>& right) const;

Parametri

right
Oggetto checked_array_iterator in base al quale verificare la disuguaglianza.

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

Esempio

// checked_array_iterators_opneq.cpp
// compile with: /EHsc
#include <iterator>
#include <iostream>

using namespace std;
using namespace stdext;

int main() {
   int a[] = {0, 1, 2, 3, 4};
   int b[5];
   copy(a, a + 5, checked_array_iterator<int*>(b,5));
   copy(a, a + 5, checked_array_iterator<int*>(b,5));

   checked_array_iterator<int*> checked_output_iterator(b,5);
   checked_array_iterator<int*> checked_output_iterator2(b,5);

   if (checked_output_iterator2 != checked_output_iterator)
      cout << "checked_array_iterators are not equal" << endl;
   else
      cout << "checked_array_iterators are equal" << endl;

   copy (a, a + 5, checked_output_iterator);
   checked_output_iterator++;

   if (checked_output_iterator2 != checked_output_iterator)
      cout << "checked_array_iterators are not equal" << endl;
   else
      cout << "checked_array_iterators are equal" << endl;
}
/* Output:
checked_array_iterators are equal
checked_array_iterators are not equal
*/

checked_array_iterator::operator<

Verifica se checked_array_iterator sul lato sinistro dell'operatore è minore di checked_array_iterator sul lato destro.

bool operator<(const checked_array_iterator<_Iterator>& right) const;

Parametri

right
Oggetto checked_array_iterator in base al quale verificare la disuguaglianza.

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

Esempio

// checked_array_iterators_oplt.cpp
// compile with: /EHsc
#include <iterator>
#include <iostream>

using namespace std;
using namespace stdext;

int main() {
   int a[] = {0, 1, 2, 3, 4};
   int b[5];
   copy(a, a + 5, checked_array_iterator<int*>(b,5));
   copy(a, a + 5, checked_array_iterator<int*>(b,5));

   checked_array_iterator<int*> checked_output_iterator(b,5);
   checked_array_iterator<int*> checked_output_iterator2(b,5);

   if (checked_output_iterator2 < checked_output_iterator)
      cout << "checked_output_iterator2 is less than checked_output_iterator" << endl;
   else
      cout << "checked_output_iterator2 is not less than checked_output_iterator" << endl;

   copy (a, a + 5, checked_output_iterator);
   checked_output_iterator++;

   if (checked_output_iterator2 < checked_output_iterator)
      cout << "checked_output_iterator2 is less than checked_output_iterator" << endl;
   else
      cout << "checked_output_iterator2 is not less than checked_output_iterator" << endl;
}
/* Output:
checked_output_iterator2 is not less than checked_output_iterator
checked_output_iterator2 is less than checked_output_iterator
*/

checked_array_iterator::operator>

Verifica se checked_array_iterator sul lato sinistro dell'operatore è maggiore di checked_array_iterator sul lato destro.

bool operator>(const checked_array_iterator<_Iterator>& right) const;

Parametri

right
Oggetto checked_array_iterator con cui eseguire il confronto.

Osservazioni:

Vedere checked_array_iterator::operator< per un esempio di codice.

Per altre informazioni, vedere Checked Iterators.

checked_array_iterator::operator<=

Verifica se checked_array_iterator sul lato sinistro dell'operatore è minore o uguale di checked_array_iterator sul lato destro.

bool operator<=(const checked_array_iterator<_Iterator>& right) const;

Parametri

right
Oggetto checked_array_iterator con cui eseguire il confronto.

Osservazioni:

Vedere checked_array_iterator::operator>= per un esempio di codice.

Per altre informazioni, vedere Checked Iterators.

checked_array_iterator::operator>=

Verifica se checked_array_iterator sul lato sinistro dell'operatore è maggiore o uguale a checked_array_iterator sul lato destro.

bool operator>=(const checked_array_iterator<_Iterator>& right) const;

Parametri

right
Oggetto checked_array_iterator con cui eseguire il confronto.

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

Esempio

// checked_array_iterators_opgteq.cpp
// compile with: /EHsc
#include <iterator>
#include <iostream>

using namespace std;
using namespace stdext;

int main() {
   int a[] = {0, 1, 2, 3, 4};
   int b[5];
   copy(a, a + 5, checked_array_iterator<int*>(b,5));
   copy(a, a + 5, checked_array_iterator<int*>(b,5));

   checked_array_iterator<int*> checked_output_iterator(b,5);
   checked_array_iterator<int*> checked_output_iterator2(b,5);

   if (checked_output_iterator2 >= checked_output_iterator)
      cout << "checked_output_iterator2 is greater than or equal to checked_output_iterator" << endl;
   else
      cout << "checked_output_iterator2 is less than checked_output_iterator" << endl;

   copy (a, a + 5, checked_output_iterator);
   checked_output_iterator++;

   if (checked_output_iterator2 >= checked_output_iterator)
      cout << "checked_output_iterator2 is greater than or equal to checked_output_iterator" << endl;
   else
      cout << "checked_output_iterator2 is less than checked_output_iterator" << endl;
}
/* Output:
checked_output_iterator2 is greater than or equal to checked_output_iterator
checked_output_iterator2 is less than checked_output_iterator
*/

checked_array_iterator::operator*

Restituisce l'elemento a cui punta un checked_array_iterator.

reference operator*() const;

Valore restituito

Il valore dell'elemento a cui punta il checked_array_iterator.

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

Esempio

// checked_array_iterator_pointer.cpp
// compile with: /EHsc
#include <iterator>
#include <algorithm>
#include <vector>
#include <utility>
#include <iostream>

using namespace std;
using namespace stdext;

int main() {
   int a[] = {0, 1, 2, 3, 4};
   int b[5];
   pair<int, int> c[1];
   copy(a, a + 5, checked_array_iterator<int*>(b,5));

   for (int i = 0 ; i < 5 ; i++)
      cout << b[i] << endl;

    c[0].first = 10;
    c[0].second = 20;

   checked_array_iterator<int*> checked_output_iterator(b,5);
   checked_array_iterator<int*>::pointer p = &(*checked_output_iterator);
   checked_array_iterator<pair<int, int>*> chk_c(c, 1);
   checked_array_iterator<pair<int, int>*>::pointer p_c = &(*chk_c);

   cout << "b[0] = " << *p << endl;
   cout << "c[0].first = " << p_c->first << endl;
}
/* Output:
0
1
2
3
4
b[0] = 0
c[0].first = 10
*/

checked_array_iterator::operator->

Restituisce un puntatore all'elemento a cui punta un checked_array_iterator.

pointer operator->() const;

Valore restituito

Puntatore all'elemento a cui punta l'oggetto checked_array_iterator.

Osservazioni:

Per un esempio di codice, vedere checked_array_iterator::pointer.

Per altre informazioni, vedere Checked Iterators.

checked_array_iterator::operator++

Incrementa checked_array_iterator all'elemento successivo.

checked_array_iterator& operator++();

checked_array_iterator<_Iterator> operator++(int);

Valore restituito

Il primo operatore restituisce l'oggetto checked_array_iterator pre-incrementato e il secondo, l'operatore di post-incremento, restituisce una copia dell'oggetto checked_array_iterator incrementato.

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

Esempio

// checked_array_iterators_op_plus_plus.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main() {
   using namespace stdext;
   using namespace std;
   int a[] = {6, 3, 77, 199, 222};
   int b[5];
   copy(a, a + 5, checked_array_iterator<int*>(b,5));

   checked_array_iterator<int*> checked_output_iterator(b,5);

   cout << *checked_output_iterator << endl;
   ++checked_output_iterator;
   cout << *checked_output_iterator << endl;
   checked_output_iterator++;
   cout << *checked_output_iterator << endl;
}
/* Output:
6
3
77
*/

checked_array_iterator::operator--

Decrementa checked_array_iterator all'elemento precedente.

checked_array_iterator<_Iterator>& operator--();

checked_array_iterator<_Iterator> operator--(int);

Valore restituito

Il primo operatore restituisce l'oggetto checked_array_iterator pre-decrementato e il secondo, l'operatore di post-decremento, restituisce una copia dell'oggetto checked_array_iterator decrementato.

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

Esempio

// checked_array_iterators_op_minus_minus.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main() {
   using namespace stdext;
   using namespace std;
   int a[] = {6, 3, 77, 199, 222};
   int b[5];
   copy(a, a + 5, checked_array_iterator<int*>(b,5));

   checked_array_iterator<int*> checked_output_iterator(b,5);

   cout << *checked_output_iterator << endl;
   checked_output_iterator++;
   cout << *checked_output_iterator << endl;
   checked_output_iterator--;
   cout << *checked_output_iterator << endl;
}
/* Output:
6
3
6
*/

checked_array_iterator::operator+=

Aggiunge un offset specificato a un checked_array_iterator.

checked_array_iterator<_Iterator>& operator+=(difference_type _Off);

Parametri

_Fuori
Offset di incremento dell'iteratore.

Valore restituito

Riferimento all'elemento a cui punta l'oggetto checked_array_iterator.

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

Esempio

// checked_array_iterators_op_plus_eq.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main() {
   using namespace stdext;
   using namespace std;
   int a[] = {6, 3, 77, 199, 222};
   int b[5];
   copy(a, a + 5, checked_array_iterator<int*>(b,5));

   checked_array_iterator<int*> checked_output_iterator(b,5);

   cout << *checked_output_iterator << endl;
   checked_output_iterator += 3;
   cout << *checked_output_iterator << endl;
}
/* Output:
6
199
*/

checked_array_iterator::operator+

Aggiunge un offset a un iteratore e restituisce il nuovo checked_array_iterator che punta all'elemento inserito in corrispondenza della nuova posizione dell'offset.

checked_array_iterator<_Iterator> operator+(difference_type _Off) const;

Parametri

_Fuori
Offset da aggiungere all'oggetto checked_array_iterator.

Valore restituito

Oggetto checked_array_iterator che punta all'elemento di offset.

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

Esempio

// checked_array_iterators_op_plus.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main() {
   using namespace stdext;
   using namespace std;
   int a[] = {6, 3, 77, 199, 222};
   int b[5];
   copy(a, a + 5, checked_array_iterator<int*>(b,5));

   checked_array_iterator<int*> checked_output_iterator(b,5);

   cout << *checked_output_iterator << endl;
   checked_output_iterator = checked_output_iterator + 3;
   cout << *checked_output_iterator << endl;
}
/* Output:
6
199
*/

checked_array_iterator::operator-=

Decrementa un offset specificato da un checked_array_iterator.

checked_array_iterator<_Iterator>& operator-=(difference_type _Off);

Parametri

_Fuori
Offset di incremento dell'iteratore.

Valore restituito

Riferimento all'elemento a cui punta l'oggetto checked_array_iterator.

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

Esempio

// checked_array_iterators_op_minus_eq.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main() {
   using namespace stdext;
   using namespace std;
   int a[] = {6, 3, 77, 199, 222};
   int b[5];
   copy(a, a + 5, checked_array_iterator<int*>(b,5));

   checked_array_iterator<int*> checked_output_iterator(b,5);

   checked_output_iterator += 3;
   cout << *checked_output_iterator << endl;
   checked_output_iterator -= 2;
   cout << *checked_output_iterator << endl;
}
/* Output:
199
3
*/

checked_array_iterator::operator-

Decrementa un offset da un iteratore e restituisce il nuovo checked_array_iterator che punta all'elemento inserito in corrispondenza della nuova posizione dell'offset.

checked_array_iterator<_Iterator> operator-(difference_type _Off) const;

difference_type operator-(const checked_array_iterator& right) const;

Parametri

_Fuori
L'offset deve essere diminuito da checked_array_iterator.

Valore restituito

Oggetto checked_array_iterator che punta all'elemento di offset.

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

checked_array_iterator::operator[]

Restituisce un riferimento all'offset di un elemento dall'elemento a cui punta un checked_array_iterator di un numero specificato di posizioni.

reference operator[](difference_type _Off) const;

Parametri

_Fuori
Offset dall'indirizzo dell'oggetto checked_array_iterator.

Valore restituito

Riferimento all'offset dell'elemento.

Osservazioni:

Per altre informazioni, vedere Checked Iterators.

Esempio

// checked_array_iterators_op_diff.cpp
// compile with: /EHsc
#include <vector>
#include <iostream>

int main() {
   using namespace std;
   int V1[10];

   for (int i = 0; i < 10 ; i++)
      V1[i] = i;

   // Declare a difference type for a parameter
   stdext::checked_array_iterator<int*>::difference_type diff = 2;

   stdext::checked_array_iterator<int*> VChkIter(V1, 10);

   stdext::checked_array_iterator<int*>::reference refrpos = VChkIter [diff];

   cout << refrpos + 1 << endl;
}
/* Output:
3
*/

checked_array_iterator::p ointer

Tipo che fornisce un puntatore a un elemento a cui punta un checked_array_iterator.

typedef typename iterator_traits<_Iterator>::pointer pointer;

Osservazioni:

Per un esempio di codice, vedere checked_array_iterator::operator*.

Per altre informazioni, vedere Checked Iterators.

checked_array_iterator::reference

Tipo che fornisce un riferimento a un elemento a cui punta un checked_array_iterator.

typedef typename iterator_traits<_Iterator>::reference reference;

Osservazioni:

Per un esempio di codice, vedere checked_array_iterator::operator[].

Per altre informazioni, vedere Checked Iterators.

Vedi anche

<iterator>
Informazioni di riferimento per la libreria standard C++