C Standard Library Facilities - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

C Standard Library Facilities

Description:

C++ Standard Library Facilities Basic run-time language support allocation, run-time type info, ... C standard library Strings and I/O international character sets ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 24
Provided by: PeterMa
Category:

less

Transcript and Presenter's Notes

Title: C Standard Library Facilities


1
C Standard Library Facilities
  • Basic run-time language support
  • allocation, run-time type info, ...
  • C standard library
  • Strings and I/O
  • international character sets, localization, ...
  • Containers and algorithms using containers
  • vector, list, map,... traversal, sort, merge,...
  • Support for numerical computation
  • complex, BLAS-like operations, slices, ...

STL
2
STL Components
  • containers
  • data structure templates
  • vector, list, deque, (priority-)queue, stack
  • (multi-)map, (multi-)set
  • generic algorithms
  • independent of containers
  • iterators
  • pointer-like objects used to cycle through all
    the elements stored in a container

3
containers, iterators, algorithms
vector ltTgt
sort
list ltTgt
find
4
containers, iterators, algorithms
  • include ltvectorgt include ltvector.hgt
  • include ltalgorithmgtinclude ltalgo.hgt
  • include ltiostreamgt include ltiostream.hgt
  • using namespace std
  • int ia1033,17,11,88,43,99,6,9,12,7
  • int main()
  • vectorltintgt x(ia,ia10)
  • sort(x.begin(), x.end())

5
  • // grab value to search for
  • int s_value
  • cin gtgt s_value
  • // search for an element
  • vectorltintgtiterator found
  • foundfind(x.begin(),x.end(),s_value)
  • if(found ! x.end())
  • cout ltlt "search value found!\n"
  • else
  • cout ltlt "search value not found!\n"

6
list instead of vector
  • include ltlistgt
  • include ltalgorithmgt
  • include ltiostreamgt
  • using namespace std
  • int ia1033,17,11,88,43,99,6,9,12,7
  • int main()
  • listltintgt x(ia,ia10)
  • sort(x.begin(), x.end())

7
  • // grab value to search for
  • int s_value
  • cin gtgt s_value
  • // search for an element
  • vectorltintgtiterator found
  • foundfind(x.begin(),x.end(),s_value)
  • if(found ! x.end())
  • cout ltlt "search value found!\n"
  • else
  • cout ltlt "search value not found!\n"

8
Algorithms with built-in arrays
  • include ltalgorithmgt
  • include ltiostreamgt
  • using namespace std
  • int ia1033,17,11,88,43,99,6,9,12,7
  • int main()
  • int s_value
  • cout ltlt "enter search value "
  • cin gtgt s_value
  • int found
  • foundfind(ia0,ia10,s_value)
  • if(found ! ia10) ...

9
Containers
  • Sequence Containers
  • Vector
  • List
  • Deque
  • Stack
  • Queue Adaptor
  • Priority_queue
  • Almost Containers
  • Built-in arrays, strings, valarrays, bitsets
  • Associative Containers
  • Map
  • Multimap
  • Set
  • Multiset

10
string Type
  • include ltiostreamgt
  • include ltstringgt
  • include ltalgorithmgt
  • using namespace std
  • string s1("Hello")
  • string s2("World")
  • cout ltlt s1 " " s2 '\n'
  • string s3(s1 " " s2)
  • cout ltlt '\"'ltlt s3 ltlt "\" has " ltlt s3.length()
  • ltlt " characters" ltlt endl
  • replace(s3.begin(), s3.end(), 'W', 'w')
  • cout ltlt s3 ltlt endl

11
Constructors
  • container()
  • container(n) (not for ass. cont.)
  • container(n,x) (not for ass. cont.)
  • container(first,last)
  • container(c)
  • container()

12
Stack, Queue, List Operations
  • push_back() pop_back()
  • push_front() pop_front()
  • insert(p,x)
  • insert(p,n,x)
  • insert(p,first,last)
  • erase(p)
  • erase(first,last)
  • clear()

13
Container Summary
14
Algorithms
  • copy
  • sort
  • find
  • fill
  • partition
  • insert, delete
  • union, intersection
  • accumulate
  • ...

15
Iterators
  • The glue that makes it possible to use generic
    algorithms and orthogonalize those algorithms
    from the data structures
  • An iterator i is a generalized means of
    traversing a data structure
  • for an array, an array index or a pointer
  • Dereferencing an iterator, i, is guaranteed to
    give the item, but an iterator does not obey all
    pointer operations

16
Iterators, Element Access
  • begin()
  • end()
  • rbegin()
  • rend()
  • front()
  • back()
  • at()
  • Points to first element
  • Points to one-past-last element
  • Points to first element of reverse seq.
  • Points to one-past-last element of rev. seq.
  • First element
  • Last element
  • Subscripting, unchecked access
  • Subscripting, checked access

17
Iterators (continued)
  • containerltTgtiterator firstc.begin()
  • containerltTgtiterator lastc.end()
  • first,last)
  • i
  • first last
  • i ! last
  • i n (long jump)
  • i x
  • x i

18
Iterator Operations
19
STL Components (continued)
  • function objects
  • an instance of a class that defines the
    parenthesis operator as a member function
  • to be passed as argument instead of functions
  • adaptors
  • container adapters provide a restricted interface
  • eg. stack and queue are adaptors to vector, list
    or dequeue
  • allocators

20
Nonmodifying Sequence Operations
  • for_each()
  • find()
  • find_if()
  • find_first_of()
  • adjacent_find()
  • count()
  • count_if()
  • mismatch()
  • equal()
  • search()
  • find_end()
  • search_n()

listltPersongt all ... for_each(all.begin(),all.en
d(),Print(cout))
21
Function objects
  • class bThan
  • public
  • bThan(int x) testValue(x)
  • const int testValue
  • bool operator() (int val)
  • return val gt testValue
  • listltintgtiterator firstBig
  • find_if(aList.begin(),aList.end(),bThan(12))

22
STL Components
  • containers
  • data structure templates
  • vector, list, deque, (priority-)queue, stack
  • (multi-)map, (multi-)set
  • generic algorithms
  • independent of containers
  • iterators
  • pointer-like objects used to cycle through all
    the elements stored in a container

23
Future of OO
  • STL is not OO, is OOP dead?
  • Not all problems are best solved in OOP fashion!
  • Many problems are best solved in an OOP manner.
  • Know as much as you can about as many styles of
    programming as you can, an use the style most
    appropriate to the problem.
Write a Comment
User Comments (0)
About PowerShow.com