다음을 통해 공유


<iterator> 함수

advance

지정된 위치 수만큼 반복기를 증가시킵니다.

template <class InputIterator, class Distance>
void advance(InputIterator& InIt, Distance Off);

매개 변수

InIt
입력 반복기에 대해 증가하고 요구 사항을 충족해야 하는 반복기입니다.

Off
반복기의 차이 형식으로 변환할 수 있고 반복기의 위치를 증가시킬 횟수를 지정하는 정수 계열 형식입니다.

설명

범위는 비지정적이어야 합니다. 여기서 반복기는 역참조 가능하거나 끝을 지나야 합니다.

InputIterator 양방향 반복기 형식 Off 에 대한 요구 사항을 충족하는 경우 음수일 수 있습니다. 입력 또는 정방향 반복기 형식인 Off 경우 InputIterator 음수여야 합니다.

고급 함수는 임의 액세스 반복기에 대한 요구 사항을 충족하는 경우 InputIterator 일정한 복잡성을 가지며, 그렇지 않으면 선형 복잡성이 있으므로 비용이 많이 들 수 있습니다.

예시

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

int main()
{
    using namespace std;

    list<int> L;
    for (int i = 1; i < 9; ++i)
    {
        L.push_back(i);
    }
    list<int>::iterator LPOS = L.begin();

    cout << "The list L is: ( ";
    for (auto L_Iter = L.begin(); L_Iter != L.end(); L_Iter++)
    {
        cout << *L_Iter << " ";
    }
    cout << ")." << endl;

    cout << "The iterator LPOS initially points to the first element: "
        << *LPOS << "." << endl;

    advance(LPOS, 4);
    cout << "LPOS is advanced 4 steps forward to point"
        << " to the fifth element: "
        << *LPOS << "." << endl;

    advance(LPOS, -3);
    cout << "LPOS is moved 3 steps back to point to the "
        << "2nd element: " << *LPOS << "." << endl;
}
The list L is: ( 1 2 3 4 5 6 7 8 ).
The iterator LPOS initially points to the first element: 1.
LPOS is advanced 4 steps forward to point to the fifth element: 5.
LPOS is moved 3 steps back to point to the 2nd element: 2.

back_inserter

지정된 컨테이너 뒤에 요소를 삽입할 수 있는 반복기를 만듭니다.

template <class Container>
back_insert_iterator<Container> back_inserter(Container& Cont);

매개 변수

Cont
후면 삽입을 실행할 컨테이너입니다.

Return Value

컨테이너 개체 Cont와 연결된 back_insert_iterator입니다.

설명

C++ 표준 라이브러리 내에서 인수는 멤버 함수 push_backdeque 가 있는 세 개의 시퀀스 컨테이너 중 하나인 클래스, list 클래스 또는 vector 클래스를 참조해야 합니다.

예시

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

int main()
{
    using namespace std;

    vector<int> vec;
    for (int i = 0; i < 3; ++i)
    {
        vec.push_back(i);
    }

    cout << "The initial vector vec is: ( ";
    for (auto vIter = vec.begin(); vIter != vec.end(); vIter++)
    {
        cout << *vIter << " ";
    }
    cout << ")." << endl;

    // Insertions can be done with template function
    back_insert_iterator<vector<int> > backiter(vec);
    *backiter = 30;
    backiter++;
    *backiter = 40;

    // Alternatively, insertions can be done with the
    // back_insert_iterator member function
    back_inserter(vec) = 500;
    back_inserter(vec) = 600;

    cout << "After the insertions, the vector vec is: ( ";
    for (auto vIter = vec.begin(); vIter != vec.end(); vIter++)
    {
        cout << *vIter << " ";
    }
    cout << ")." << endl;
}
The initial vector vec is: ( 0 1 2 ).
After the insertions, the vector vec is: ( 0 1 2 30 40 500 600 ).

begin

지정된 컨테이너의 첫 번째 요소에 대한 반복기를 검색합니다.

template <class Container>
auto begin(Container& cont)  `
   -> decltype(cont.begin());

template <class Container>
auto begin(const Container& cont)   `
   -> decltype(cont.begin());

