List

简介: 1 //我们可以象这样来定义一个STL的list: 2 #include <string> 3 #include <list> 4 int main (void) 5 { 6 list<string> Milkshakes; 7 ...
  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 <string103  #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  

 

目录
相关文章
|
11天前
|
存储 编译器 容器
list从0到1的突破
list从0到1的突破
9 0
|
2月前
|
存储 C++ 容器
【C++】list的认识与使用
【C++】list的认识与使用
|
3月前
|
算法 搜索推荐 C++
【C++】list的使用(下)
`C++` 中 `std::list` 的 `merge()`、`sort()` 和 `reverse()` 操作: - `merge(x)` 和 `merge(x, comp)`: 合并两个已排序的`list`,将`x`的元素按顺序插入当前`list`,`x`清空。比较可自定义。 - `sort()` 和 `sort(comp)`: 对`list`元素排序,保持等价元素相对顺序。内置排序基于稳定排序算法,速度较慢。 -reverse(): 反转`list`中元素的顺序。 这些操作不涉及元素构造/销毁,直接移动元素。注意,`sort()`不适合`std::list`,因链表结构不利于快速排序
|
3月前
|
编译器 C++ 容器
【C++】list的使用(上)
迭代器在STL中统一了访问接口,如`list`的`begin()`和`end()`。示例展示了如何使用正向和反向迭代器遍历`list`。注意`list`的迭代器不支持加减操作,只能用`++`和`--`。容器的`empty()`和`size()`用于检查状态和获取元素数。`front()`和`back()`访问首尾元素,`assign()`重载函数用于替换内容,`push_*/pop_*`管理两端元素,`insert()`插入元素,`erase()`删除元素,`resize()`调整大小,`clear()`清空容器。这些接口与`vector`和`string`类似,方便使用。
|
5月前
|
索引 Python
List
List
30 6
|
5月前
|
人工智能
B - Dima and To-do List
B - Dima and To-do List
31 0
|
11月前
|
存储 C++ 容器
C++ list
C++ list
|
缓存 编译器 C++
list的实现
list的实现
【C++】list的介绍和使用(下)
【C++】list的介绍和使用(下)
【C++】list的介绍和使用(下)
|
存储 C++ 容器
【C++】list的介绍和使用(上)
【C++】list的介绍和使用(上)
【C++】list的介绍和使用(上)