다음을 통해 공유


checked_array_iterator 클래스

checked_array_iterator 클래스를 사용하여 확인한 반복기로 배열 또는 포인터를 변환할 수 있습니다 이러한 경고를 전역적으로 해제하는 대신 이 클래스를 원시 포인터 또는 배열에 대한 래퍼로 목적에 따라 사용하여(make_checked_array_iterator 함수 사용) 확인을 제공하고 확인되지 않은 포인터 경고를 관리합니다. 필요에 따라 이 클래스의 확인되지 않은 버전, unchecked_array_iterator를 사용할 수 있습니다.

참고 항목

이 클래스는 C++ 표준 라이브러리의 Microsoft 확장입니다. 이 함수를 사용하여 구현한 코드는 이 Microsoft 확장을 지원하지 않는 C++ 표준 빌드 환경으로 이식할 수 없습니다. 이 클래스의 사용을 요구하지 않는 코드를 작성하는 방법을 보여 주는 예는 아래의 두 번째 예제를 참조하세요.

구문

template <class _Iterator>
class checked_array_iterator;

설명

이 클래스는 stdext 네임스페이스에 정의됩니다.

확인된 반복기 기능에 대한 자세한 내용 및 예제 코드는 확인된 반복기를 참조하세요.

예제

다음 샘플은 확인된 배열 반복기를 정의 및 사용하는 방법을 보여 줍니다.

대상이 복사하는 모든 요소를 보관할 수 없는 크기인 경우, 라인을 변경하는 경우가 해당합니다.

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

수신

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

런타임 오류가 발생합니다.

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

C++ 표준 라이브러리 알고리즘을 사용할 경우 checked_array_iterator 클래스를 사용할 필요가 없도록 하려면 동적으로 할당된 배열 대신 vector를 사용해 보세요. 다음 예제에서는 이 작업을 수행하는 방법을 보여 줍니다.

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

생성자

생성자 Description
checked_array_iterator 기본 반복기에서 기본 checked_array_iterator 또는 checked_array_iterator를 생성합니다.

Typedef

형식 이름 설명
difference_type 동일한 컨테이너 안에서 요소를 참조하는 두 checked_array_iterator 사이의 차이를 제공하는 형식입니다.
pointer checked_array_iterator로 주소를 지정하는 요소에 포인터를 제공하는 형식입니다.
reference checked_array_iterator로 주소를 지정하는 요소에 참조를 제공하는 형식입니다.

멤버 함수

멤버 함수 설명
base checked_array_iterator에서 기본 반복기를 복구합니다.

연산자

연산자 설명
연산자== checked_array_iterator가 같은지 테스트합니다.
operator!= checked_array_iterator가 다른지 테스트합니다.
연산자< 연산자의 좌변에 있는 checked_array_iterator가 우변에 있는 checked_array_iterator보다 작은지 테스트합니다.
연산자> 연산자의 좌변에 있는 checked_array_iterator가 우변에 있는 checked_array_iterator보다 큰지 테스트합니다.
operator<= 연산자의 좌변에 있는 checked_array_iterator가 우변에 있는 checked_array_iterator보다 작거나 같은지 테스트합니다.
operator>= 연산자의 좌변에 있는 checked_array_iterator가 우변에 있는 checked_array_iterator보다 크거나 같은지 테스트합니다.
operator* checked_array_iterator가 주소 지정하는 요소를 반환합니다.
operator-> checked_array_iterator가 주소 지정하는 요소로 포인터를 반환합니다.
operator++ checked_array_iterator를 다음 요소로 증가시킵니다.
연산자-- checked_array_iterator를 이전 요소로 감소시킵니다.
operator+= checked_array_iterator에 지정된 오프셋을 추가합니다.
operator+ 반복기에 오프셋을 추가하고 새 오프셋 위치에서 삽입된 요소를 주소 지정하는 새 checked_array_iterator를 반환합니다.
operator-= checked_array_iterator에서 지정된 오프셋을 감소시킵니다.
operator- 반복기에서 오프셋을 감소시키고 새로운 오프셋 위치에서 삽입된 요소를 주소 지정하는 새로운 checked_array_iterator를 반환합니다.
operator[] checked_array_iterator에서 주소 지정하는 요소의 요소 오프셋으로 지정된 위치 수만큼 참조를 반환합니다.

