다음을 통해 공유


<iterator> 연산자

operator!=

연산자의 왼쪽에 있는 반복기 개체가 오른쪽의 반복기 개체와 같지 않은지 테스트합니다.

template <class RandomIterator>
bool operator!=(const reverse_iterator<RandomIterator>& left, const reverse_iterator<RandomIterator>& right);

template <class Type, class CharType, class Traits, class Distance>
bool operator!=(const istream_iterator<Type, CharType, Traits, Distance>& left, const istream_iterator<Type, CharType, Traits, Distance>& right);

template <class CharType, class Tr>
bool operator!=(const istreambuf_iterator<CharType, Traits>& left, const istreambuf_iterator<CharType, Traits>& right);

매개 변수

left
iterator 형식의 개체입니다.

right
iterator 형식의 개체입니다.

Return Value

true 반복기 개체가 같지 않으면 false 반복기 개체가 같으면 입니다.

설명

반복기 개체가 컨테이너의 동일한 요소로 주소 지정되면 서로 같습니다. 두 반복기가 컨테이너의 다른 요소를 가리키면 같지 않습니다.

예시

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

int main()
{
    using namespace std;

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

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

    // Initializing reverse_iterators to the last element
    vector <int>::reverse_iterator rVPOS1 = vec.rbegin(),
        rVPOS2 = vec.rbegin();

    cout << "The iterator rVPOS1 initially points to the first "
        << "element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

    if (rVPOS1 != rVPOS2)
    {
        cout << "The iterators are not equal." << endl;
    }
    else
    {
        cout << "The iterators are equal." << endl;
    }

    rVPOS1++;
    cout << "The iterator rVPOS1 now points to the second "
        << "element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

    if (rVPOS1 != rVPOS2)
    {
        cout << "The iterators are not equal." << endl;
    }
    else
    {
        cout << "The iterators are equal." << endl;
    }
}
The vector vec is: ( 1 2 3 4 5 6 7 8 ).
The iterator rVPOS1 initially points to the first element
in the reversed sequence: 8.
The iterators are equal.
The iterator rVPOS1 now points to the second element
in the reversed sequence: 7.
The iterators are not equal.

operator==

연산자의 좌변에 있는 반복기 개체가 우변에 있는 반복기 개체와 같은지를 테스트합니다.

template <class RandomIterator1, class RandomIterator2>
bool operator==(
    const move_iterator<RandomIterator1>& left,
    const move_iterator<RandomIterator2>& right);

template <class RandomIterator1, class RandomIterator2>
bool operator==(
    const reverse_iterator<RandomIterator1>& left,
    const reverse_iterator<RandomIterator2>& right);

template <class Type, class CharType, class Traits, class Distance>
bool operator==(
    const istream_iterator<Type, CharType, Traits, Distance>& left,
    const istream_iterator<Type, CharType, Traits, Distance>& right);

template <class CharType, class Tr>
bool operator==(
    const istreambuf_iterator<CharType, Traits>& left,
    const istreambuf_iterator<CharType, Traits>& right);

매개 변수

left
iterator 형식의 개체입니다.

right
iterator 형식의 개체입니다.

Return Value

true 반복기 개체가 같으면 이고, false 반복기 개체가 같지 않으면

설명

반복기 개체가 컨테이너의 동일한 요소로 주소 지정되면 서로 같습니다. 두 반복기가 컨테이너의 다른 요소를 가리키면 같지 않습니다.

처음 두 템플릿 연산자는 leftright가 동일한 반복기를 저장하는 경우에만 true를 반환합니다. 세 번째 템플릿 연산자는 leftright가 동일한 스트림 포인터를 저장하는 경우에만 true를 반환합니다. 네 번째 템플릿 연산자는 left.equal (right)를 반환합니다.

예시

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

int main()
{
    using namespace std;

    vector<int> vec;
    for (int i = 1; i < 6; ++i)
    {
        vec.push_back(2 * i);
    }

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

    // Initializing reverse_iterators to the last element
    vector <int>::reverse_iterator rVPOS1 = vec.rbegin(),
        rVPOS2 = vec.rbegin();

    cout << "The iterator rVPOS1 initially points to the first "
        << "element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

    if (rVPOS1 == rVPOS2)
    {
        cout << "The iterators are equal." << endl;
    }
    else
    {
        cout << "The iterators are not equal." << endl;
    }

    rVPOS1++;
    cout << "The iterator rVPOS1 now points to the second "
        << "element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

    if (rVPOS1 == rVPOS2)
    {
        cout << "The iterators are equal." << endl;
    }
    else
    {
        cout << "The iterators are not equal." << endl;
    }
}
The vector vec is: ( 2 4 6 8 10 ).
The iterator rVPOS1 initially points to the first element
in the reversed sequence: 10.
The iterators are equal.
The iterator rVPOS1 now points to the second element
in the reversed sequence: 8.
The iterators are not equal.