template <class Ty, class Size>
Ty *begin(Ty (& array)[Size]);

매개 변수

cont
컨테이너입니다.

array
Ty 형식의 개체의 배열입니다.

Return Value

첫 번째 두 템플릿 함수에서 cont.begin()을 반환합니다. 첫 번째 함수는 비상수이고, 두 번째는 상수입니다.

세 번째 템플릿 함수는 array를 반환합니다.

예시

제네릭 동작이 더 필요할 경우 컨테이너 멤버 begin() 대신 이 템플릿 함수를 사용하는 것이 좋습니다.

// cl.exe /EHsc /nologo /W4 /MTd
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <vector>

template <typename C> void reverse_sort(C& c)
{
    std::sort(std::begin(c), std::end(c), std::greater<>());
}

template <typename C> void print(const C& c)
{
    for (const auto& e : c)
    {
        std::cout << e << " ";
    }
    std::cout << "\n";
}

int main() 
{
    std::vector<int> v = { 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 };

    print(v);
    reverse_sort(v);
    print(v);

    std::cout << "--\n";

    int arr[] = { 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1 };

    print(arr);
    reverse_sort(arr);
    print(arr);
}
11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
52 40 34 26 20 17 16 13 11 10 8 5 4 2 1
--
23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
160 106 80 70 53 40 35 23 20 16 10 8 5 4 2 1

이 함수 reverse_sort는 비멤버 버전 begin()을 호출하므로 정기 배열 이외에 모든 종류의 컨테이너를 지원합니다. 컨테이너 멤버begin()를 사용하는 코딩reverse_sort:

template <typename C>
void reverse_sort(C& c) {
    using std::begin;
    using std::end;

    std::sort(c.begin(), c.end(), std::greater<>());

}

그런 다음 배열을 보내면 이 컴파일러 오류가 발생합니다.

error C2228: left of '.begin' must have class/struct/union

cbegin

지정된 컨테이너의 첫 번째 요소에 대한 const(읽기 전용) 반복기를 검색합니다.

template <class Container>
auto cbegin(const Container& cont)
   -> decltype(cont.begin());

매개 변수

cont
컨테이너 또는 initializer_list.

Return Value

상수 cont.begin()입니다.

설명

이 함수는 모든 C++ 표준 라이브러리 컨테이너와 함께 initializer_list작동합니다.

begin() 템플릿 함수 대신 이 멤버 함수를 사용하여 반환 값이 const_iterator임을 보장할 수 있습니다. 일반적으로 다음 예제와 같이 형식 추론 키워드(keyword) 함께 auto 사용됩니다. 이 예제에서는 Container를 수정 가능(비const) 컨테이너 또는 begin()cbegin()을 지원하는 모든 종류의 initializer_list로 가정합니다.

auto i1 = Container.begin();
// i1 is Container<T>::iterator
auto i2 = Container.cbegin();

// i2 is Container<T>::const_iterator

cend

지정된 컨테이너의 마지막 요소 다음에 있는 요소에 대한 const(읽기 전용) 반복기를 검색합니다.

template <class Container>
auto cend(const Container& cont)
   -> decltype(cont.end());

매개 변수

cont
컨테이너 또는 initializer_list.

Return Value

상수 cont.end()입니다.

설명

이 함수는 모든 C++ 표준 라이브러리 컨테이너와 함께 initializer_list작동합니다.

end() 템플릿 함수 대신 이 멤버 함수를 사용하여 반환 값이 const_iterator임을 보장할 수 있습니다. 일반적으로 다음 예제와 같이 형식 추론 키워드(keyword) 함께 auto 사용됩니다. 이 예제에서는 Container를 수정 가능(비const) 컨테이너 또는 end()cend()을 지원하는 모든 종류의 initializer_list로 가정합니다.

auto i1 = Container.end();
// i1 is Container<T>::iterator
auto i2 = Container.cend();

// i2 is Container<T>::const_iterator

crbegin

