博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
List
阅读量:7042 次
发布时间:2019-06-28

本文共 4215 字,大约阅读时间需要 14 分钟。

1 //我们可以象这样来定义一个STL的list:  2 #include <string>   3  #include <list>   4  int main (void)   5  {   6      list<string> Milkshakes;   7  return0;   8  }  9   10  //使用list的成员函数push_back和push_front插入一个元素到list中: 11 #include <string>  12  #include <list>  13  int main (void)  14  {  15      list<string> Milkshakes;  16      Milkshakes.push_back("Chocolate");  17     Milkshakes.push_back("Strawberry");  18      Milkshakes.push_front("Lime");  19      Milkshakes.push_front("Vanilla");  20      return0;  21  } 22   23  //用for循环来处理list中的元素  24 // 因为STL的list是以双链的list来实现的, 它不支持随机存取。vector和deque(向量和双端队列)和一些其他的STL的容器可以支持随机存取。  25  26 /*  27  || How to print the contents of a simple STL list. Whew!  28  */  29  #include <iostream.h>  30  #include <string>  31  #include <list>  32  int main (void)  33  {  34    list<string> Milkshakes;  35    list<string>::iterator MilkshakeIterator;  36    Milkshakes.push_back("Chocolate");  37    Milkshakes.push_back("Strawberry");  38    Milkshakes.push_front("Lime");  39    Milkshakes.push_front("Vanilla");  40    // print the milkshakes  41    Milkshakes.push_front("The Milkshake Menu");  42    Milkshakes.push_back("*** Thats the end ***");  43    for (MilkshakeIterator=Milkshakes.begin(); MilkshakeIterator!=Milkshakes.end(); ++MilkshakeIterator)  44    {  45      // dereference the iterator to get the element  46      cout << *MilkshakeIterator << endl;  47    }  48  } 49   50   51  //用STL的通用算法for_each来处理list中的元素  52 //使用STL list和 iterator,我们要初始化、比较和给iterator增量来遍历这个容器。STL通用的for_each 算法能够减轻我们的工作。 53  54   /*  55  || How to print a simple STL list MkII  56  */  57  #include <iostream.h>  58  #include <string>  59  #include <list>  60  #include <algorithm>  61  PrintIt (string& StringToPrint) {  62    cout << StringToPrint << endl;  63  }  64  int main (void) {  65    list<string> FruitAndVegetables;  66    FruitAndVegetables.push_back("carrot");  67    FruitAndVegetables.push_back("pumpkin");  68    FruitAndVegetables.push_back("potato");  69    FruitAndVegetables.push_front("apple");  70   FruitAndVegetables.push_front("pineapple");  71    for_each (FruitAndVegetables.begin(), FruitAndVegetables.end(), PrintIt);  72  } 73   74   75  //用STL的通用算法count()来统计list中的元素个数  76 //STL的通用算法count()和count_it()用来给容器中的对象记数。就象for_each()一样,count()和count_if() 算法也是在iterator范围内来做的。  77 //让我们在一个学生测验成绩的list中来数一数满分的个数。这是一个整型的List。 78 /*  79  || How to count objects in an STL list  80  */  81  #include <list>  82  #include <algorithm>  83  #  84  int main (void)  85  {  86    list<int> Scores;  87    #  88    Scores.push_back(100); Scores.push_back(80);  89    Scores.push_back(45); Scores.push_back(75);  90    Scores.push_back(99); Scores.push_back(100);  91    #  92    int NumberOf100Scores(0);  93    count (Scores.begin(), Scores.end(), 100, NumberOf100Scores);  94    #  95    cout << "There were " << NumberOf100Scores << " scores of 100" << endl;  96  } 97   98  //用STL的通用算法count_if()来统计list中的元素个数  99 /* 100  || Using a function object to help count things 101  */ 102  #include <string> 103  #include <list> 104  #include <algorithm> 105  conststring ToothbrushCode("0003"); 106  class IsAToothbrush 107  { 108    public: 109      booloperator() ( string& SalesRecord ) 110      { 111        return SalesRecord.substr(0,4)==ToothbrushCode; 112      } 113  }; 114  int main (void) 115  { 116    list<string> SalesRecords; 117    SalesRecords.push_back("0001 Soap"); 118    SalesRecords.push_back("0002 Shampoo"); 119    SalesRecords.push_back("0003 Toothbrush"); 120    SalesRecords.push_back("0004 Toothpaste"); 121    SalesRecords.push_back("0003 Toothbrush"); 122    int NumberOfToothbrushes(0); 123    count_if (SalesRecords.begin(), SalesRecords.end(), 124    IsAToothbrush(), NumberOfToothbrushes); 125    cout << "There were " 126         << NumberOfToothbrushes 127       << " toothbrushes sold" << endl; 128  }129  130  131  132  133  134  //取出最后一个元素135     cout << "------------------------------------------------" << endl << "操作:取出最后一个元素" << endl;136     cout << lInt.back() << endl;137 138     //取出最前一个元素139     cout << "------------------------------------------------" << endl << "操作:取出最前一个元素" << endl;140     cout << lInt.front() << endl;141 142

 

转载地址:http://zvxal.baihongyu.com/

你可能感兴趣的文章
释放LINUX内存(请使用火狐浏览器浏览本页面)
查看>>
Andrew Ng 深度学习笔记-01-week3-课程
查看>>
Android获取通过XML设置的空间的高宽
查看>>
生活的苦逼
查看>>
在iptables防火墙下开启vsftpd的端口
查看>>
Mysql、MariaDB 新型主从集群配置GTID
查看>>
Linux HA Cluster的实例演示(2)
查看>>
Delphi之word报表
查看>>
unity的默认文件目录及脚本之间的执行顺序
查看>>
angular 定时函数
查看>>
移动端app测试关注点
查看>>
Android 仿QQ消息界面
查看>>
a demo for how to use QThread
查看>>
扩展欧几里德算法
查看>>
【原创】多字节版本下MFC控件处理字符集的BUG
查看>>
ntp服务器
查看>>
子线程中刷新了UI
查看>>
UIPopoverController事件分发
查看>>
记一次在线安装postgresql-9.4的问题
查看>>
zabbix/自动发现规则
查看>>