400 likes | 716 Views
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
E N D
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 • To standardize commonly used components • History • Originally developed by Alexander Stepanov in 1980s • Accepted as part of C++ standard in Feb. 1994
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
Components of STL Algorithm 1 Algorithm 2 Object 1 Object 2 Iterator 2 Iterator 1 Container Object 3 Iterator 3 Algorithm 3
§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
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);
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"));
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
§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
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
§14.4 Algorithms • More than sixty algorithms in STL • Five groups • Retrieve or non-mutating Algorithms • Mutating Algorithms • Sorting Algorithms • Set Algorithms • Relational Algorithms
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>
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);
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
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());
“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
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!
§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"; }
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
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