요구 사항

헤더:<반복기>

네임스페이스: stdext

검사ed_array_iterator::base

checked_array_iterator에서 기본 반복기를 복구합니다.

_Iterator base() const;

설명

자세한 내용은 확인된 반복기을 참조하세요.

예시

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

검사ed_array_iterator::검사ed_array_iterator

기본 반복기에서 기본 checked_array_iterator 또는 checked_array _iterator를 생성합니다.

checked_array_iterator();

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

매개 변수

ptr
배열에 대한 포인터입니다.

size
배열 크기입니다.

인덱스
(선택 사항) 반복기를 초기화하는 배열의 요소입니다. 기본적으로 반복기는 배열의 첫 번째 요소로 초기화됩니다.

설명

자세한 내용은 확인된 반복기을 참조하세요.

예시

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

검사ed_array_iterator::d ifference_type

동일한 컨테이너 안에서 요소를 참조하는 두 checked_array_iterator 사이의 차이를 제공하는 형식입니다.

typedef typename iterator_traits<_Iterator>::difference_type difference_type;

설명

checked_array_iterator 차이 형식은 반복기 차이 형식과 같습니다.

코드 샘플은 checked_array_iterator::operator[]를 참조하세요.

자세한 내용은 확인된 반복기을 참조하세요.

검사ed_array_iterator::operator==

checked_array_iterator가 같은지 테스트합니다.

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

매개 변수

right
같은지 확인할 checked_array_iterator입니다.

설명

자세한 내용은 확인된 반복기을 참조하세요.

예시

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

검사ed_array_iterator::operator!=

checked_array_iterator가 다른지 테스트합니다.

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

매개 변수

right
같지 않은지 확인할 checked_array_iterator입니다.

설명

자세한 내용은 확인된 반복기을 참조하세요.

예시

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

연산자의 좌변에 있는 checked_array_iterator가 우변에 있는 checked_array_iterator보다 작은지 테스트합니다.

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

매개 변수

right
같지 않은지 확인할 checked_array_iterator입니다.

설명

자세한 내용은 확인된 반복기을 참조하세요.

예시

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

연산자의 좌변에 있는 checked_array_iterator가 우변에 있는 checked_array_iterator보다 큰지 테스트합니다.

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

매개 변수

right
비교할 대상인 checked_array_iterator입니다.

설명

코드 샘플을 참조하세요 checked_array_iterator::operator< .

자세한 내용은 확인된 반복기을 참조하세요.

checked_array_iterator::operator<=

연산자의 좌변에 있는 checked_array_iterator가 우변에 있는 checked_array_iterator보다 작거나 같은지 테스트합니다.

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

매개 변수

right
비교할 대상인 checked_array_iterator입니다.

설명

코드 샘플을 참조하세요 checked_array_iterator::operator>= .

자세한 내용은 확인된 반복기을 참조하세요.

checked_array_iterator::operator>=

연산자의 좌변에 있는 checked_array_iterator가 우변에 있는 checked_array_iterator보다 크거나 같은지 테스트합니다.

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

매개 변수

right
비교할 대상인 checked_array_iterator입니다.

설명

자세한 내용은 확인된 반복기을 참조하세요.

예시

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

검사ed_array_iterator::operator*

checked_array_iterator가 주소 지정하는 요소를 반환합니다.

reference operator*() const;

Return Value

checked_array_iterator에서 주소를 지정한 요소의 값입니다.

설명

자세한 내용은 확인된 반복기을 참조하세요.

예시

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

checked_array_iterator가 주소 지정하는 요소로 포인터를 반환합니다.

pointer operator->() const;

Return Value

checked_array_iterator에서 주소를 지정한 요소에 대한 포인터입니다.

설명

