分享

C 学习:list容器详解(二)

 昵称30612043 2016-04-14

 接C++学习:list容器详解(一)

用STL的通用算法for_each来处理list中的元素

  使用STL list和 iterator,我们要初始化、比较和给iterator增量来遍历这个容器。STL通用的for_each 算法能够减轻我们的工作。

/*
|| How to print a simple STL list MkII
*/
#include <iostream.h>
#include <string>
#include <list>
#include <algorithm>

PrintIt (string& StringToPrint) {
 cout << StringToPrint << endl;
}

int main (void) {
 list<string> FruitAndVegetables;
 FruitAndVegetables.push_back('carrot');
 FruitAndVegetables.push_back('pumpkin');
 FruitAndVegetables.push_back('potato');
 FruitAndVegetables.push_front('apple');
 FruitAndVegetables.push_front('pineapple');

 for_each (FruitAndVegetables.begin(), FruitAndVegetables.end(), PrintIt);
}


  在这个程序中我们使用STL的通用算法for_each()来遍历一个iterator的范围,然后调用PrintIt()来处理每个对象。 我们不需要初始化、比较和给iterator增量。for_each()为我们漂亮的完成了这些工作。我们执行于对象上的 操作被很好的打包在这个函数以外了,我们不用再做那样的循环了,我们的代码更加清晰了。

  for_each算法引用了iterator范围的概念,这是一个由起始iterator和一个末尾iterator指出的范围。 起始iterator指出操作由哪里开始,末尾iterator指明到哪结束,但是它不包括在这个范围内。   

     用STL的通用算法count()来统计list中的元素个数

  STL的通用算法count()和count_it()用来给容器中的对象记数。就象for_each()一样,count()和count_if() 算法也是在iterator范围内来做的。

  让我们在一个学生测验成绩的list中来数一数满分的个数。这是一个整型的List。

/*
|| How to count objects in an STL list
*/
#include <list>
#include <algorithm>
#
int main (void)
{
 list<int> Scores;
 #
 Scores.push_back(100); Scores.push_back(80);
 Scores.push_back(45); Scores.push_back(75);
 Scores.push_back(99); Scores.push_back(100);
 #
 int NumberOf100Scores(0);
 count (Scores.begin(), Scores.end(), 100, NumberOf100Scores);
 #
 cout << 'There were ' << NumberOf100Scores << ' scores of 100' << endl;
}


  count()算法统计等于某个值的对象的个数。上面的例子它检查list中的每个整型对象是不是100。每次容器中的对象等于100,它就给NumberOf100Scores加1。这是程序的输出:

There were 2 scores of 100


  用STL的通用算法count_if()来统计list中的元素个数

  count_if()是count()的一个更有趣的版本。他采用了STL的一个新组件,函数对象。count_if() 带一个函数对象的参数。函数对象是一个至少带有一个operator()方法的类。有些STL算法作为参数接收 函数对象并调用这个函数对象的operator()方法。

  函数对象被约定为STL算法调用operator时返回true或false。它们根据这个来判定这个函数。举个例子会 说的更清楚些。count_if()通过传递一个函数对象来作出比count()更加复杂的评估以确定一个对象是否应该被 记数。在这个例子里我们将数一数牙刷的销售数量。我们将提交包含四个字符的销售码和产品说明的销售记录。

/*
|| Using a function object to help count things
*/
#include <string>
#include <list>
#include <algorithm>

const string ToothbrushCode('0003');

class IsAToothbrush
{
 public:
  bool operator() ( string& SalesRecord )
  {
   return SalesRecord.substr(0,4)==ToothbrushCode;
  }
};

