The Standard C Library - PowerPoint PPT Presentation

About This Presentation
Title:

The Standard C Library

Description:

A concept is a list of properties of a type. ... mutable int _len; public: String(const char* data) : _data(data),_len(-1){} void capitalie ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 37
Provided by: csHu
Category:

less

Transcript and Presenter's Notes

Title: The Standard C Library


1
The Standard C Library
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 operatorlt ,...
Equality Comparable - types with operator ,...
Assignable - types with operator and copy Ctor
4
Main Components
5
  • Holds copies of elements.
  • Assumes elements haveCopy Ctor operator
  • The standard defines the interface.
  • Two main classes
  • Sequential containers list, vector,....
  • Associative containersmap, set ...

Assignable - types with operator and copy Ctor
6
Sequential Containers
  • Maintain a linear sequence of objects

7
Sequential Containers
  • 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

8
Containers Example - vectorltTgt
  • Array of elements of type T
  • Random Access
  • Can grow on as needed basis

stdvectorltintgt v(2) v0 45 v1 32
v.push_back(60)
9
Associative Containers
  • Supports efficient retrieval of elements (values)
    based on keys.
  • (Typical) Implementation
  • red-black binary trees
  • hash-table (SGI extension, not standard)

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

11
Sorted Associative Containers Order
  • Sorted Associative containers use operatorlt as
    default order
  • We can control order by using our own comparison
    function
  • To understand this issue, we need to use function
    object

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

13
Example
  • templatelttypename Tgt
  • class less
  • public
  • bool operator()(const T lhs, const T rhs)
  • return lhs lt rhs
  • lessltintgt cmp // declare an object
  • if( cmp(1,2) )
  • if( lessltintgt()(1,2) )

14
Using Comparators
  • templatelttypename T,
  • typename Cmp lessltTgt gt
  • class set
  • setltintgt s1
  • setltint,lessltintgt gt s2// same type

15
Using Comparators
  • templatelttypename T,
  • typename Cmp lessltTgt gt
  • class set
  • setltint,MyComp gt s3
  • MyComp cmp
  • setltint,MyComp gt s4(cmp)

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

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

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

20
Iterators and Containers
  • class Container
  • typedef iterator // iterator type of
    container
  • iterator begin() // first element of
    container
  • iterator end() // element after last of
    container

Container c Containeriterator i for( i
c.begin() i ! c.end() i) // do
something with i
21
Iterators Map
Suppose we work with mapltstring,intgt
dictionary mapltstring,intgtiterator i i
dictionary.begin() What is the type of i ?
22
Iterators Map
  • Every STL container type Container defines
  • Containervalue_type
  • Type of elements stored in container
  • This is the type returned by an iterator
  • Containervalue_type
  • Containeriterator operator()

23
Iterators Map
  • Ok, so what type of elements does a map return?
  • mapltKeyType,ValueTypegt keeps pairs
  • KeyType key key of entry
  • ValueType value value of entry

24
Pairs
  • templatelt typename T1, typename T2gt
  • 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
  • templatelt typename Key, typename T,
  • typename Cmp lessltKeygt gt
  • class map
  • public
  • typedef pairltconst Key, Tgt value_type
  • typedef Key key_type
  • typedef T mapped_type
  • typedef Cmp key_compare

26
Using map iterator
  • mapltstring,intgt dict
  • mapltstring,intgtiterator i
  • for( i dict.begin()
  • i ! dict.end()
  • i )
  • cout ltlt i-gtfirst ltlt
  • ltlt i-gtsecond ltlt \n

27
  • Good functionality, wrong interface
  • For example, adaptors of basic containers
  • Limited interface
  • stackltT,Seqgt
  • queueltT,Seqgt

28
  • Most STL algorithms works on sequences
  • Sequences are passed as two iterators
  • beginning element
  • element one after last
  • Algorithms depend on iterator type
  • not on container type

29
Copy
  • templatelt typename In, typename Outgt
  • Out copy(In first, In last, Out res)
  • while (first ! last)
  • res first
  • return res

30
Sort
template ltclass RandomAccessIterator,
class StrictWeakOrderinggt void
sort(RandomAccessIterator first,
RandomAccessIterator last,
StrictWeakOrdering comp)
31
Cryptic error messages
32
Cryptic error messages
  • New solution in g, define before including a
  • standard library header

define _GLIBCXX_CONCEPT_CHECKS
33
Links to documentation
  • SGI http//www.sgi.com/tech/stl/
  • Dinkum http//www.dinkumware.com/refxcpp.html
  • http//www.cplusplus.com/ref/

34
Main Components
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)
_lenstrlen(_data) return _len
36
Inserters
Write a Comment
User Comments (0)
About PowerShow.com