컨테이너의 끝에서 시작하여 컨테이너 요소에 대한 역방향 읽기 전용 반복기를 가져옵니다.

template <class C> constexpr auto crbegin(const C& c) -> decltype(std::rbegin(c));

매개 변수

C
컨테이너의 형식입니다.

c
컨테이너 인스턴스입니다.

반환 값

이 반복기는 컨테이너의 끝에서 시작하여 컨테이너의 요소를 역순으로 반환합니다.

요소 10, 20 및 30을 포함하는 벡터 그림입니다. 가장 왼쪽 요소 앞에는 센티넬을 나타내는 가상 상자가 있습니다(맨 왼쪽 요소에 숫자 10 포함). 레이블이 crend()인 경우 벡터의 첫 번째 요소는 숫자 10을 포함하며 '마지막 요소'라는 레이블이 지정됩니다. 벡터의 맨 오른쪽 요소는 30을 포함하며 crbegin()이라는 레이블이 지정됩니다.

예: crbegin

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{10, 20, 30};
    for (auto i = std::crbegin(v); i != std::crend(v); ++i)
    {
        std::cout << *i << ' '; // outputs 30 20 10
    }
    // v[1] = 100; // error because the iterator is const
}
30 20 10

crend

읽기 전용 역방향 요소 시퀀스의 끝에 있는 sentinel을 가져옵니다.

template <class C> constexpr auto crend(const C& c) -> decltype(std::rend(c));

매개 변수

C
컨테이너의 형식입니다.

c
컨테이너 인스턴스입니다.

반환 값

sentinel은 컨테이너의 역방향 보기에서 마지막 요소를 따릅니다.

요소 10, 20 및 30을 포함하는 벡터 그림입니다. 가장 왼쪽 요소 앞에는 센티넬을 나타내는 가상 상자가 있습니다(맨 왼쪽 요소에 숫자 10 포함). 레이블이 crend()인 경우 벡터의 첫 번째 요소는 숫자 10을 포함하며 '마지막 요소'라는 레이블이 지정됩니다. 벡터의 맨 오른쪽 요소는 30을 포함하며 crbegin()이라는 레이블이 지정됩니다.

crend예제

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{10, 20, 30};
    auto vi = std::crend(v);
    --vi; // get off the sentinel and onto the last element in the reversed range
    std::cout << *vi; // outputs 10
    // vi[0] = 300; // error because the iterator is const
}
10

data

컨테이너의 첫 번째 요소에 대한 포인터를 가져옵니다.

1) template <class C> constexpr auto data(C& c) -> decltype(c.data());
2) template <class C> constexpr auto data(const C& c) -> decltype(c.data());
3) template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept;
4) template <class E> constexpr const E* data(initializer_list<E> il) noexcept;

매개 변수

C
컨테이너의 형식입니다.

c
컨테이너의 인스턴스입니다.

E
이니셜라이저 목록의 요소 형식입니다.

il
이니셜라이저 목록입니다.

N
배열의 요소 수입니다.

T
배열에 있는 데이터의 형식입니다.

반환 값

1, 2) 첫 번째 요소에 대한 컨테이너의 형식을 기반으로 하는 포인터입니다. 예를 들어 컨테이너가 정수의 벡터인 경우 반환 값의 형식은 .int *

3) 배열로 첫 번째 요소에 대한 포인터입니다.

4) 이니셜라이저 목록의 첫 번째 요소에 대한 포인터입니다.

예제 data

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{ 10, 20, 30 };
    std::string src{ "a string" };

    const char *charPtr = std::data(src);
    int* intPtr = std::data(v);
    std::cout << charPtr << ", " << *intPtr << '\n'; // a string, 10
}
a string, 10

distance

두 반복기에 의해 주소가 지정된 위치 사이의 간격의 수를 결정합니다.

template <class InputIterator>
typename iterator_traits<InputIterator>::difference_type distance(InputIterator first, InputIterator last);

매개 변수

first
두 번째로부터의 거리를 결정해야 하는 첫 번째 반복기입니다.