int main (void)
{
 list<string> SalesRecords;

 SalesRecords.push_back('0001 Soap');
 SalesRecords.push_back('0002 Shampoo');
 SalesRecords.push_back('0003 Toothbrush');
 SalesRecords.push_back('0004 Toothpaste');
 SalesRecords.push_back('0003 Toothbrush');

 int NumberOfToothbrushes(0);
 count_if (SalesRecords.begin(), SalesRecords.end(),
 IsAToothbrush(), NumberOfToothbrushes);

 cout << 'There were '
 << NumberOfToothbrushes
 << ' toothbrushes sold' << endl;
}


  这是这个程序的输出:

  There were 2 toothbrushes sold 这个程序是这样工作的:定义一个函数对象类IsAToothbrush,这个类的对象能判断出卖出的是否是牙刷 。如果这个记录是卖出牙刷的记录的话,函数调用operator()返回一个true,否则返回false。

  count_if()算法由第一和第二两个iterator参数指出的范围来处理容器对象。它将对每个 IsAToothbrush()返回true的容器中的对象增加NumberOfToothbrushes的值。

  最后的结果是NumberOfToothbrushes这个变量保存了产品代码域为'0003'的记录的个数,也就是牙刷的个数。

  注意count_if()的第三个参数IsAToothbrush(),它是由它的构造函数临时构造的一个对象。你可以把IsAToothbrush类的一个临时对象 传递给count_if()函数。count_if()将对该容器的每个对象调用这个函数。

    使用count_if()的一个更加复杂的函数对象

  我们可以更进一步的研究一下函数对象。假设我们需要传递更多的信息给一个函数对象。我们不能通过 调用operator来作到这点,因为必须定义为一个list的中的对象的类型。 然而我们通过为IsAToothbrush指出一个非缺省的构造函数就可以用任何我们所需要的信息来初始化它了。 例如,我们可能需要每个牙刷有一个不定的代码。我们可以把这个信息加到下面的函数对象中:

/*
|| Using a more complex function object
*/
#include <iostream.h>
#include <string>
#include <list>
#include <algorithm>

class IsAToothbrush
{
 public:
  IsAToothbrush(string& InToothbrushCode) :
  ToothbrushCode(InToothbrushCode) {}
  bool operator() (string& SalesRecord)
  {
   return SalesRecord.substr(0,4)==ToothbrushCode;
  }
 private:
  string ToothbrushCode;
};

int main (void)
{
 list<string> SalesRecords;

 SalesRecords.push_back('0001 Soap');
 SalesRecords.push_back('0002 Shampoo');
 SalesRecords.push_back('0003 Toothbrush');
 SalesRecords.push_back('0004 Toothpaste');
 SalesRecords.push_back('0003 Toothbrush');

 string VariableToothbrushCode('0003');

 int NumberOfToothbrushes(0);
 count_if (SalesRecords.begin(), SalesRecords.end(),
 IsAToothbrush(VariableToothbrushCode),
 NumberOfToothbrushes);
 cout << 'There were '
 << NumberOfToothbrushes
 << ' toothbrushes matching code '
 << VariableToothbrushCode
 << ' sold'
 << endl;
}

  程序的输出是:

  There were 2 toothbrushes matching code 0003 sold 这个例子演示了如何向函数对象传递信息。你可以定义任意你想要的构造函数,你可以再函数对象中做任何你 想做的处理,都可以合法编译通过。

  你可以看到函数对象真的扩展了基本记数算法。

  到现在为止,我们都学习了:

   ·定义一个list

   ·向list中加入元素

   ·如何知道list是否为空

   ·如何使用for循环来遍历一个list

   ·如何使用STL的通用算法for_each来遍历list

   ·list成员函数begin() 和 end() 以及它们的意义

   ·iterator范围的概念和一个范围的最后一个位置实际上并不被处理这一事实

   ·如何使用STL通用算法count()和count_if()来对一个list中的对象记数

   ·如何定义一个函数对象

  我选用这些例子来演示list的一般操作。如果你懂了这些基本原理,你就可以毫无疑问的使用STL了 建议你作一些练习。我们现在用一些更加复杂的操作来扩展我们的知识,包括list成员函数和STL通用算法。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多