1 / 31

Chapter 14 Introduction to the Standard Template Library

Chapter 14 Introduction to the Standard Template Library. §14.1 Introduction §14.2 Containers §14.3 Iterators §14.4 Algorithms §14.5 Function Objects. §14.1 Introduction. Standard Template Library ( STL ) 标准模板库 A software library included in the C++ Standard Library Purpose

Download Presentation

Chapter 14 Introduction to the Standard Template Library

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 14 Introduction to the Standard Template Library §14.1 Introduction §14.2 Containers §14.3 Iterators §14.4 Algorithms §14.5 Function Objects

  2. §14.1 Introduction • Standard Template Library (STL) 标准模板库 • A software library included in the C++ Standard Library • Purpose • To standardize commonly used components • History • Originally developed by Alexander Stepanov in 1980s • Accepted as part of C++ standard in Feb. 1994

  3. Components of STL • Containers(容器) • A container object, such as vector, used to store a collection of data, often referred to as elements • Algorithms(算法) • Functions to manipulate data such as sorting, searching, and comparing elements. • Most of them use iterators to access the elements in the container. • Iterators(迭代器) • Objects that facilitate traversing through the elements in a container • Like built-in pointers that provide a convenient way to access and manipulate the elements in a container

  4. Components of STL Algorithm 1 Algorithm 2 Object 1 Object 2 Iterator 2 Iterator 1 Container Object 3 Iterator 3 Algorithm 3

  5. §14.2 Containers Containers • vector • deque • list Sequence containers • set • multiset • map • multimap Associative containers • stack • queue • priority_queue Derived containers linear data structures Non-linear (tree) containers Suitable for searching • Constrained versions of sequence containers

  6. Sequence Containers • Represent linear data structures • Three sequence containers • vector 向量, list 表, and deque 双端队列 • Differ only in performance Element 0 Last Element Element 2 Element 1 iterator Begin() end() int values[] = {1, 2, 3, 4 }; list<int> intList(values, values + 4);

  7. Sequence Containers

  8. Associative Containers • Represent non-linear data structures (tree) • Elements are sorted by keys • E.g. the name of student objects • Suitable for searching via keys • Set/Multiset: set of items • Map/Multimap: set of item pairs int values[] = {3, 5, 1, 7, 2, 2}; set<int> set1(values, values + 6); map<int, string> map1; map1.insert(map<int, string>::value_type(100, "John Smith"));

  9. Derived Containers • Containers adapted from the sequence containers • For handling special cases • Programmer can choose an appropriate sequence container for a derived containers stack<int, vector<int> > stack2; *: default one

  10. Containers supported by the STL

  11. Containers supported by the STL

  12. Common Functions in All Containers

  13. Common Functions in Sequence/Associative Containers

  14. Common Functions in Sequence Container

  15. Common Functions in Associative Containers

  16. §14.3 Iterators • Iterators are objects to facilitate traversing through the elements in a container • A iterator is like a built-inpointer that can manipulate the elements in a container random access bidirectional forward input output

  17. Iterators and Characteristics

  18. Iterator Operations

  19. Iterator Types Supported by Containers

  20. Iterator Demo vector<int> intVector; intVector.push_back(10); intVector.push_back(20); intVector.push_back(30); intVector.push_back(40); intVector.push_back(50); intVector.push_back(60); vector<int>::iterator p1 = intVector.begin(); for (; p1 != intVector.end(); p1++){ cout << *p1 << " "; } cout << endl << *(--p1) << endl; cout << *(p1 - 3) << endl; cout << p1[-3] << endl; *p1 = 1234; cout << *p1 << endl; IteratorDemo

  21. §14.4 Algorithms • More than sixty algorithms in STL • Five groups • Retrieve or non-mutating Algorithms • Mutating Algorithms • Sorting Algorithms • Set Algorithms • Relational Algorithms

  22. The copy Algorithm template <typename InputIterator, typename OutputIterator> copy(InputIterator beg, InputIterator end, OutputIterator targetPostition) int values[] = {1,2,3,4,5,6}; vector<int> intVector(5); list<int> intList(5); copy(values+2, values+4, intVector.begin()); copy(values, values+5, intList.begin()); #include <algorithm> #include <vector> #include <list> #include <iterator>

  23. Copy to Output Stream intVector.insert(intVector.begin(), 747); ostream_iterator<int> output(cout, " "); cout<<"\nAfter the insertion funciton, intVector: "; copy(intVector.begin(), intVector.end(), output);

  24. The copy Demo int values[] = {1,2,3,4,5,6}; vector<int> intVector(5); list<int> intList(5); copy(values+2, values+4, intVector.begin()); copy(values, values+5, intList.begin()); intVector.insert(intVector.begin(), 747); copy(intVector.begin(), intVector.begin()+4, intList.begin()); After initial copy intVector: 3 4 0 0 0 After initial copy intList: 1 2 3 4 5 After the insertion function, intVector: 747 3 4 0 0 0 After the copy function, intList: 747 3 4 0 5

  25. Caution • The target container in copying must contains enough elements to store the values from the source container int values[] = {1,2,3,4,5} vector<int> intVector; copy(values+2, values+4, intVector.begin());

  26. “sort” and “binary_search” template <typename RandomAccessIterator> void sort(RandomAccessIterator beg, RandomAccessIterator end) template <typename RandomAccessIterator, typename relationalOperator> void sort(RandomAccessIterator beg, RandomAccessIterator end, relationalOperator op) template <typename ForwardIterator, typename T> bool binary_search(ForwardIterator beg, ForwardIterator end, const T &value) template <typename ForwardIterator, typename T, typename strickWeakOrdering> bool binary_search(ForwardIterator beg, ForwardIterator end, const T &value, strickWeakOrdering op) SortDemo

  27. Demo of sort, binary_search int array1[] = {1, 7, 3, 4, 3, 3, 1, 2}; sort(array1, array1 + 8); binary_search(array1, array1 + 8, 4) sort(array1, array1 + 8, greater_equal<int>()); binary_search(array1, array1 + 8, 4, greater_equal<int>()) A function operator!

  28. §14.5 Function Objects • Object can be used as a function • “function”  “object” • Passing a function as a parameter • In <functional> header int main(){ int x[] = {10,50,30,40,20}; int y[] = {70,90,60,80}; sort(x,x+5,greater<int>()); sort(y,y+4); for (inti=0;i<5;i++) cout<<x[i]<<" "; cout<<"\n"; for (int j=0;j<4;j++) cout<<y[j]<<" "; cout<<"\n"; int z[9]; merge(x,x+5,y,y+4,z); for (inti=0;i<9;i++) cout<<z[i]<<" "; cout<<"\n"; }

  29. Functors in <functional>

  30. Customized Function Objects • By defining member function operator() in a class class My { public: intoperator()(int a) {return a;} } ; My mo; int x = mo(0); class BiggerThan{ public: const int testValue; BiggerThan(int x) : testValue(x) { } bool operator()(int val) const { return val > testValue; } }; template <typename T> class greater : binary_function <T,T,bool> { public: bool operator() (const T& x, const T& y) const{return x>y;} }; int my[] = {1, 7, 3, 4, 3, 3, 1, 2}; sort(my, my + 8, Mygreater<int>()); BiggerThan bt(10); cout<<bt(11)<<" "<<bt(9)<<endl; My m; cout<<m(4)<<endl; 1 0 4

  31. A Summary • Concepts • STL, Container, Iterator, Algorithm • Three types of containers • Function, Operation, Algorithm • Functional object • Features of different containers • Main functions of containers • Features of STL algorithm • Use of functional object

More Related