operator<

연산자의 좌변에 있는 반복기 개체가 우변에 있는 반복기 개체보다 작은지를 테스트합니다.

template <class RandomIterator>
bool operator<(const reverse_iterator<RandomIterator>& left, const reverse_iterator<RandomIterator>& right);

매개 변수

left
iterator 형식의 개체입니다.

right
iterator 형식의 개체입니다.

Return Value

true 식의 왼쪽에 있는 반복기가 식의 오른쪽에 있는 반복기보다 작으면 false 오른쪽의 반복기보다 크거나 같은 경우

설명

한 반복기 개체가 컨테이너에서 다른 반복기 개체에 의해 주소가 지정된 요소보다 먼저 나타나는 요소의 주소를 지정하는 경우 다른 반복기 개체보다 작습니다. 한 반복기 개체는 다른 반복기 개체와 동일한 요소 또는 다른 반복기 개체에서 주소가 지정된 요소보다 나중에 컨테이너에서 발생하는 요소의 주소를 지정하는 경우 다른 반복기 개체보다 작지 않습니다.

예시

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

int main()
{
    using namespace std;

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

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

    // Initializing reverse_iterators to the last element
    vector <int>::reverse_iterator rVPOS1 = vec.rbegin(),
        rVPOS2 = vec.rbegin();

    cout << "The iterators rVPOS1& rVPOS2 initially point to the "
        << "first element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

    if (rVPOS1 < rVPOS2)
    {
        cout << "The iterator rVPOS1 is less than"
            << " the iterator rVPOS2." << endl;
    }
    else
    {
        cout << "The iterator rVPOS1 is not less than"
            << " the iterator rVPOS2." << endl;

        rVPOS2++;
        cout << "The iterator rVPOS2 now points to the second "
            << "element\n in the reversed sequence: "
            << *rVPOS2 << "." << endl;

        if (rVPOS1 < rVPOS2)
        {
            cout << "The iterator rVPOS1 is less than"
                << " the iterator rVPOS2." << endl;
        }
        else
        {
            cout << "The iterator rVPOS1 is not less than"
                << " the iterator rVPOS2." << endl;
        }
    }
}
The initial vector vec is: ( 0 2 4 6 8 10 ).
The iterators rVPOS1& rVPOS2 initially point to the first element
in the reversed sequence: 10.
The iterator rVPOS1 is not less than the iterator rVPOS2.
The iterator rVPOS2 now points to the second element
in the reversed sequence: 8.
The iterator rVPOS1 is less than the iterator rVPOS2.

operator<=

연산자의 좌변에 있는 반복기 개체가 우변에 있는 반복기 개체보다 작거나 같은지를 테스트합니다.

template <class RandomIterator>
bool operator<=(const reverse_iterator<RandomIterator>& left, const reverse_iterator<RandomIterator>& right);

매개 변수

left
iterator 형식의 개체입니다.

right
iterator 형식의 개체입니다.

Return Value

true 식의 왼쪽에 있는 반복기가 식의 오른쪽에 있는 반복기보다 작거나 같으면 입니다. false 오른쪽의 반복기보다 크면

설명

한 반복기 개체가 동일한 요소 또는 컨테이너에서 다른 반복기 개체에 의해 주소가 지정된 요소보다 먼저 나타나는 요소의 주소를 지정하는 경우 다른 반복기 개체보다 작거나 같습니다. 한 반복기 개체가 컨테이너에서 다른 반복기 개체에 의해 주소가 지정된 요소보다 나중에 나타나는 요소의 주소를 지정하는 경우 다른 반복기 개체보다 큽니다.

예시

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

int main()
{
    using namespace std;

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

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

    vector <int>::reverse_iterator rVPOS1 = vec.rbegin() + 1,
        rVPOS2 = vec.rbegin();

    cout << "The iterator rVPOS1 initially points to the "
        << "second element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

    cout << "The iterator rVPOS2 initially points to the "
        << "first element\n in the reversed sequence: "
        << *rVPOS2 << "." << endl;

    if (rVPOS1 <= rVPOS2)
    {
        cout << "The iterator rVPOS1 is less than or "
            << "equal to the iterator rVPOS2." << endl;
    }
    else
    {
        cout << "The iterator rVPOS1 is greater than "
            << "the iterator rVPOS2." << endl;
    }

    rVPOS2++;
    cout << "The iterator rVPOS2 now points to the second "
        << "element\n in the reversed sequence: "
        << *rVPOS2 << "." << endl;

    if (rVPOS1 <= rVPOS2)
    {
        cout << "The iterator rVPOS1 is less than or "
            << "equal to the iterator rVPOS2." << endl;
    }
    else
    {
        cout << "The iterator rVPOS1 is greater than "
            << "the iterator rVPOS2." << endl;
    }
}
The initial vector vec is: ( 0 2 4 6 8 10 ).
The iterator rVPOS1 initially points to the second element
in the reversed sequence: 8.
The iterator rVPOS2 initially points to the first element
in the reversed sequence: 10.
The iterator rVPOS1 is greater than the iterator rVPOS2.
The iterator rVPOS2 now points to the second element
in the reversed sequence: 8.
The iterator rVPOS1 is less than or equal to the iterator rVPOS2.

