共用方式為


<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 類型的物件。

傳回值

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 類型的物件。

傳回值

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 類型的物件。

傳回值

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 類型的物件。

傳回值

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 類型的物件。

傳回值

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 類型的物件。

傳回值

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_iteratorreverse_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
要為常數 move_iterator 或常數 reverse_iterator 調整位移的位置數目。

right
要調整位移的迭代器。

傳回值

傳回總和 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
迭代器。

傳回值

兩個反覆運算器之間的差異。

備註

第一個範本運算子會傳回 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.