C/C++ For Linux <ul><ul><li>Session 9 </li></ul></ul><ul><ul><li>C++ - Session 4 </li></ul></ul>
Outline <ul><li>Templates </li></ul><ul><li>STL session 1 </li></ul><ul><ul><li>Containers and iterators </li></ul></ul>
The Problem <ul><li>Decoupling type-independent and type-dependent data </li></ul><ul><ul><li>Consider containers </li></u...
Templates <ul><li>Syntax </li></ul><ul><li>Parameters </li></ul><ul><li>Specialization </li></ul><ul><li>Function Template...
Template Specialization <ul><li>Order: More -> Less </li></ul><ul><li>Compile time </li></ul><ul><li>Default parameters </...
Type ID <ul><li>reinterpret_cast: bitwise </li></ul><ul><li>static_cast: some relation </li></ul><ul><li>const_cast: remov...
Standard Template Library <ul><li>Containers & Iterators </li></ul><ul><li>Algorithms & Functionals </li></ul><ul><li>Stri...
Containers <ul><li>Holds other objects </li></ul><ul><li>Design Problem: </li></ul><ul><ul><li>Generic types held (don't c...
STL Solution <ul><li>Templates for generic types </li></ul><ul><li>Iterators to decouple iteration </li></ul><ul><li>Adapt...
Iterators <ul><li>Points to position of element </li></ul><ul><li>Operators for element access </li></ul><ul><ul><li>“Dere...
Functors <ul><li>Function object </li></ul><ul><li>Binary and unary functors </li></ul><ul><li>Predicates </li></ul><ul><l...
Common Container Interface <ul><li>iterator, const_iterator, reverse_iterator </li></ul><ul><li>begin(), end(), rbegin(), ...
Vector <ul><li>Fast access, expensive resize </li></ul><ul><li>Access: [], at() </li></ul><ul><li>reserve()/resize() </li>...
List <ul><li>Expensive access, quick resize </li></ul><ul><li>push_front(), front(), pop_front </li></ul><ul><li>splice(),...
Deque <ul><li>Double ended queue </li></ul><ul><li>Front methods from list </li></ul><ul><li>Also parameterized by compare...
Sequence Adapters <ul><li>Parameterized by Container </li></ul><ul><li>queue </li></ul><ul><li>priority_queue </li></ul><u...
Associative Containers <ul><li>Unique </li></ul><ul><ul><li>Pair, map, hash_map, set </li></ul></ul><ul><li>Multiple </li>...
Map/Multimap <ul><li><Key, Value, Allocator, Cmp> </li></ul><ul><li>[“key”], find </li></ul><ul><li>Pairs </li></ul><ul><l...
Additional <ul><li>set/multiset:  </li></ul><ul><ul><li>always sorted </li></ul></ul><ul><ul><li>No Value </li></ul></ul><...
Upcoming SlideShare
Loading in …5
×

Sysprog 9

1,003 views
906 views

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,003
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Sysprog 9

  1. 1. C/C++ For Linux <ul><ul><li>Session 9 </li></ul></ul><ul><ul><li>C++ - Session 4 </li></ul></ul>
  2. 2. Outline <ul><li>Templates </li></ul><ul><li>STL session 1 </li></ul><ul><ul><li>Containers and iterators </li></ul></ul>
  3. 3. The Problem <ul><li>Decoupling type-independent and type-dependent data </li></ul><ul><ul><li>Consider containers </li></ul></ul><ul><li>Generic programming </li></ul><ul><ul><li>Consider algorithms </li></ul></ul><ul><li>Still preserve safe typing </li></ul><ul><ul><li>As opposed to void * </li></ul></ul>
  4. 4. Templates <ul><li>Syntax </li></ul><ul><li>Parameters </li></ul><ul><li>Specialization </li></ul><ul><li>Function Templates </li></ul>
  5. 5. Template Specialization <ul><li>Order: More -> Less </li></ul><ul><li>Compile time </li></ul><ul><li>Default parameters </li></ul><ul><li>GNU </li></ul><ul><ul><li>Include </li></ul></ul><ul><ul><li>Compilation Performance </li></ul></ul><ul><li>Readability </li></ul><ul><ul><li>Code </li></ul></ul><ul><ul><li>Compiler messages </li></ul></ul>
  6. 6. Type ID <ul><li>reinterpret_cast: bitwise </li></ul><ul><li>static_cast: some relation </li></ul><ul><li>const_cast: remove const </li></ul><ul><li>dynamic_cast: dynamic data </li></ul><ul><ul><li>Better type checking </li></ul></ul><ul><ul><li>More expensive </li></ul></ul><ul><li>typeid operator </li></ul><ul><ul><li>type_info::name() </li></ul></ul>
  7. 7. Standard Template Library <ul><li>Containers & Iterators </li></ul><ul><li>Algorithms & Functionals </li></ul><ul><li>Strings & Streams </li></ul><ul><li>std namespace </li></ul><ul><li>STL Manual: http://www.sgi.com/tech/stl/ </li></ul>
  8. 8. Containers <ul><li>Holds other objects </li></ul><ul><li>Design Problem: </li></ul><ul><ul><li>Generic types held (don't care element) </li></ul></ul><ul><ul><li>Some commonality in iface (don't care container) </li></ul></ul><ul><ul><ul><li>Iterating </li></ul></ul></ul><ul><ul><ul><li>Algorithms </li></ul></ul></ul><ul><ul><li>Some specifics in iface </li></ul></ul><ul><ul><li>Efficiency </li></ul></ul>
  9. 9. STL Solution <ul><li>Templates for generic types </li></ul><ul><li>Iterators to decouple iteration </li></ul><ul><li>Adapters for interface adjustments </li></ul><ul><li>Functors to decouple algorithm operations from obj/contner code. </li></ul>
  10. 10. Iterators <ul><li>Points to position of element </li></ul><ul><li>Operators for element access </li></ul><ul><ul><li>“Dereference” * </li></ul></ul><ul><ul><li>Access -> </li></ul></ul><ul><ul><li>Move positions ++, -- </li></ul></ul><ul><ul><li>Equality == </li></ul></ul><ul><ul><li>Assignment = </li></ul></ul>
  11. 11. Functors <ul><li>Function object </li></ul><ul><li>Binary and unary functors </li></ul><ul><li>Predicates </li></ul><ul><li>Adapters </li></ul><ul><li>Algorithms </li></ul>
  12. 12. Common Container Interface <ul><li>iterator, const_iterator, reverse_iterator </li></ul><ul><li>begin(), end(), rbegin(), rend() </li></ul><ul><li>push_back(), back(), pop_back() </li></ul><ul><li>insert, erase, clear </li></ul><ul><li>size(), empty() </li></ul><ul><li>Template Parameters </li></ul><ul><ul><li>Element Type, Allocator </li></ul></ul>
  13. 13. Vector <ul><li>Fast access, expensive resize </li></ul><ul><li>Access: [], at() </li></ul><ul><li>reserve()/resize() </li></ul><ul><li>max_size() </li></ul><ul><li>Ctors: range, size </li></ul>
  14. 14. List <ul><li>Expensive access, quick resize </li></ul><ul><li>push_front(), front(), pop_front </li></ul><ul><li>splice(), merge(), sort() </li></ul>
  15. 15. Deque <ul><li>Double ended queue </li></ul><ul><li>Front methods from list </li></ul><ul><li>Also parameterized by compare </li></ul><ul><li>[] from vector </li></ul>
  16. 16. Sequence Adapters <ul><li>Parameterized by Container </li></ul><ul><li>queue </li></ul><ul><li>priority_queue </li></ul><ul><li>stack </li></ul>
  17. 17. Associative Containers <ul><li>Unique </li></ul><ul><ul><li>Pair, map, hash_map, set </li></ul></ul><ul><li>Multiple </li></ul><ul><ul><li>Multimap, multiset, hash_multimap </li></ul></ul>
  18. 18. Map/Multimap <ul><li><Key, Value, Allocator, Cmp> </li></ul><ul><li>[“key”], find </li></ul><ul><li>Pairs </li></ul><ul><li>lower_bound </li></ul><ul><li>upper_bound </li></ul><ul><li>equal_range </li></ul>
  19. 19. Additional <ul><li>set/multiset: </li></ul><ul><ul><li>always sorted </li></ul></ul><ul><ul><li>No Value </li></ul></ul><ul><li>hash_map/hash_multimap </li></ul><ul><ul><li>Hash parameter </li></ul></ul>

×