코드 샘플은 checked_array_iterator::pointer를 참조하세요.

자세한 내용은 확인된 반복기을 참조하세요.

검사ed_array_iterator::operator++

checked_array_iterator를 다음 요소로 증가시킵니다.

checked_array_iterator& operator++();

checked_array_iterator<_Iterator> operator++(int);

Return Value

첫 번째 연산자는 사전 증가된 checked_array_iterator를 반환하고, 두 번째(사후 증가) 연산자는 증가된 checked_array_iterator의 복사본을 반환합니다.

설명

자세한 내용은 확인된 반복기을 참조하세요.

예시

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

검사ed_array_iterator::operator--

checked_array_iterator를 이전 요소로 감소시킵니다.

checked_array_iterator<_Iterator>& operator--();

checked_array_iterator<_Iterator> operator--(int);

Return Value

첫 번째 연산자는 사전 감소된 checked_array_iterator를 반환하고, 두 번째(사후 감소) 연산자는 감소된 checked_array_iterator의 복사본을 반환합니다.

설명

자세한 내용은 확인된 반복기을 참조하세요.

예시

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

검사ed_array_iterator::operator+=

checked_array_iterator에 지정된 오프셋을 추가합니다.

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

매개 변수

_Off
반복기를 증가시킬 오프셋입니다.

Return Value

checked_array_iterator에서 주소를 지정한 요소에 대한 참조입니다.

설명

자세한 내용은 확인된 반복기을 참조하세요.

예시

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

검사ed_array_iterator::operator+

반복기에 오프셋을 추가하고 새 오프셋 위치에서 삽입된 요소를 주소 지정하는 새 checked_array_iterator를 반환합니다.

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

매개 변수

_Off
checked_array_iterator에 추가할 오프셋입니다.

Return Value

오프셋 요소의 주소를 지정하는 checked_array_iterator입니다.

설명

자세한 내용은 확인된 반복기을 참조하세요.

예시

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

검사ed_array_iterator::operator-=

checked_array_iterator에서 지정된 오프셋을 감소시킵니다.

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

매개 변수

_Off
반복기를 증가시킬 오프셋입니다.

Return Value

checked_array_iterator에서 주소를 지정한 요소에 대한 참조입니다.

설명

자세한 내용은 확인된 반복기을 참조하세요.

예시

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

검사ed_array_iterator::operator-

반복기에서 오프셋을 감소시키고 새로운 오프셋 위치에서 삽입된 요소를 주소 지정하는 새로운 checked_array_iterator를 반환합니다.

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

difference_type operator-(const checked_array_iterator& right) const;

매개 변수

_Off
checked_array_iterator에서 감소될 오프셋입니다.

Return Value

오프셋 요소의 주소를 지정하는 checked_array_iterator입니다.

설명

자세한 내용은 확인된 반복기을 참조하세요.

검사ed_array_iterator::operator[]

checked_array_iterator에서 주소 지정하는 요소의 요소 오프셋으로 지정된 위치 수만큼 참조를 반환합니다.

reference operator[](difference_type _Off) const;

매개 변수

_Off
checked_array_iterator 주소의 오프셋입니다.

Return Value

요소 오프셋에 대한 참조입니다.

설명

자세한 내용은 확인된 반복기을 참조하세요.

예시

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

검사ed_array_iterator::p ointer

checked_array_iterator로 주소를 지정하는 요소에 포인터를 제공하는 형식입니다.

typedef typename iterator_traits<_Iterator>::pointer pointer;

설명

코드 샘플은 checked_array_iterator::operator*를 참조하세요.

자세한 내용은 확인된 반복기을 참조하세요.

검사ed_array_iterator::reference

checked_array_iterator로 주소를 지정하는 요소에 참조를 제공하는 형식입니다.

typedef typename iterator_traits<_Iterator>::reference reference;

설명

코드 샘플은 checked_array_iterator::operator[]를 참조하세요.

자세한 내용은 확인된 반복기을 참조하세요.

참고 항목

<iterator>
C++ 표준 라이브러리 참조