STL_iterator迭代器(1)——迭代器的分类

一、容器迭代器

尽管C++指针也是迭代器,但用的更多的是容器迭代器。容器迭代器用法和iterdemo.cpp一样,但和将迭代器申明为指针变量不同的是,你可以使用容器类方法来获取迭代器对象。两个典型的容器类方法是begin()和end()。它们在大多数容器中表示整个容器范围。其他一些容器还使用rbegin()和rend()方法提供反向迭代器,以按反向顺序指定对象范围。下面的程序创建了一个矢量容器(STL的和数组等价的对象),并使用迭代器在其中搜索。

1 //容器迭代器 
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6  
 7 vector<int>intVector(100);
 8  
 9 int main()
10 {
11       intVector[20] = 50;
12       
13       const vector<int>::iterator intIter = find(intVector.begin(), intVector.end(), 50);
14       //const使迭代器成为常量,而不是他指向的对象不允许改变 
15     if (intIter != intVector.end())
16       {
17           //intIter = intVector.begin();
18           *intIter = 123;
19        cout << "Vector contains value " << *intIter << endl;
20      }
21       else
22       {
23         cout << "Vector does not contain 50" << endl;
24      }
25    return 0;
26 }

二、输入迭代器

输入迭代器是最普通的类型。输入迭代器至少能够使用==和!=测试是否相等;使用来访问数据;使用++操作来递推迭代器到下一个元素或到达past-the-end* 值。为了理解迭代器和STL函数是如何使用它们的,现在来看一下find()模板函数的定义:

1 template <class InputIterator, class T>
2 InputIterator find(InputIterator first, InputIterator last, const T& value) 
3 {
4     while (first != last && *first != value) ++first;
5     return first;
6 }

find()算法中,注意如果first和last指向不同的容器,该算法可能陷入死循环。

三、输出迭代器

输出迭代器缺省只写,通常用于将数据从一个位置拷贝到另一个位置。由于输出迭代器无法读取对象,因此你不会在任何搜索和其他算法中使用它。要想读取一个拷贝的值,必须使用另一个输入迭代器(或它的继承迭代器)。

1 //输出迭代器
 2 #include <iostream>
 3 #include <algorithm>   // Need copy()
 4 #include <vector>      // Need vector
 5 using namespace std;
 6  
 7 double darray[10] = {1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9};
 8  
 9 vector<double> vdouble(10);
10  
11 int main()
12 {
13       vector<double>::iterator outputIterator = vdouble.begin();
14       
15       copy(darray, darray + 10, outputIterator);
16       
17       while (outputIterator != vdouble.end()) 
18     {
19         cout << *outputIterator << endl;
20         outputIterator++;
21       }
22   return 0;
23 }

当使用copy()算法的时候,你必须确保目标容器有足够大的空间,或者容器本身是自动扩展的。

四、前推迭代器

前推迭代器能够读写数据值,并能够向前推进到下一个值。但是没法递减。replace()算法显示了前推迭代器的使用方法。

1 template <class ForwardIterator, class T>
2 void replace (ForwardIterator first,
3               ForwardIterator last,
4               const T& old_value,
5               const T& new_value);

使用replace()将[first,last]范围内的所有值为old_value的对象替换为new_value。

replace(vdouble.begin(), vdouble.end(), 1.5, 3.14159);

五、双向迭代器

双向迭代器要求能够增减。如reverse()算法要求两个双向迭代器作为参数:

1 template <class BidirectionalIterator>
2 void reverse (BidirectionalIterator first, BidirectionalIterator last);

使用reverse()函数来对容器进行逆向排序:

reverse(vdouble.begin(), vdouble.end());

六、随机访问迭代器

随机访问迭代器能够以任意顺序访问数据,并能用于读写数据(不是const的C++指针也是随机访问迭代器)。STL的排序和搜索函数使用随机访问迭代器。随机访问迭代器可以使用关系操作符作比较。

random_shuffle()函数随机打乱原先的顺序。申明为:

1 template <class RandomAccessIterator>
2 void random_shuffle (RandomAccessIterator first,
3                      RandomAccessIterator last);
random_shuffle(vdouble.begin(), vdouble.end());

原文链接: https://www.cnblogs.com/pingge/p/3216702.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/97357

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午4:11
下一篇 2023年2月10日 上午4:13

相关推荐