last
첫 번째로부터의 거리를 결정해야 하는 두 번째 반복기입니다.

Return Value

같을 때까지 증가해야 하는 first 횟수입니다 last.

설명

거리 함수는 임의 액세스 반복기에 대한 요구 사항을 충족하는 경우 InputIterator 일정한 복잡성을 가지며, 그렇지 않으면 선형 복잡성이 있으므로 비용이 많이 들 수 있습니다.

예시

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

int main()
{
    using namespace std;

    list<int> L;
    for (int i = -1; i < 9; ++i)
    {
        L.push_back(2 * i);
    }
    list <int>::iterator L_Iter, LPOS = L.begin();

    cout << "The list L is: ( ";
    for (L_Iter = L.begin(); L_Iter != L.end(); L_Iter++)
    {
        cout << *L_Iter << " ";
    }
    cout << ")." << endl;

    cout << "The iterator LPOS initially points to the first element: "
         << *LPOS << "." << endl;

    advance(LPOS, 7);
    cout << "LPOS is advanced 7 steps forward to point "
        << " to the eighth element: "
        << *LPOS << "." << endl;

    list<int>::difference_type Ldiff;
    Ldiff = distance(L.begin(), LPOS);
    cout << "The distance from L.begin( ) to LPOS is: "
        << Ldiff << "." << endl;
}
The list L is: ( -2 0 2 4 6 8 10 12 14 16 ).
The iterator LPOS initially points to the first element: -2.
LPOS is advanced 7 steps forward to point  to the eighth element: 12.
The distance from L.begin( ) to LPOS is: 7.

empty

template <class C> constexpr auto empty(const C& c) -> decltype(c.empty());
template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept;
template <class E> constexpr bool empty(initializer_list<E> il) noexcept;

매개 변수

C
컨테이너의 형식입니다.

c
컨테이너의 인스턴스입니다.

E
이니셜라이저 목록의 요소 형식입니다.

il
이니셜라이저 목록입니다.

N
배열의 요소 수입니다.

T
배열에 있는 데이터의 형식입니다.

반환 값

컨테이너에 요소가 없으면 반환하고, 그렇지 않으면 false반환 true 합니다.

예시

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{ 10,20,30 };
    std::vector<int> v2;

    std::cout << std::boolalpha << std::empty(v); // outputs false
    std::cout << std::boolalpha << ", " << std::empty(v2); // outputs true
}
false, true

end

지정된 컨테이너에서 마지막 요소 다음의 요소에 대한 반복기를 검색합니다.

template <class Container>
auto end(Container& cont)
   -> decltype(cont.end());

template <class Container>
auto end(const Container& cont)
   -> decltype(cont.end());

template <class Ty, class Size>
Ty *end(Ty (& array)[Size]);

매개 변수

cont
컨테이너입니다.

array
Ty 형식의 개체의 배열입니다.

Return Value

첫 번째 두 템플릿 함수는 cont.end()를 반환합니다(첫 번째 함수는 비상수이며, 두 번째 함수는 상수임).

세 번째 템플릿 함수는 array + Size를 반환합니다.

설명

코드 예제는 .를 참조하세요 begin.

front_inserter

지정된 컨테이너 앞에 요소를 삽입할 수 있는 반복기를 만듭니다.

template <class Container>
front_insert_iterator<Container> front_inserter(Container& Cont);

매개 변수

Cont
앞에 요소를 삽입할 컨테이너 개체입니다.

Return Value

컨테이너 개체 Cont와 연결된 front_insert_iterator입니다.

설명

front_insert_iterator 클래스의 멤버 함수 front_insert_iterator를 사용할 수도 있습니다.

C++ 표준 라이브러리 내에서 인수는 push_back 멤버 함수가 있는 두 가지 시퀀스 컨테이너 deque 클래스 또는 "list 클래스" 중 하나를 참조해야 합니다.

예시

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

