List

  STL的目的是标准化组件,这样就不用重新开发,可以使用现成的组件。STL现在是C++的一部分,因此不用额外安装什麽。它被内建在编译器之内。现在,来看一看STLlist

  定义一个list

  可以象这样来定义一个STLlist
#include string
#include
list
int main (void)
{
 liststring Milkshakes;
 return 0;
}
  liststring Milkshakes声明了liststring>模板类的一个实例,然后就是实例化该类的一个对象。

  使用list的成员函数push_backpush_front插入一个元素到list中:
#include string
#include
list

int main (void)
{
 liststring Milkshakes;
 Milkshakes.push_back("Chocolate");
 Milkshakes.push_back("Strawberry");
 Milkshakes.push_front("Lime");
 Milkshakes.push_front("Vanilla");
 return 0;
}

  现在有个4个字符串在list中。list的成员函数push_back()把一个对象放到一个list的后面,而 push_front()把对象放到前面。通常把一些错误信息push_back()到一个list中去,然后push_front()一个标题到list中,这样它就会在这个错误消息以前打印它了。

list
的成员函数empty()

  知道一个list是否为空很重要。如果list为空,empty()这个成员函数返回真。通常会这样使用它。通篇程序都用push_back()来把错误消息放到list中去。然后,通过调用empty() 就可以知道这个程序是否报告了错误。如果定义了一个list来放信息,一个放警告,一个放严重错误,就可以通过使用empty()轻易的说出到底有那种类型的错误发生了。
  
// Using a list to track and report program messages and status
#include
iostream.h
#include
string
#include
list
int main (void)
{
 #define OK 0
 #define INFO 1
 #define WARNING 2
 int return_code;
 liststring InfoMessages;
 liststring WarningMessages;

 // during a program these messages are loaded at various points
 InfoMessages.push_back("Info: Program started");
 // do work...
 WarningMessages.push_back("Warning: No Customer records have been found");
 // do work...

 return_code = OK;

 if (!InfoMessages.empty()) {
  // there were info messages
  InfoMessages.push_front("Informational Messages:");
  // ... print the info messages list, we'll see how later
  return_code = INFO;
 }

 if (!WarningMessages.empty()) {
  // there were warning messages
  WarningMessages.push_front("Warning Messages:");
  // ... print the warning messages list, we'll see how later
  return_code = WARNING;
 }

 // If there were no messages say so.
 if (InfoMessages.empty() && WarningMessages.empty()) {
  cout << "There were no messages " << endl;
 }

 return return_code;
}

 for循环来处理list中的元素

  想要遍历一个list,比如打印一个中的所有对象来看看list上不同操作的结果。要一个元素一个元素的遍历一个list可以这样做:
/*
|| How to print the contents of a simple STL list. Whew!
*/
#include
iostream.h
#include
string
#include
list

int main (void)
{
 liststring Milkshakes;
 liststring::iterator MilkshakeIterator;

 Milkshakes.push_back("Chocolate");
 Milkshakes.push_back("Strawberry");
 Milkshakes.push_front("Lime");
 Milkshakes.push_front("Vanilla");

 // print the milkshakes
 Milkshakes.push_front("The Milkshake Menu");
 Milkshakes.push_back("*** Thats the end ***");
 for (MilkshakeIterator=Milkshakes.begin(); MilkshakeIterator!=Milkshakes.end(); ++MilkshakeIterator)
 {
  // dereference the iterator to get the element
  cout << *MilkshakeIterator << endl;
 }
}

  这个程序定义了一个iterator MilkshakeIterator。把它指向了这个list的第一个元素。这可以调用Milkshakes.begin()来作到,它会返回一个指向list开头的iterator。然后把它和Milkshakes.end()返回值来做比较。

  容器的end()函数会返回一个指向容器的最后一个位置的iterator

  list容器不支持在iterator加一个数来指向隔一个的对象。就是说,不能用Milkshakes.begin()+2来指向list中的第三个对象,因为STLlist是以双链的list来实现的,它不支持随机存取。vectordeque(向量和双端队列)和一些其他的STL的容器可以支持随机存取。

 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) {
 liststring 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)
{
 listint 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,它就给NumberOf100Scores1

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

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

  函数对象被约定为STL算法调用operator时返回truefalse。它们根据这个来判定这个函数。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)
{
 liststring 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;
}

这个程序是这样工作的:定义一个函数对象类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)
{
 liststring 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;
}

  这个例子演示了如何向函数对象传递信息。可以定义任意想要的构造函数,可以在函数对象中做任何想做的处理,都可以合法编译通过。

  可以看到函数对象扩展了基本记数算法。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值