stl速成

本文详细介绍了STL中的迭代器类型,包括前推迭代器、双向迭代器和随机访问迭代器,并展示了replace()、reverse()和random_shuffle()等算法的使用。此外,还探讨了函数对象,如发生器、绑定器和否定函数对象,并强调了STL在数据处理中的优势和灵活性。
摘要由CSDN通过智能技术生成

前推迭代器

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

template <class ForwardIterator, class T>
void replace (ForwardIterator first,
              ForwardIterator last,
              const T& old_value,
              const T& new_value);

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

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

双向迭代器

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

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

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

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

随机访问迭代器

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

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

template <class RandomAccessIterator>
void random_shuffle (RandomAccessIterator first,
                     RandomAccessIterator last);

使用方法:

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

迭代器技术

要学会使用迭代器和容器以及算法,需要学习下面的新技术。

流和迭代器

本书的很多例子程序使用I/O流语句来读写数据。例如:

int value;
cout << "Enter value: ";
cin >> value;
cout << "You entered " << value << endl;

对于迭代器,有另一种方法使用流和标准函数。理解的要点是将输入/输出流作为容器看待。因此,任何接受迭代器参数的算法都可以和流一起工作。

Listing 4. outstrm.cpp

#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;
}

函数Display()显示了如何使用一个输出流迭代器。下面的语句将容器中的值传输到cout输出流对象中:

copy(v.begin(), v.end(),
  ostream_iterator<int>(cout, "/t"));

第三个参数实例化了ostream_iterator<int>类型,并将它作为copy()函数的输出目标迭代器对象。“/t”字符串是作为分隔符。运行结果:

$ g++ outstrm.cpp
$ ./a.out
Before sorting
677   722   686   238   964   397   251   118   11    312
After sorting
11    118   238   251   312   397   677   686   722   964

这是STL神奇的一面『确实神奇』。为定义输出流迭代器,STL提供了模板类ostream_iterator。这个类的构造函数有两个参数:一个ostream对象和一个string值。因此可以象下面一样简单地创建一个迭代器对象:

ostream_iterator<int>(cout, "/n")

该迭代起可以和任何接受一个输出迭代器的函数一起使用。

插入迭代器

插入迭代器用于将值插入到容器中。它们也叫做适配器,因为它们将容器适配或转化为一个迭代器,并用于copy()这样的算法中。例如,一个程序定义了一个链表和一个矢量容器:

list<double> dList;
vector<double> dVector;

通过使用front_inserter迭代器对象,可以只用单个copy()语句就完成将矢量中的对象插入到链表前端的操作:

copy(dVector.begin(), dVector.end(), front_inserter(dList));

三种插入迭代器如下:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值