int main()
{
    using namespace std;

    list<int> L;
    for (int i = -1; i < 9; ++i)
    {
        L.push_back(i);
    }

    cout << "The list L is:\n ( ";
    for (auto L_Iter = L.begin(); L_Iter != L.end(); L_Iter++)
    {
        cout << *L_Iter << " ";
    }
    cout << ")." << endl;

    // Using the template function to insert an element
    front_insert_iterator<list <int>> Iter(L);
    *Iter = 100;

    // Alternatively, you may use the front_insert member function
    front_inserter(L) = 200;

    cout << "After the front insertions, the list L is:\n ( ";
    for (auto L_Iter = L.begin(); L_Iter != L.end(); L_Iter++)
    {
        cout << *L_Iter << " ";
    }
    cout << ")." << endl;
}
The list L is:
( -1 0 1 2 3 4 5 6 7 8 ).
After the front insertions, the list L is:
( 200 100 -1 0 1 2 3 4 5 6 7 8 ).

inserter

대신 사용할 inserter(Cont, Where) 수 있는 도우미 템플릿 함수입니다 insert_iterator<Container>(Cont, Where).

template <class Container>
insert_iterator<Container>
inserter(
    Container& Cont,
    typename Container::iterator Where);

매개 변수

Cont
새 요소를 추가할 컨테이너입니다.

Where
삽입 지점을 찾고 있는 반복기입니다.

설명

템플릿 함수가 insert_iterator<Container>(Cont, Where)를 반환합니다.

예시

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

int main()
{
    using namespace std;

    list<int> L;
    for (int i = 2; i < 5; ++i)
    {
        L.push_back(10 * i);
    }

    cout << "The list L is:\n ( ";
    for (auto L_Iter = L.begin(); L_Iter != L.end(); L_Iter++)
    {
        cout << *L_Iter << " ";
    }
    cout << ")." << endl;

    // Using the template version to insert an element
    insert_iterator<list<int>> Iter(L, L.begin());
    *Iter = 1;

    // Alternatively, using the member function to insert an element
    inserter(L, L.end()) = 500;

    cout << "After the insertions, the list L is:\n ( ";
    for (auto L_Iter = L.begin(); L_Iter != L.end(); L_Iter++)
    {
        cout << *L_Iter << " ";
    }
    cout << ")." << endl;
}
The list L is:
( 20 30 40 ).
After the insertions, the list L is:
( 1 20 30 40 500 ).

make_checked_array_iterator

다른 알고리즘에서 사용할 수 있는 checked_array_iterator를 만듭니다.

참고 항목

이 함수는 C++ 표준 라이브러리의 Microsoft 확장입니다. 이 함수를 사용하여 구현한 코드는 이 Microsoft 확장을 지원하지 않는 C++ 표준 빌드 환경으로 이식할 수 없습니다.

template <class Iter>
checked_array_iterator<Iter>
    make_checked_array_iterator(
Iter Ptr,
    size_t Size,
    size_t Index = 0);

매개 변수

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

Size
대상 배열의 크기입니다.

Index
배열에 대한 선택적 크기입니다.

Return Value

checked_array_iterator의 인스턴스입니다.

설명

make_checked_array_iterator 함수는 stdext 네임스페이스에 정의되어 있습니다.

이 함수는 원시 포인터를 사용하며, 경계를 벗어나 확인하지 않는 checked_array_iterator 클래스로 래핑되는 경우가 일반적입니다. 해당 클래스는 확인된 것으로 표시되기 때문에 C++ 표준 라이브러리에서 그에 대해 경고하지 않습니다. 자세한 내용과 코드 예제는 확인된 반복기를 참조하세요.

예시

다음 예제에서는 벡터를 만들고 10개 항목으로 채웁니다. 벡터 콘텐츠는 복사 알고리즘을 사용하여 배열로 복사된 다음, make_checked_array_iterator를 사용하여 대상을 지정합니다. 그러면 디버그 어설션 실패를 트리거할 수 있도록 고의적 경계 위반 확인을 수행합니다.

// make_checked_array_iterator.cpp
// compile with: /EHsc /W4 /MTd

