📜  C++ STL中的迭代器

📅  最后修改于: 2021-05-30 09:42:36             🧑  作者: Mango

先决条件:迭代器简介
迭代器用于指向STL容器的内存地址。它们主要用于数字,字符等的顺序。它们减少了程序的复杂性和执行时间。

迭代器的操作

1. begin() :-此函数用于返回容器的开始位置

2.端(): -该函数用于之后的容器的端部位置返回。

// C++ code to demonstrate the working of
// iterator, begin() and end()
#include
#include // for iterators
#include // for vectors
using namespace std;
int main()
{
    vector ar = { 1, 2, 3, 4, 5 };
      
    // Declaring iterator to a vector
    vector::iterator ptr;
      
    // Displaying vector elements using begin() and end()
    cout << "The vector elements are : ";
    for (ptr = ar.begin(); ptr < ar.end(); ptr++)
        cout << *ptr << " ";
      
    return 0;    
}

输出:

The vector elements are : 1 2 3 4 5 

3. advance() :-此函数用于递增迭代器的位置,直到其参数中提到的指定数字为止。

// C++ code to demonstrate the working of
// advance()
#include
#include // for iterators
#include // for vectors
using namespace std;
int main()
{
    vector ar = { 1, 2, 3, 4, 5 };
      
    // Declaring iterator to a vector
    vector::iterator ptr = ar.begin();
      
    // Using advance() to increment iterator position
    // points to 4
    advance(ptr, 3);
      
    // Displaying iterator position
    cout << "The position of iterator after advancing is : ";
    cout << *ptr << " ";
      
    return 0;
      
}

输出:

The position of iterator after advancing is : 4 

4. next() :-该函数返回在迭代前进其参数中提到的位置之后迭代器将指向的新迭代器。

5. prev() :-此函数返回递减迭代器在其参数中提到的位置后将指向的新迭代器。

// C++ code to demonstrate the working of
// next() and prev() 
#include
#include // for iterators
#include // for vectors
using namespace std;
int main()
{
    vector ar = { 1, 2, 3, 4, 5 };
      
    // Declaring iterators to a vector
    vector::iterator ptr = ar.begin();
    vector::iterator ftr = ar.end();
     
     
    // Using next() to return new iterator
    // points to 4
    auto it = next(ptr, 3);
      
    // Using prev() to return new iterator
    // points to 3
    auto it1 = prev(ftr, 3);
      
    // Displaying iterator position
    cout << "The position of new iterator using next() is : ";
    cout << *it << " ";
    cout << endl;
      
    // Displaying iterator position
    cout << "The position of new iterator using prev()  is : ";
    cout << *it1 << " ";
    cout << endl;
      
    return 0; 
}

输出:

The position of new iterator using next() is : 4 
The position of new iterator using prev()  is : 3 


6. inserter()
:-此函数用于在容器中的任何位置插入元素。它接受2个参数,即容器和迭代器以定位必须插入元素的位置

// C++ code to demonstrate the working of
// inserter()
#include
#include // for iterators
#include // for vectors
using namespace std;
int main()
{
    vector ar = { 1, 2, 3, 4, 5 };
    vector ar1 = {10, 20, 30}; 
      
    // Declaring iterator to a vector
    vector::iterator ptr = ar.begin();
     
    // Using advance to set position
    advance(ptr, 3);
      
    // copying 1 vector elements in other using inserter()
    // inserts ar1 after 3rd position in ar
    copy(ar1.begin(), ar1.end(), inserter(ar,ptr));
      
    // Displaying new vector elements
    cout << "The new vector after inserting elements is : ";
    for (int &x : ar) 
        cout << x << " ";
      
    return 0;    
}

输出:

The new vector after inserting elements is : 1 2 3 10 20 30 4 5 

迭代器的类型:

  1. 输入迭代器
  2. 输出迭代器
  3. 正向迭代器
  4. 双向迭代器
  5. 随机访问迭代器
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”