checked_array_iterator – třída

Třída checked_array_iterator umožňuje transformovat pole nebo ukazatel na zaškrtnuté iterátor. Tuto třídu použijte jako obálku (pomocí funkce make_checked_array_iterator ) pro nezpracované ukazatele nebo pole jako cílený způsob, jak zajistit kontrolu a spravovat nezaškrtnuté upozornění ukazatele místo globálního umlčování těchto upozornění. V případě potřeby můžete použít nezaškrtnutou verzi této třídy unchecked_array_iterator.

Poznámka

Tato třída je rozšířením standardní knihovny jazyka C++ od Microsoftu. Kód implementovaný pomocí této funkce není přenosný do standardního prostředí pro sestavování v jazyce C++, která toto rozšíření společnosti Microsoft nepodporují. Jak napsat kód, který nevyžaduje použití této třídy, viz druhý příklad.

Syntaxe

template <class _Iterator>
class checked_array_iterator;

Poznámky

Tato třída je definována v oboru názvů stdext .

Další informace a ukázkový kód pro funkci kontrolovaného iterátoru najdete v tématu Zaškrtnuté iterátory.

Příklady

Následující příklad ukazuje, jak definovat a používat iterátor zkontrolovaného pole.

Pokud cíl není dostatečně velký pro všechny prvky, které jsou kopírovány, jako by tomu bylo při změně řádku:

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

na

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

Dojde k chybě modulu 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 )
*/

Abyste se vyhnuli potřebě checked_array_iterator třídy při použití algoritmů standardní knihovny jazyka C++, zvažte použití vector namísto dynamicky přiděleného pole. Následující příklad demonstruje, jak to udělat.

// 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
*/

Konstruktory

Konstruktor Popis
checked_array_iterator Vytvoří výchozí checked_array_iterator nebo checked_array_iterator z podkladového iterátoru.

Typedefs

Název typu Popis
difference_type Typ, který poskytuje rozdíl mezi dvěma checked_array_iteratorodkazy na prvky ve stejném kontejneru.
Ukazatel Typ, který poskytuje ukazatel na prvek adresovaný znakem checked_array_iterator.
Odkaz Typ, který poskytuje odkaz na prvek adresovaný znakem checked_array_iterator.

Členské funkce

Členová funkce Popis
base Obnoví základní iterátor z jeho checked_array_iterator.

Operátory

Operátor Popis
operator== Testuje dvě checked_array_iterators pro rovnost.
operator!= Testuje dva checked_array_iterators kvůli nerovnosti.
operator< Testuje, jestli checked_array_iterator je na levé straně operátoru menší než checked_array_iterator na pravé straně.
operator> Testuje, jestli checked_array_iterator je na levé straně operátoru větší než checked_array_iterator na pravé straně.
operator<= Testuje, zda checked_array_iterator je levá strana operátoru menší nebo rovna checked_array_iterator pravé straně.
operator>= Testuje, zda checked_array_iterator je levá strana operátoru větší nebo rovna checked_array_iterator pravé straně.
Operátor* Vrátí prvek, který checked_array_iterator adresa.
Operátor-> Vrátí ukazatel na prvek adresovaný znakem checked_array_iterator.
operator++ Zvýší na checked_array_iterator další prvek.
Operátor-- Dekrementuje na checked_array_iterator předchozí prvek.
operator+= Přidá zadaný posun do určitého posunu checked_array_iterator.
operator+ Přidá posun iterátoru a vrátí nové checked_array_iterator adresování vloženého prvku na nové pozici posunu.
operator-= Dekrementuje zadaný posun od určitého posunu checked_array_iteratorod .
Operátor- Sníží posun z iterátoru a vrátí nové checked_array_iterator adresování vloženého prvku na nové pozici posunu.
operator[] Vrátí odkaz na posun prvku od prvku adresovaného checked_array_iterator zadaným počtem pozic.

Požadavky

Header:<iterator>

Obor názvů: stdext

checked_array_iterator::base

Obnoví základní iterátor z jeho checked_array_iterator.

_Iterator base() const;

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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

Vytvoří výchozí checked_array_iterator nebo checked_array _iterator z podkladového iterátoru.

checked_array_iterator();

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

Parametry

ptr
Ukazatel na pole.

Velikost
Velikost pole.

Index
(Volitelné) Prvek v poli pro inicializaci iterátoru. Ve výchozím nastavení je iterátor inicializován na první prvek v poli.

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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

Typ, který poskytuje rozdíl mezi dvěma checked_array_iteratorodkazy na prvky ve stejném kontejneru.

typedef typename iterator_traits<_Iterator>::difference_type difference_type;

Poznámky

Typ checked_array_iterator rozdílu je stejný jako typ rozdílu iterátoru.

Viz checked_array_iterator::operator[] pro vzorový kód.

Další informace naleznete v tématu Kontrola iterátorů.

checked_array_iterator::operator==

Testuje dvě checked_array_iterators pro rovnost.

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

Parametry