#include <algorithm>
#include <iterator> // stdext::make_checked_array_iterator
#include <memory> // std::make_unique
#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <typename C> void print(const string& s, const C& c)
{
    cout << s;

    for (const auto& e : c) 
    {
        cout << e << " ";
    }

    cout << endl;
}

int main()
{
    const size_t dest_size = 10;
    // Old-school but not exception safe, favor make_unique<int[]>
    // int* dest = new int[dest_size];
    unique_ptr<int[]> updest = make_unique<int[]>(dest_size);
    int* dest = updest.get(); // get a raw pointer for the demo

    vector<int> v;

    for (int i = 0; i < dest_size; ++i) 
    {
        v.push_back(i);
    }
    print("vector v: ", v);

    copy(v.begin(), v.end(), stdext::make_checked_array_iterator(dest, dest_size));

    cout << "int array dest: ";
    for (int i = 0; i < dest_size; ++i)
    {
        cout << dest[i] << " ";
    }
    cout << endl;

    // Add another element to the vector to force an overrun.
    v.push_back(10);

    // ! The next line causes a debug assertion when it executes.
    copy(v.begin(), v.end(), stdext::make_checked_array_iterator(dest, dest_size));
}

make_move_iterator

제공된 반복기를 stored 반복기로 포함하는 move iterator를 만듭니다.

template <class Iterator>
move_iterator<Iterator>
make_move_iterator(const Iterator& It);

매개 변수

It
새 이동 반복기에 저장되는 반복기입니다.

설명

템플릿 함수가 move_iterator<Iterator>(_It)를 반환합니다.

make_unchecked_array_iterator

다른 알고리즘에서 사용할 수 있는 unchecked_array_iterator를 만듭니다.

참고 항목

이 함수는 C++ 표준 라이브러리의 Microsoft 확장입니다. 이 함수를 사용하여 구현한 코드는 이 Microsoft 확장을 지원하지 않는 C++ 표준 빌드 환경으로 이식할 수 없습니다.

template <class Iter>
unchecked_array_iterator<Iter>
    make_unchecked_array_iterator(Iter Ptr);

매개 변수

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

Return Value

unchecked_array_iterator의 인스턴스입니다.

설명

make_unchecked_array_iterator 함수는 stdext 네임스페이스에 정의되어 있습니다.

이 함수는 원시 포인터를 사용하고 확인을 수행하지 않는 클래스에서 래핑되어 최적화 대상이 없지만, C4996 등의 컴파일러 경고도 해제합니다. 따라서 이 방법은 경고를 전체적으로 해제하거나 확인 비용 없이 확인하지 않은 포인터 경고를 처리할 수 있는 계획적 방법입니다. 자세한 내용과 코드 예제는 확인된 반복기를 참조하세요.

예시

다음 예제에서는 벡터를 만들고 10개 항목으로 채웁니다. 벡터 콘텐츠는 복사 알고리즘을 사용하여 배열로 복사된 다음, make_unchecked_array_iterator를 사용하여 대상을 지정합니다.

// make_unchecked_array_iterator.cpp
// compile with: /EHsc /W4 /MTd

#include <algorithm>
#include <iterator> // stdext::make_unchecked_array_iterator
#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <typename C> void print(const string& s, const C& c)
{
    cout << s;

    for (const auto& e : c) 
    {
        cout << e << " ";
    }

    cout << endl;
}

int main()
{
    const size_t dest_size = 10;
    int* dest = new int[dest_size];
    vector<int> v;

    for (int i = 0; i < dest_size; ++i) 
    {
        v.push_back(i);
    }
    print("vector v: ", v);

    // COMPILER WARNING SILENCED: stdext::unchecked_array_iterator is marked as checked in debug mode
    // (it performs no checking, so an overrun will trigger undefined behavior)
    copy(v.begin(), v.end(), stdext::make_unchecked_array_iterator(dest));

    cout << "int array dest: ";
    for (int i = 0; i < dest_size; ++i)
    {
        cout << dest[i] << " ";
    }
    cout << endl;

    delete[] dest;
}
vector v: 0 1 2 3 4 5 6 7 8 9
int array dest: 0 1 2 3 4 5 6 7 8 9