operator>

연산자의 좌변에 있는 반복기 개체가 우변에 있는 반복기 개체보다 큰지를 테스트합니다.

template <class RandomIterator>
bool operator>(const reverse_iterator<RandomIterator>& left, const reverse_iterator<RandomIterator>& right);

매개 변수

left
iterator 형식의 개체입니다.

right
iterator 형식의 개체입니다.

Return Value

true 식의 왼쪽에 있는 반복기가 식의 오른쪽에 있는 반복기보다 크면 false 오른쪽의 반복기보다 작거나 같으면

설명

한 반복기 개체가 컨테이너에서 다른 반복기 개체에 의해 주소가 지정된 요소보다 나중에 나타나는 요소의 주소를 지정하는 경우 다른 반복기 개체보다 큽니다. 한 반복기 개체는 다른 반복기 개체와 동일한 요소 또는 다른 반복기 개체에서 주소가 지정된 요소보다 컨테이너의 앞부분에서 발생하는 요소의 주소를 지정하는 경우 다른 반복기 개체보다 크지 않습니다.

예시

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

int main()
{
    using namespace std;

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

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

    vector <int>::reverse_iterator rVPOS1 = vec.rbegin(),
        rVPOS2 = vec.rbegin();

    cout << "The iterators rVPOS1 & rVPOS2 initially point to "
        << "the first element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

    if (rVPOS1 > rVPOS2)
    {
        cout << "The iterator rVPOS1 is greater than "
            << "the iterator rVPOS2." << endl;
    }
    else
    {
        cout << "The iterator rVPOS1 is less than or "
            << "equal to the iterator rVPOS2." << endl;
    }

    rVPOS1++;
    cout << "The iterator rVPOS1 now points to the second "
        << "element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

    if (rVPOS1 > rVPOS2)
    {
        cout << "The iterator rVPOS1 is greater than "
            << "the iterator rVPOS2." << endl;
    }
    else
    {
        cout << "The iterator rVPOS1 is less than or "
            << "equal to the iterator rVPOS2." << endl;
    }
}
The initial vector vec is: ( 0 2 4 6 8 10 ).
The iterators rVPOS1 & rVPOS2 initially point to the first element
in the reversed sequence: 10.
The iterator rVPOS1 is less than or equal to the iterator rVPOS2.
The iterator rVPOS1 now points to the second element
in the reversed sequence: 8.
The iterator rVPOS1 is greater than the iterator rVPOS2.

operator>=

연산자의 좌변에 있는 반복기 개체가 우변에 있는 반복기 개체보다 크거나 같은지를 테스트합니다.

template <class RandomIterator>
bool operator>=(const reverse_iterator<RandomIterator>& left, const reverse_iterator<RandomIterator>& right);

매개 변수

left
iterator 형식의 개체입니다.

right
iterator 형식의 개체입니다.

Return Value

true 식의 왼쪽에 있는 반복기가 식의 오른쪽에 있는 반복기보다 크거나 같으면 입니다. false 오른쪽의 반복기보다 작으면

설명

한 반복기 개체가 동일한 요소 또는 컨테이너에서 다른 반복기 개체에 의해 주소가 지정된 요소보다 나중에 나타나는 요소의 주소를 지정하는 경우 다른 반복기 개체보다 큽니다. 한 반복기 개체가 컨테이너에서 다른 반복기 개체에 의해 주소가 지정된 요소보다 먼저 나타나는 요소의 주소를 지정하는 경우 다른 반복기 개체보다 작습니다.

예시

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

