先决条件: 迭代器简介 迭代器用于指向 STL 容器。它们主要用于数字序列、字符序列等。它们降低了程序的复杂性和执行时间。
null
迭代器的运算 :-
1.开始 :-此函数用于返回 起始位置 从容器中取出。
2.结束() :-此函数用于返回 之后 末端位置 从容器中取出。
// C++ code to demonstrate the working of // iterator, begin() and end() #include<iostream> #include<iterator> // for iterators #include<vector> // for vectors using namespace std; int main() { vector< int > ar = { 1, 2, 3, 4, 5 }; // Declaring iterator to a vector vector< int >::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.预付款 :-此函数用于 增加迭代器的位置 直到其参数中提到的指定数字。
// C++ code to demonstrate the working of // advance() #include<iostream> #include<iterator> // for iterators #include<vector> // for vectors using namespace std; int main() { vector< int > ar = { 1, 2, 3, 4, 5 }; // Declaring iterator to a vector vector< int >::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.下一步 :-这个功能 返回新的迭代器 迭代器将在 推进阵地 在其论点中提到。
5.prev() :-这个功能 返回新的迭代器 迭代器会指出 在减少位置之后 在其论点中提到。
// C++ code to demonstrate the working of // next() and prev() #include<iostream> #include<iterator> // for iterators #include<vector> // for vectors using namespace std; int main() { vector< int > ar = { 1, 2, 3, 4, 5 }; // Declaring iterators to a vector vector< int >::iterator ptr = ar.begin(); vector< int >::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.插入器() :-此函数用于 在任何位置插入元件 在容器里。它接受 2个参数,容器和迭代器,以确定必须插入元素的位置 .
// C++ code to demonstrate the working of // inserter() #include<iostream> #include<iterator> // for iterators #include<vector> // for vectors using namespace std; int main() { vector< int > ar = { 1, 2, 3, 4, 5 }; vector< int > ar1 = {10, 20, 30}; // Declaring iterator to a vector vector< int >::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
迭代器的类型:
本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END