Právo
Proti checked_array_iterator kterým se má kontrolovat rovnost.

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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!=

Testuje dva checked_array_iterators kvůli nerovnosti.

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

Parametry

Právo
Proti checked_array_iterator kterému je třeba zkontrolovat nerovnost.

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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<

Testuje, jestli checked_array_iterator je na levé straně operátoru menší než checked_array_iterator na pravé straně.

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

Parametry

Právo
Proti checked_array_iterator kterému je třeba zkontrolovat nerovnost.

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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>

Testuje, jestli checked_array_iterator je na levé straně operátoru větší než checked_array_iterator na pravé straně.

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

Parametry

Právo
Porovnání checked_array_iterator .

Poznámky

Podívejte se checked_array_iterator::operator< na ukázku kódu.

Další informace naleznete v tématu Kontrola iterátorů.

checked_array_iterator::operator<=

Testuje, zda checked_array_iterator je levá strana operátoru menší nebo rovna checked_array_iterator pravé straně.

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

Parametry

Právo
Porovnání checked_array_iterator .

Poznámky

Podívejte se checked_array_iterator::operator>= na ukázku kódu.

Další informace naleznete v tématu Kontrola iterátorů.

checked_array_iterator::operator>=

Testuje, zda checked_array_iterator je levá strana operátoru větší nebo rovna checked_array_iterator pravé straně.

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

Parametry

Právo
Porovnání checked_array_iterator .

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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*

Vrátí prvek, který checked_array_iterator adresa.

reference operator*() const;

Vrácená hodnota

Hodnota prvku adresovaného znakem checked_array_iterator.

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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->

Vrátí ukazatel na prvek adresovaný znakem checked_array_iterator.

pointer operator->() const;

Vrácená hodnota

Ukazatel na prvek adresovaný znakem checked_array_iterator.

Poznámky

Ukázku kódu najdete v tématu checked_array_iterator::p ointer .

Další informace naleznete v tématu Kontrola iterátorů.

checked_array_iterator::operator++

Zvýší na checked_array_iterator další prvek.

checked_array_iterator& operator++();

checked_array_iterator<_Iterator> operator++(int);

Vrácená hodnota

První operátor vrátí předvykrementovaný checked_array_iterator operátor a druhý, operátor postinkrementace, vrátí kopii přírůstku checked_array_iterator.

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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--

Dekrementuje na checked_array_iterator předchozí prvek.

checked_array_iterator<_Iterator>& operator--();

checked_array_iterator<_Iterator> operator--(int);

Vrácená hodnota

První operátor vrátí předdekrementovaný checked_array_iterator operátor a druhý, operátor postdekrementu, vrátí kopii dekrementovaného checked_array_iterator.

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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+=

Přidá zadaný posun do určitého posunu checked_array_iterator.

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

Parametry

_Off
Posun, o který se má iterátor zvýšit.

Vrácená hodnota

Odkaz na prvek adresovaný znakem checked_array_iterator.

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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+

Přidá posun iterátoru a vrátí nové checked_array_iterator adresování vloženého prvku na nové pozici posunu.

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

Parametry

_Off
Posun, který má být přidán do checked_array_iterator.

Vrácená hodnota

Adresování checked_array_iterator posunu prvku.

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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-=

Dekrementuje zadaný posun od určitého posunu checked_array_iteratorod .

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

Parametry

_Off
Posun, o který se má iterátor zvýšit.

Vrácená hodnota

Odkaz na prvek adresovaný znakem checked_array_iterator.

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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-

Sníží posun z iterátoru a vrátí nové checked_array_iterator adresování vloženého prvku na nové pozici posunu.

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

difference_type operator-(const checked_array_iterator& right) const;

Parametry

_Off
Posun, který má být dekrementován z checked_array_iterator.

Vrácená hodnota

Adresování checked_array_iterator posunu prvku.

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

checked_array_iterator::operator[]

Vrátí odkaz na posun prvku od prvku adresovaného checked_array_iterator zadaným počtem pozic.

reference operator[](difference_type _Off) const;

Parametry

_Off
Posun od checked_array_iterator adresy.

Vrácená hodnota

Odkaz na posun prvku.

Poznámky

Další informace naleznete v tématu Kontrola iterátorů.

Příklad

// 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

Typ, který poskytuje ukazatel na prvek adresovaný znakem checked_array_iterator.

typedef typename iterator_traits<_Iterator>::pointer pointer;

Poznámky

Ukázku kódu najdete v tématu checked_array_iterator::operator* .

Další informace naleznete v tématu Kontrola iterátorů.

checked_array_iterator::reference

Typ, který poskytuje odkaz na prvek adresovaný znakem checked_array_iterator.

typedef typename iterator_traits<_Iterator>::reference reference;

Poznámky

Viz checked_array_iterator::operator[] pro vzorový kód.

Další informace naleznete v tématu Kontrola iterátorů.

Viz také

<Iterace>
Standardní knihovna C++ – referenční dokumentace