int main()
{
    using namespace std;

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

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

    vector <int>::reverse_iterator rVPOS1 = vec.rbegin(),
        rVPOS2 = vec.rbegin() + 1;

    cout << "The iterator rVPOS1 initially points to the "
        << "first element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

    cout << "The iterator rVPOS2 initially points to the "
        << "second element\n in the reversed sequence: "
        << *rVPOS2 << "." << endl;

    if (rVPOS1 >= rVPOS2)
    {
        cout << "The iterator rVPOS1 is greater than or "
            << "equal to the iterator rVPOS2." << endl;
    }
    else
    {
        cout << "The iterator rVPOS1 is less than "
            << "the iterator rVPOS2." << endl;
    }
    rVPOS1++;
    cout << "The iterator rVPOS1 now points to the second "
        << "element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

    if (rVPOS1 >= rVPOS2)
    {
        cout << "The iterator rVPOS1 is greater than or "
            << "equal to the iterator rVPOS2." << endl;
    }
    else
    {
        cout << "The iterator rVPOS1 is less than "
            << "the iterator rVPOS2." << endl;
    }
}
The initial vector vec is: ( 0 2 4 6 8 10 ).
The iterator rVPOS1 initially points to the first element
in the reversed sequence: 10.
The iterator rVPOS2 initially points to the second element
in the reversed sequence: 8.
The iterator rVPOS1 is less than the iterator rVPOS2.
The iterator rVPOS1 now points to the second element
in the reversed sequence: 8.
The iterator rVPOS1 is greater than or equal to the iterator rVPOS2.

operator+

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

template <class RandomIterator, class Diff>
move_iterator<RandomIterator>
operator+(
    Diff _Off,
    const move_iterator<RandomIterator>& right);

template <class RandomIterator>
reverse_iterator<RandomIterator>
operator+(
    Diff _Off,
    const reverse_iterator<RandomIterator>& right);

매개 변수

Off
const move_iterator 또는 const reverse_iterator가 오프셋되는 위치의 수입니다.

right
오프셋할 반복기입니다.

Return Value

합계 right + Off를 반환합니다.

예시

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

int main()
{
    using namespace std;

    vector<int> vec;
    for (int i = 0; i < 6; ++i) 
    {
        vec.push_back(2 * i);
    }
  
    cout << "The initial vector vec is: ( ";
    for (vector <int>::iterator vIter = vec.begin(); vIter != vec.end(); vIter++)
    {
        cout << *vIter << " ";
    }
    cout << ")." << endl;

    vector <int>::reverse_iterator rVPOS1 = vec.rbegin();

    cout << "The iterator rVPOS1 initially points to "
        << "the first element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

    vector<int>::difference_type diff = 4;
    rVPOS1 = diff + rVPOS1;

    cout << "The iterator rVPOS1 now points to the fifth "
        << "element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;
}
The initial vector vec is: ( 0 2 4 6 8 10 ).
The iterator rVPOS1 initially points to the first element
in the reversed sequence: 10.
The iterator rVPOS1 now points to the fifth element
in the reversed sequence: 2.

operator-

다른 반복기에서 하나의 반복기를 빼고 차이를 반환합니다.

template <class RandomIterator1, class RandomIterator2>
Tdiff operator-(
    const move_iterator<RandomIterator1>& left,
    const move_iterator<RandomIterator2>& right);

template <class RandomIterator1, class RandomIterator2>
Tdiff operator-(
    const reverse_iterator<RandomIterator1>& left,
    const reverse_iterator<RandomIterator2>& right);

매개 변수

left
반복기입니다.

right
반복기입니다.

Return Value

두 반복기의 차이입니다.

설명

첫 번째 템플릿 연산자는 left.base() - right.base()를 반환합니다.

두 번째 템플릿 연산자는 right.current - left.current를 반환합니다.

Tdiff는 반환된 식의 형식에 의해 결정됩니다. 그렇지 않으면 RandomIterator1::difference_type.

예제

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

int main()
{
    using namespace std;

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

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

    vector <int>::reverse_iterator rVPOS1 = vec.rbegin(),
        rVPOS2 = vec.rbegin();

    cout << "The iterators rVPOS1 & rVPOS2 initially point to "
        << "the first element\n in the reversed sequence: "
        << *rVPOS1 << "." << endl;

    for (int i = 1; i < 5; ++i)
    {
        rVPOS2++;
    }
	
    cout << "The iterator rVPOS2 now points to the fifth "
        << "element\n in the reversed sequence: "
        << *rVPOS2 << "." << endl;

    vector<int>::difference_type diff = rVPOS2 - rVPOS1;
    cout << "The difference: rVPOS2 - rVPOS1= "
        << diff << "." << endl;
}
The initial vector vec is: ( 0 2 4 6 8 10 ).
The iterators rVPOS1 & rVPOS2 initially point to the first element
in the reversed sequence: 10.
The iterator rVPOS2 now points to the fifth element
in the reversed sequence: 2.
The difference: rVPOS2 - rVPOS1= 4.