L06_stlstr - Standard Template Library (STL) and Strings...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Standard Template Library (STL) and Strings Nyhoff: 5,9 Deitel: 18,22 COMP152 Spring 2010 D.Y. Yeung Outline STL Vector List Deque Container adapters Stack Queue String and its operations COMP152 2 Consult on-line C++ tutorials for detailed usage and examples STL (Standard Template Library) Defines powerful, template-based, reusable components and algorithms to process them Implements many common data structures Developed by Alexander Stepanov and Meng Lee Involving many advanced C++ coding features and implementation Conceived and designed for performance and flexibility Similar interfaces between vector, list, and deque, with storage always handled transparently and automatically (expanding and contracting as needed) behind programmer’s back. COMP152 3 STL (Standard Template Library) Components : Containers: Generic "off-the-shelf" class templates for storing collections of data Algorithms: Generic "off-the-shelf" function templates for operating on containers Iterators: Generalized "smart" pointers that allow algorithms to operate on almost any container COMP152 4 Container Class Algorithms Iterator Containers in Standard Template Library Sequence containers Represent linear data structures Start from index/location 0 Associative containers Nonlinear containers Store key/value pairs Container adapters Implemented as constrained sequence containers “Near-containers” Pointer-based arrays, strings, bitsets and valarrays COMP152 5 Container Type STL Containers Sequential deque, list, vector Associative map, multimap, set, multiset Adapters priority_queue, queue, stack Near-containers bitset, valarray, string The vector Container A type-independent pattern for an array class Capacity can expand Self contained Can be conceptualized as a powerful array C-style pointer-based arrays have great potential for errors and several shortcomings C++ does not check whether subscripts fall outside the range of the array Two arrays cannot be meaningfully compared with equality or relational operators One array cannot be assigned to another using the assignment operators COMP152 6 The vector Container Requires header file <vector> A data structure with contiguous memory locations Efficient, direct access to any element via subscript operator Or member function at , which provides range-checking Commonly used when data must be sorted and easily accessible via indices (subscripts) When additional memory is needed Transparently allocates larger contiguous memory, copies elements and deallocates old memory (behind user’s back) Supports random-access iterators All STL algorithms can operate on vectors COMP152 7 The vector Container Declaration Constructors COMP152 8 template <typename T> class vector { . . . } vector<int> v, // empty vector v1(100), // 100 elements of type int...
View Full Document

This note was uploaded on 08/25/2010 for the course COMP COMP152 taught by Professor D.y.yeung during the Spring '10 term at HKUST.

Page1 / 73

L06_stlstr - Standard Template Library (STL) and Strings...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online