STL中的(list)链表
来源:互联网 发布:java array length 编辑:程序博客网 时间:2024/06/01 20:48
1 定义一个list
#include <iostream>#include <string>#include <list>using namespace std;int main (void){ list<string> Milkshakes; return 0;}
2 使用list的成员函数push_back和push_front插入一个元素到list中
#include <iostream>#include <string>#include <list>using namespace std;int main (void){ list<string> Milkshakes; Milkshakes.push_back("Chocolate"); Milkshakes.push_back("Strawberry"); Milkshakes.push_front("Lime"); Milkshakes.push_front("Vanilla"); return 0;}
3、用for循环来处理list中的元素
#include <iostream>#include <algorithm>#include <string>#include <list>using namespace std;int main (void){ list<string> Milkshakes; list<string>::iterator MilkshakeIterator; Milkshakes.push_back("Chocolate"); Milkshakes.push_back("Strawberry"); Milkshakes.push_front("Lime"); Milkshakes.push_front("Vanilla"); Milkshakes.push_front("The Milkshake Menu"); Milkshakes.push_back("*** Thats the end ***"); for (MilkshakeIterator=Milkshakes.begin();MilkshakeIterator!=Milkshakes.end();++MilkshakeIterator) cout << *MilkshakeIterator << endl; return 0;}
4、用STL的通用算法for_each来处理list中的元素
#include <iostream>#include <string>#include <list>#include <algorithm>using namespace std;void 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); return 0;}
5、用list成员函数remove()从list中删除元素。
#include <iostream>#include <algorithm>#include <string>#include <list>using namespace std;void PrintIt (const string& StringToPrint){ cout << StringToPrint << endl;}int main (void){ list<string> Birds; Birds.push_back("cockatoo"); Birds.push_back("galah"); Birds.push_back("cockatoo"); Birds.push_back("rosella"); Birds.push_back("corella"); cout << "Original list with cockatoos" << endl; for_each(Birds.begin(), Birds.end(), PrintIt); Birds.remove("cockatoo"); cout << "Now no cockatoos" << endl; for_each(Birds.begin(), Birds.end(), PrintIt); return 0;}
6、使用STL通用算法remove()从list中删除元素
通用算法remove()使用和list的成员函数不同的方式工作。一般情况下不改变容器的大小,通用remove()算法返回一个指向新的list的结尾的iterator。从开始到这个新的结尾(不含新结尾元素)的范围 包含了remove后剩下所有元素。你可以用list成员函数erase函数来删除从新结尾到老结尾的部分。
#include <iostream>#include <algorithm>#include <string>#include <list>using namespace std;void PrintIt(string &AString){ cout << AString << endl;}int main (void){ list<string> Birds; list<string>::iterator NewEnd; Birds.push_back("cockatoo"); Birds.push_back("galah"); Birds.push_back("cockatoo"); Birds.push_back("rosella"); Birds.push_back("king parrot"); cout << "Original list" << endl; for_each(Birds.begin(), Birds.end(), PrintIt); NewEnd = remove(Birds.begin(), Birds.end(), "cockatoo"); cout << endl << "List according to new past the end iterator" << endl; for_each(Birds.begin(), NewEnd, PrintIt); cout << endl << "Original list now. Care required!" << endl; for_each(Birds.begin(), Birds.end(), PrintIt); return 0;}
7、使用list成员函数从list中删除元素
list成员函数pop_front()删掉list中的第一个元素,pop_back()删掉最后一个元素。函数erase()删掉由一个iterator指出的元素。还有另一个erase()函数可以删掉一个范围的元素。
#include <iostream>#include <list>#include <algorithm>#include <string.h>using namespace std;int main (void){ list<int> list1; for (int i = 0; i < 10; ++i) list1.push_back(i); list1.pop_front(); list1.pop_back(); list1.erase(list1.begin()); list1.erase(list1.begin(), list1.end()); cout << "list contains " << list1.size() << " elements" << endl; return 0;}
8、list的成员函数empty()
如果list为空,empty()这个成员函数返回真。
#include <iostream>#include <list>#include <algorithm>#include <string.h>using namespace std;int main(void){ list<string> s; s.push_back("asdfg"); s.push_back("asdfg"); if(!s.empty()) cout<<"NO"<<endl; list<string>::iterator MilkshakeIterator; for (MilkshakeIterator=s.begin();MilkshakeIterator!=s.end();MilkshakeIterator++) s.remove("asdfg"); if(!s.empty()) cout<<"NO"<<endl; return 0;}
9、用list的成员函数插入元素到list中
list的成员函数push_front()和push_back()分别把元素加入到list的前面和后面。你可以使用insert() 把对象插入到list中的任何地方。
insert()可以加入一个对象,一个对象的若干份拷贝,或者一个范围以内的对象。这里是一些插入对象到list中的例子:
#include <list>#include <string.h>#include <iostream>#include <algorithm>using namespace std;int main (void){ list<int> list1; for (int i = 0; i < 10; ++i) list1.push_back(i); list1.insert(list1.begin(), -1); list1.insert(list1.end(), 10); int IntArray[2] = {11,12}; list1.insert(list1.end(), &IntArray[0], &IntArray[2]); return 0;}
10、使用list的成员函数sort()排序一个list。
要排序一个list,我们要用list的成员函数sort(),而不是通用算法sort()。所有我们用过的算法都是通用算法。然而,在STL中有时容器支持它自己对一个特殊算法的实现,这通常是为了提高性能
在这个例子中,list容器有它自己的sort算法,这是因为通用算法仅能为那些提供随机存取里面元素的容器排序,而由于list是作为一个连接的链表实现的,它不支持对它里面的元素随机存取。所以就需要一个特殊的 sort()成员函数来排序list。
由于各种原因,容器在性能需要较高或有特殊效果需求的场合支持外部函数(extra functions), 这通过利用构造函数的结构特性可以作到。
#include <iostream>#include <string.h>#include <list>#include <algorithm>using namespace std;void PrintIt (string& StringToPrint){ cout << StringToPrint << endl;}int main (void){ list<string> Staff; list<string>::iterator PeopleIterator; Staff.push_back("John"); Staff.push_back("Bill"); Staff.push_back("Tony"); Staff.push_back("Fidel"); Staff.push_back("Nelson"); cout << "The unsorted list " << endl; for_each(Staff.begin(), Staff.end(), PrintIt ); Staff.sort(); cout << "The sorted list " << endl; for_each(Staff.begin(), Staff.end(), PrintIt); return 0;}
11、使用STL通用算法find()在list中查找对象
我们如何在list中查找东西呢?
STL的通用算法find()和find_if()可以做这些。
就象for_each(), count(), count_if() 一样,这些算法也使用iterator范围,这个范围指出一个list或任意其他容器中的一部分来处理。通常首iterator指着开始的位置,次iterator指着停止处理的地方。由次iterator指出的元素不被处理。
这是find()如何工作:
#include <iostream>#include <string>#include <list>#include <algorithm>using namespace std;int main (void){ list<string> Fruit; list<string>::iterator FruitIterator; Fruit.push_back("Apple"); Fruit.push_back("Pineapple"); Fruit.push_back("Star Apple"); FruitIterator = find (Fruit.begin(), Fruit.end(), "Pineapple"); if (FruitIterator == Fruit.end()) cout << "Fruit not found in list" << endl; else cout << *FruitIterator << endl; return 0;}
12、使用STL通用算法find_if()在list中搜索对象
这是find()的一个更强大的版本。这个例子演示了find_if(),它接收一个函数对象的参数作为参数,并使用它来做更复杂的评价对象是否和给出的查找条件相付。
假设我们的list中有一些按年代排列的包含了事件和日期的记录。我们希望找出发生在1997年的事件。
这是find()的一个更强大的版本。这个例子演示了find_if(),它接收一个函数对象的参数作为参数, 并使用它来做更复杂的评价对象是否和给出的查找条件相付。假设我们的list中有一些按年代排列的包含了事件和日期的记录。我们希望找出发生在1997年的事件。
#include <iostream>#include <string>#include <list>#include <algorithm>using namespace std;class EventIsIn1997{public: bool operator () (string& EventRecord) { return EventRecord.substr(12,4)=="1997"; }};int main (void){ list<string> Events; Events.push_back("07 January 1995 Draft plan of house prepared"); Events.push_back("07 February 1996 Detailed plan of house prepared"); Events.push_back("10 January 1997 Client agrees to job"); Events.push_back("15 January 1997 Builder starts work on bedroom"); Events.push_back("30 April 1997 Builder finishes work"); list<string>::iterator EventIterator = find_if (Events.begin(), Events.end(), EventIsIn1997()); if (EventIterator==Events.end()) cout << "Event not found in list" << endl; else cout << *EventIterator << endl; return 0;}
13、.使用STL通用算法search在list中找一个序列
一些字符在STL容器中很好处理,让我们看一看一个难处理的字符序列。我们将定义一个list来放字符。
list<char> Characters;
现在我们有了一个字符序列,它不用任何帮助就知道然后管理内存。它知道它是从哪里开始、到哪里结束。 它非常有用。我不知道我是否说过以null结尾的字符数组。
让我们加入一些我们喜欢的字符到这个list中:
Characters.push_back('\0');
Characters.push_back('\0');
Characters.push_back('1');
Characters.push_back('2');
我们将得到多少个空字符呢?
int NumberOfNullCharacters(0);
count(Characters.begin(), Characters.end(), '\0', NumberOfNullCharacters);
cout << "We have " << NumberOfNullCharacters << endl;
让我们找字符'1'
list<char>::iterator Iter;
Iter = find(Characters.begin(), Characters.end(), '1');
cout << "We found " << *Iter << endl;
这个例子演示了STL容器允许你以更标准的方法来处理空字符。现在让我们用STL的search算法来搜索容器中 的两个null。就象你猜的一样,STL通用算法search()用来搜索一个容器,但是是搜索一个元素串,不象find()和find_if() 只搜索单个的元素。
#include <iostream>#include <string>#include <list>#include <algorithm>using namespace std;int main(void){ list<char> TargetCharacters; list<char> ListOfCharacters; TargetCharacters.push_back('\0'); TargetCharacters.push_back('\0'); ListOfCharacters.push_back('1'); ListOfCharacters.push_back('2'); ListOfCharacters.push_back('\0'); ListOfCharacters.push_back('\0'); list<char>::iterator PositionOfNulls = search(ListOfCharacters.begin(), ListOfCharacters.end(), TargetCharacters.begin(), TargetCharacters.end()); if (PositionOfNulls!=ListOfCharacters.end()) cout << "We found the nulls" << endl; return 0;}
- STL中的(list)链表
- STL中的list容器
- STL中的list容器
- STL中的list小结
- stl中的list
- STL中的list详解
- STL中的list
- STL-list中的spilce()
- STL-list中的merge()
- STL中的list
- STL中的list
- 【STL】模拟STL中的List轮
- STL中的List排序问题
- STL中的List排序问题
- STL中的deque和list
- stl中的list学习笔记
- 模拟实现STL中的list
- 模拟实现STL中的list
- [leetcode]: 202. Happy Number
- javac的使用
- JavaScript变量和作用域
- C++排序算法之选择排序
- 解决hibernate查询对象criteria比较日期是否同天
- STL中的(list)链表
- eclipse提示java compiler level does not match the version of the installed java
- 洛谷OJ
- at 命令
- 5.20-DP优化总结
- STL容器BitSet(位图)——1道腾讯笔试题的正确打开方式
- 最短路径—Dijkstra算法和Floyd算法
- MySQL 报错注入
- Python的引用、浅拷贝、深拷贝的一个很好的小例子