Download presentation
Presentation is loading. Please wait.
1
Standard Template Library
C++ Review Standard Template Library CSCE 221H Texas A&M University 1 1
2
Outline Standard Template Library Containers Iterators Algorithms
Summary
3
Standard Template Library
A general purpose library of data structures (containers) and algorithms communicating through iterators. Plug each piece together and use in different applications. container generic algorithm sort vector iterator #include <vector> #include <algorithm> int main(){ int myArray[4]={2, 8, 5, 3}; std::vector<int> v(myArray, myArray+4); std::sort(v.begin(), v.end()); }
4
Containers Container is an object storing other objects which can be accessed by iterators. Associated types: X::value_type X::iterator X::const_iterator … X Type of a container a Object of type X
5
Containers Valid expressions a.begin() Beginning of container a
a.end() End of container a a.size() Size of container a a.empty() Whether container a is empty
6
Iterators Iterators are used to point to other objects.
Iterators play an interface between data structures and algorithms. container generic algorithm sort vector iterator
7
Iterators Input Iterator Output Iterator Forward Iterator
Get elements out from container Output Iterator Put elements into container Forward Iterator Get persistent elements from container Bidirectional Iterator Be able to get elements in a reversed order Random Access Iterator Arbitrarily access elements
8
Algorithms After defining iterators, they can be use to specify algorithms. #include <vector> #include <algorithm> int main(){ std::vector<int> v; v.push_back(2); v.push_back(8); v.push_back(5); v.push_back(3); vector<int>::iterator result = find(v.begin(), v.end(), 5); assert(result == v.end() || *result == 5); }
9
Summary STL contains important components: containers (data structures), iterators, and algorithms. All components are reusable and flexible.
10
Lab Assignment Create a class called Point with two private data members, a public function Print, and constructors. Use a container store 10 Points, initialize them with non-zero data, and print them. Write a function to find the Point which has the maximum y coordinate by any STL algorithm. The return value is a pair containing the corresponding x and y values.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.