next

지정된 횟수만큼 반복하고 새 반복기 위치를 반환합니다.

template <class InputIterator>
InputIterator next(
    InputIterator first,
    typename iterator_traits<InputIterator>::difference_type off = 1);

매개 변수

first
현재 위치입니다.

off
반복할 횟수입니다.

Return Value

off번 반복 후 새 반복기 위치를 반환합니다.

설명

이 템플릿 함수는 off번 증가된 next를 반환합니다.

prev

역순으로 지정된 횟수만큼 반복하고 새 반복기 위치를 반환합니다.

template <class BidirectionalIterator>
BidirectionalIterator prev(
    BidirectionalIterator first,
    typename iterator_traits<BidirectionalIterator>::difference_type off = 1);

매개 변수

first
현재 위치입니다.

off
반복할 횟수입니다.

설명

이 템플릿 함수는 off번 감소된 next를 반환합니다.

rbegin

컨테이너의 요소를 역순으로 반환하는 반복기를 가져옵니다.

template <class C> constexpr auto rbegin(C& c) -> decltype(c.rbegin());
template <class C> constexpr auto rbegin(const C& c) -> decltype(c.rbegin());

매개 변수

C
컨테이너의 형식입니다.

c
컨테이너 인스턴스입니다.

반환 값

반환된 반복기는 역방향 범위의 끝에서 시작하여 컨테이너의 요소를 역순으로 표시합니다.

요소 10, 20 및 30을 포함하는 벡터 그림입니다. 가장 왼쪽 요소 앞에는 센티넬을 나타내는 가상 상자가 있습니다(맨 왼쪽 요소에 숫자 10 포함). 레이블이 rend()입니다. 벡터의 첫 번째 요소는 숫자 10을 포함하며 '마지막 요소'라는 레이블이 지정됩니다. 벡터의 맨 오른쪽 요소는 30을 포함하며 rbegin()이라는 레이블이 지정됩니다.

예제 rbegin

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{ 10, 20, 30 };

	for (auto e = std::rbegin(v); e != std::rend(v); ++e)
	{
		std::cout << *e << ' '; // outputs 30 20 10
	}
}
30 20 10

rend

역방향 요소 시퀀스의 끝에 있는 sentinel을 가져옵니다.

template <class C> constexpr auto rend(C& c)-> decltype(c.rend());
template <class C> constexpr auto rend(const C& c) -> decltype(c.rend());

매개 변수

C
컨테이너의 형식입니다.

c
컨테이너 인스턴스입니다.

반환 값

컨테이너의 끝에 있는 sentinel에 대한 역방향 반복기입니다. sentinel은 컨테이너의 역방향 보기에서 마지막 요소를 따릅니다.

요소 10, 20 및 30을 포함하는 벡터 그림입니다. 가장 왼쪽 요소 앞에는 센티넬을 나타내는 가상 상자가 있습니다(맨 왼쪽 요소에 숫자 10 포함). 레이블이 rend()입니다. 벡터의 첫 번째 요소는 숫자 10을 포함하며 '마지막 요소'라는 레이블이 지정됩니다. 벡터의 맨 오른쪽 요소는 30을 포함하며 rbegin()이라는 레이블이 지정됩니다.

rend예제

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{10, 20, 30};
    auto vi = std::rend(v);
    --vi; // get off the sentinel and onto the last element in the reversed range
    std::cout << *vi; // outputs 10
}
10

size

template <class C> constexpr auto size(const C& c)
    -> decltype(c.size());
template <class T, size_t N> constexpr size_t size(const T (&array)[N]) noexcept;

매개 변수

C
컨테이너의 형식입니다.

c
컨테이너의 인스턴스입니다.

N
배열의 요소 수입니다.

T
배열에 있는 데이터의 형식입니다.

반환 값

부호 없는 정수와 유사한 값으로 컨테이너의 요소 수입니다.

예제 size

#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v{ 10, 20, 30 };
    size_t s = std::size(v);
    std::cout << s; // outputs 3
}
3