Presentation is loading. Please wait.

Presentation is loading. Please wait.

. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.

Similar presentations


Presentation on theme: ". The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface."— Presentation transcript:

1 . The Standard C++ Library

2 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface

3 Concepts A concept is a list of properties of a type. STL defines a hierarchy of concepts for containers, iterators, and element types. Concepts for element types include: Concept B is a refinement of concept A if it imposes some additional requirements on A. (Similar to inheritance) LessThan Comparable - types with operator<,... Equality Comparable - types with operator==,... Assignable - types with operator= and copy Ctor

4 4 Main Components AlgorithmsContainersIterators Function Objects AdaptorsStreamsStrings

5 u Holds copies of elements. u Assumes elements have: Copy Ctor & operator = u The standard defines the interface. u Two main classes  Sequential containers: list, vector,....  Associative containers: map, set... Containers Assignable - types with operator= and copy Ctor

6 Sequential Containers u Maintain a linear sequence of objects

7 list - a linked list of objects  Efficient insertion/deletion in front/end/middle vector - an extendable sequence of objects  Efficient insertion/deletion at end  Random access deque – double-ended queue  Efficient insertion/deletion at front/end  Random access Sequential Containers

8 8 Containers Example - vector  Array of elements of type T  Random Access  Can grow on as needed basis std::vector v(2); v[0]= 45; v[1]= 32; v.push_back(60); std::vector v(2); v[0]= 45; v[1]= 32; v.push_back(60);

9 Associative Containers u Supports efficient retrieval of elements (values) based on keys. (Typical) Implementation: u red-black binary trees u hash-table (SGI extension, not standard)

10 Sorted Associative Containers Set u A set of unique keys Map u Associate a value to key (associative array) u Unique value of each key Multiset Multimap u Same, but allow multiple values

11 Sorted Associative Containers & Order u Sorted Associative containers use operator< as default order u We can control order by using our own comparison function u To understand this issue, we need to use function object Function Objects

12 Anything that can be called as if a function. For example: Pointer to function A class that implements operator() Advantage of class: u Enable accumulating information in the function object Function Objects

13 Example template class less { public: bool operator()(const T& lhs, const T& rhs) { return lhs < rhs; } }; less cmp; // declare an object if( cmp(1,2) ) … if( less ()(1,2) ) … Creates temporary objects, and then call operator()

14 Using Comparators template<typename T, typename Cmp = less > class set { … } … set s1; set > s2;// same type

15 Using Comparators template<typename T, typename Cmp = less > class set { … } … set s3; MyComp cmp; set s4(cmp); Creates a new MyComp object. Use given MyComp object.

16 u Iterators are allow to traverse sequences u Main Methods  operator*  operator++, and operator— u Different types of iterators - to support read, write and random access u Containers define their own iterator types u Changing the container can invalidate the iterator Iterators

17 Output IteratorInput IteratorTrivial IteratorForward IteratorBi-directional IteratorRandom-Access Iterator ++, input++, output++, I/O++, --, I/O Pointer arithmetic input Iterators

18 Iterator Types u Output: write only and can write only once u Input: read many times each item u Forward supports both read and write u Bi-directional support also decrement u Random supports random access (just like C pointer)

19 Iterators & Containers Bidirectional iterators: u list, map, set Random access iterators: u vector Input/output/forward iterators: u iostreams

20 Iterators and Containers Container c … Container::iterator i; for( i = c.begin(); i != c.end(); ++i) // do something with *i Container c … Container::iterator i; for( i = c.begin(); i != c.end(); ++i) // do something with *i class Container { … typedef … iterator; // iterator type of container iterator begin(); // first element of container iterator end(); // element after last of container

21 Iterators & Map Suppose we work with: map dictionary; map ::iterator i; … i = dictionary.begin(); What is the type of *i ?

22 Iterators & Map Every STL container type Container defines Container::value_type Type of elements stored in container  This is the type returned by an iterator Container::value_type Container::iterator operator*();

23 Iterators & Map u Ok, so what type of elements does a map return?  map keeps pairs  KeyType key – “key” of entry  ValueType value – “value” of entry

24 Pairs template struct pair { typedef T1 first_type; typedef T2 first_type; T1 first; T2 second; pair( const T1& x, const T2& y ) : first(x), second(y) {} };

25 Map value_type template< typename Key, typename T, typename Cmp = less > class map { public: typedef pair value_type; typedef Key key_type; typedef T mapped_type; typedef Cmp key_compare; };

26 Using map iterator map dict; … map ::iterator i; for( i = dict.begin(); i != dict.end(); i++ ) { cout first << “ “ second << “\n”; }

27 u Good functionality, wrong interface u For example, adaptors of basic containers u Limited interface: stack queue Adaptors

28 u Most STL algorithms works on sequences u Sequences are passed as two iterators:  beginning element  element one after last u Algorithms depend on iterator type not on container type pq sequence [p,q) Algorithms

29 Copy template Out copy(In first, In last, Out res) { while (first != last) *res++ = *first++; return res; }

30 template <class RandomAccessIterator, class StrictWeakOrdering> void sort(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp); Sort

31 Cryptic error messages

32 New solution in g++, define before including a standard library header: #define _GLIBCXX_CONCEPT_CHECKS

33 u SGI: http://www.sgi.com/tech/stl/http://www.sgi.com/tech/stl/ u Dinkum: http://www.dinkumware.com/refxcpp.html http://www.dinkumware.com/refxcpp.html u http://www.cplusplus.com/ref/http://www.cplusplus.com/ref/ Links to documentation

34 34 Main Components AlgorithmsContainersIterators Function Objects AdaptorsStreamsStrings

35 Mutable class String { char* _data; mutable int _len; public: String(const char* data) : _data(data),_len(-1){} void capitalie() { char* p =_data; while (*p) *p= toupper(*p++); } int length() const { if (_len==-1) _len= strlen(_data); return _len; } };

36 Inserters


Download ppt ". The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface."

Similar presentations


Ads by Google