C++ Programming Standard Library

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Advertisements

Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
More on the STL vector list stack queue priority_queue.
Quick Overview of STL STL = Standard Template Library Main concept : Container, Iterator Application : Linked list, Stack etc.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Data Structures Using C++1 Chapter 13 Standard Template Library (STL) II.
Rossella Lau Lecture 12, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 12: An Introduction to the STL  Basic.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
Data Structures Using C++ 2E
Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
PRESENTED BY: RAJKRISHNADEEPAK.VUYYURU SWAMYCHANDAN.DONDAPATI VINESHKUMARREDDY.LANKA RAJSEKHARTIRUMALA KANDURI ALAN.
STL-Associative Containers. Associative Containers Sequence containers : "This is item 0, this is item 1, this is item 2…“ Associative containers : –
Generic Programming Using the C++ Standard Template Library.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
Data Structures Using C++ 2E Chapter 13 Standard Template Library (STL) II.
C++ STL CSCI 3110.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
Lecture 7 : Intro. to STL (Standard Template Library)
The Standard Template Library Container Classes Version 1.0.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Object-Oriented Programming (OOP) Lecture No. 41
Standard Template Library
Module 20/21/22: The Standard Template Library
C++ Standard Template Library
CS212: Object Oriented Analysis and Design
Lecture 6 of Computer Science II
Concepts of Programming Languages
Cpt S 122 – Data Structures Abstract Data Types
Programming with ANSI C ++
Standard Template Library
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Working with Strings Lecture 2 Hartmut Kaiser
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Collections Intro What is the STL? Templates, collections, & iterators
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
C++ STL Vector Container
CS212: Object Oriented Analysis and Design
Working with Strings Lecture 2 Hartmut Kaiser
Introduction to C++ STL
priority_queue<T>
Chapter 9 One-Dimensional Arrays
STL Iterators Separating Container from Data Access.
The Standard Template Library
Iterators and STL Containers
Lecture 8 : Intro. to STL (Standard Template Library)
STL (Standard Template Library)
Standard Template Library
C++ Programming: chapter 10 – STL
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
An Introduction to STL.
Standard Template Library
Presentation transcript:

C++ Programming Standard Library Strings Containers

Organization The SL library is organized into several parts. STL (Standard Template Library): Containers Iterators Algorithms Strings Streams Already explained in the previous presentation Numerics Internationalization Will not be explained in this course

Strings String classes enable you to use strings as normal types that cause no problems for the user Different from “ordinary strings” of type char* or const char* Copy, assignment and compare functions, as for fundamental types, are already available - no worry about memory corruption Defined in the header <string> Type: std::string - chain of characters of char type

String Operations Assignment: String size: Search in a string: Conversion to C-string (char*): std::string s = "abcd"; std::string t = s; t += “efgh”; std::string s = "abcd"; int size = s.size(); std::string s = "abcd"; int pos = s.find("bc"); std::string s = "abcd"; char* cs = s.c_str();

STL STL = Standard Template Library The heart of SL Provides a variety of collection classes that meet different needs, together with several algorithms that operate on them Programmers can forget about programming dynamic arrays, search algorithms etc. Just need to choose the appropriate container and call its functions to process data All components are templates Can be used for arbitrary element types Extremely efficient Cooperation of different well-structured components: Containers, iterators and algorithms

Containers Manage a collection of elements Sequence containers = ordered collections in which every element has a certain position (independent of the value) Vector: 1 dimensional array List: doubly linked list Associative containers = sorted collections (the actual position of an element depends on its value) Set / Multiset: Element sorted according to their own value Map / Multimap: Elements are key/value pairs, sorted accordingly

Vector In the header <vector> Type: std::vector<T> - where T is the element type Useful functions: push_back(elem) Add an element at the end at(idx) or [idx] Return the idx-th element front(), back() Return the first, the last element size() Return the vector size clear() Remove all elements #include <iostream> #include <vector> int main () { std::vector<int> myVector; myVector.push_back(3); myVector.push_back(4); std::cout << myVector[0] << std::endl; myVector.at(1) = 5; std::cout << myVector.back() << std::endl; myVector.clear(); return 0; }

List In the header <list> Type: std::list<T> - where T is the element type Useful functions: push_front(elem), push_back(elem) Add an element at the beginning, at the end pop_front(), pop_back() Remove the element at the beginning, at the end front(), back(), size(),clear() Same as for vector #include <iostream> #include <list> int main () { std::list<int> myList; myList.push_front(3); myList.push_front(2); myList.push_back(4); myList.pop_front(); myList.pop_back(); std::cout << myList.front() << std::endl; std::cout << myList.back() return 0; } Note that some methods are common to both vector and list (and also other containers): size(), clear(), ...

Iterators Iterator = object that can “iterate”(navigate) over elements It represents a certain position in a container All containers provide the same basic functions that allow iterators to navigate over their elements Functions defined for all containers: begin(), end() Return an iterator that represents the beginning, the end of elements in the container. The end is the position behind the last element v.begin() v.end() T vector it

Iterators Header files: All containers define their own iterator type, there is no special header for using container iterators Types: container_type::iterator container_type::const_iterator Basic operations: Operator * Returns the element of the actual position Operator ++ Lets the iterator step forward to the next element Operators == and != Return the result whether two iterators represents the same position or not Operator = Assign an iterator (the position of the element to which it refers)

Iterators - Example #include <iostream> #include <vector> int main() { std::vector<int> myVector; // ... fill the vector here // Iterating over vector elements std::vector<int>::iterator it; for ( it = myVector.begin(); it != myVector.end(); ++it ) { int i = *it; std::cout << "i = " << i << std::endl; } return 0;

When to Use which Container Vector By default, you should use a vector If you need to access the N-th element Insertion and removal can be costly Very efficient when browsing the container List If you need to insert or remove an element often in the middle Inclusion in constant time Access to the N-th element by iterating from the 1st Map If you need an element via a key A list maintained sorted when inserting new elements Associative list