C++ Primer Plus学习笔记之STL迭代器

C++ Primer Plus学习笔记之STL迭代器

一,为何使用迭代器?

模板使算法独立于存储的数据类型,而迭代器使算法独立于使用的容器类型。

二,迭代器的类型

1,输入迭代器

2,输出迭代器

3,正向迭代器

4,双向迭代器

5,随机访问迭代器

三,迭代器的层次结构

迭代器的类型形成了一个层次结构。

正向迭代器具有输入和输出迭代器的全部功能,同时还有自己的功能。

双向迭代器具有正向迭代器的全部功能,同时还有自己的功能。

随机访问迭代器具有正向迭代器的全部功能,同时还有自己的功能。

根据特定迭代器类型编写的算法可以使用该迭代器,也可以使用具有所需功能的任何其他迭代器。

所以具有随机迭代器的容器可以使用为输入迭代器编写的算法。

为什么需要这么多迭代器呢?

目的是为了在编写算法时,尽可能使用要求最低的迭代器,并让它适用于容器的最大区间。这样,

通过使用级别最低的输入迭代器,find()函数便可以用于任何包含可读取值的容器。而sort()函数

由于需要随机访问迭代器,所以只能用于支持这种迭代器的容器。

注意:各种迭代器的类型并不是确定的,而只是一种概念上的描述。

迭代器是一系列要求,而不是类型,比如指针也可以满足一类迭代器的要求—正向迭代器。

所以STL算法可以使用任何满足其要求的迭代器实现。

四,迭代器的使用

1,将指针用作迭代器

#include <iostream.h>

#include <algorithm>

 

using namespace std;

 

#define SIZE 100

int iarray[SIZE];

 

int main()

{

  iarray[20] = 50;

  int* ip = find(iarray, iarray + SIZE, 50);

  if (ip == iarray + SIZE)

    cout << "50 not found in array" << endl;

  else

    cout << *ip << " found in array" << endl;

  return 0;

}

2,流及迭代器

#include <iostream.h>

#include <stdlib.h>    // Need random(), srandom()

#include <time.h>      // Need time()

#include <algorithm>   // Need sort(), copy()

#include <vector>      // Need vector

 

using namespace std;

 

void Display(vector<int>& v, const char* s);

 

int main()

{

  // Seed the random number generator

  srandom( time(NULL) );

 

  // Construct vector and fill with random integer values

  vector<int> collection(10);

  for (int i = 0; i < 10; i++)

    collection[i] = random() % 10000;;

 

  // Display, sort, and redisplay

  Display(collection, "Before sorting");

  sort(collection.begin(), collection.end());

  Display(collection, "After sorting");

  return 0;

}

 

// Display label s and contents of integer vector v

void Display(vector<int>& v, const char* s)

{

  cout << endl << s << endl;

  copy(v.begin(), v.end(),

    ostream_iterator<int>(cout, "\t"));

  cout << endl;

}

3,几种插入迭代器

#include <iostream.h>

#include <algorithm>

#include <list>

 

using namespace std;

 

int iArray[5] = { 1, 2, 3, 4, 5 };

 

void Display(list<int>& v, const char* s);

 

int main()

{

  list<int> iList;

 

  // Copy iArray backwards into iList

  copy(iArray, iArray + 5, front_inserter(iList));

  Display(iList, "Before find and copy");

 

  // Locate value 3 in iList

  list<int>::iterator p =

    find(iList.begin(), iList.end(), 3);

 

  // Copy first two iArray values to iList ahead of p

  copy(iArray, iArray + 2, inserter(iList, p));

  Display(iList, "After find and copy");

 

  return 0;

}

 

void Display(list<int>& a, const char* s)

{

  cout << s << endl;

  copy(a.begin(), a.end(),

    ostream_iterator<int>(cout, " "));

  cout << endl;

}

4,容器迭代器

#include <iostream.h>

#include <algorithm>

#include <vector>

 

using namespace std;

 

vector<int> intVector(100);

 

void main()

{

  intVector[20] = 50;

  vector<int>::iterator intIter =

    find(intVector.begin(), intVector.end(), 50);

  if (intIter != intVector.end())

    cout << "Vector contains value " << *intIter << endl;

  else

    cout << "Vector does not contain 50" << endl;

}

 



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值