SlideShare a Scribd company logo
Overview
Ping-Lun Liao
S T
L
Outline
STL
Container
Iterator
Algorithm
Function Object
What is STL
Standard Template Library.
ANSI/ISO C++
Key Person
Alexander A. Stepanov
STL 的不同實現版本
HP STL :這是第一個版本
P.J. Plauger STL
Rouge Wave STL
SGI STL
STLport
Reference:
C++ STL編程輕鬆入門基礎 1.4節
Why do we learn STL
 第一版:史前時代 -- 轉木取火
 採用大容量的靜態數組分配。
 限定輸入的數據個數。
 採用動態內存分配。
 第二版:工業時代 -- 組件化大生產
 第三版:唯美主義的傑作
 Demo
Short Talk About STL
 Container( 容器 ) :各種資料結構如 vector 、 li
st 、 deque 、 set 、 map 。
 Algorithm :各種常見的演算法如 sort 、 search
、 copy 、 erase… 等。
 Iterators( 迭代器 ) :扮演容器與演算法之間的
膠著劑,所謂的「泛型指標」。
 Functors( 仿函式 ) :行為類似函式,可做為演
算法的某種策略 (policy) 。
 Adapters( 配接器 ) :一種用來修飾容器或仿函
式或迭代器介面的東西,如 stack 。
 Allocators( 配置器 ) :負責空間配置與管理。
這不是賞心悅目的圖
Container
Three types of containers
Sequence containers
Linear data structures (vectors, linked lists)
First-class container
Associative containers
Non-linear, can find elements quickly
Key/value pairs
First-class container
Container adapters
STL Container Classes
 Sequence containers
 vector
 deque
 list
 Associative containers
 set
 multiset
 map
 multimap
 Container adapters
 stack
 queue
 priority_queue
Sequence Containers
Three sequence containers
vector - based on arrays
deque - based on arrays
list - robust linked list
vector
 vector
<vector>
Data structure with contiguous memory locations
Access elements with []
Use when data must be sorted and easily accessible
 When memory exhausted
Allocates larger, contiguous area of memory
Copies itself there
Deallocates old memory
 Has random access iterators
list
list container
Header <list>
Efficient insertion/deletion anywhere in contain
er
Doubly-linked list (two pointers per node)
Bidirectional iterators
std::list< type > name;
deque
 deque ("deek"): double-ended queue
Header <deque>
Indexed access using []
Efficient insertion/deletion in front and back
Non-contiguous memory: has "smarter" iterators
 Same basic operations as vector
Also has
push_front (insert at front of deque)
pop_front (delete from front)
Associative containers
Associative containers
Direct access to store/retrieve elements
Uses keys (search keys)
4 types: multiset, set, multimap and map
Keys in sorted order
multiset and multimap allow duplicate keys
multimap and map have keys and associated values
multiset and set only have values
Key
Value
multiset
 multiset
Header <set>
Fast storage, retrieval of keys (no values)
Allows duplicates
Bidirectional iterators
 Ordering of elements
Done by comparator function object
Used when creating multiset
For integer multiset
less<int> comparator function object
multiset< int, std::less<int> > myObject;
Elements will be sorted in ascending order
set
 set
Header <set>
Implementation identical to multiset
Unique keys
Duplicates ignored and not inserted
Supports bidirectional iterators (but not random access)
std::set< type, std::less<type> > name;
multimap
 multimap
Header <map>
Fast storage and retrieval of keys and associated values
Has key/value pairs
Duplicate keys allowed (multiple values for a single ke
y)
One-to-many relationship
I.e., one student can take many courses
Insert pair objects (with a key and value)
Bidirectional iterators
map
 map
Header <map>
Like multimap, but only unique key/value pairs
One-to-one mapping (duplicates ignored)
Use [] to access values
Example: for map object m
m[30] = 4000.21;
Sets the value of key 30 to 4000.21
If subscript not in map, creates new key/value pair
 Type declaration
 std::map< int, double, std::less< int > >;
Container Adapters
Container adapters
stack, queue and priority_queue
Not first class containers
Do not support iterators
Do not provide actual data structure
Programmer can select implementation
Member functions push and pop
stack
 stack
Header <stack>
Insertions and deletions at one end
Last-in, first-out (LIFO) data structure
Can use vector, list, or deque (default)
Declarations
stack<type, vector<type> > myStack;
stack<type, list<type> > myOtherStack;
stack<type> anotherStack; // default deque
vector, list
 Implementation of stack (default deque)
 Does not change behavior, just performance (deque and vect
or fastest)
queue
 queue
 Header <queue>
 Insertions at back, deletions at front
 First-in-first-out (FIFO) data structure
 Implemented with list or deque (default)
 std::queue<double> values;
 Functions
 push( element )
 Same as push_back, add to end
 pop( element )
 Implemented with pop_front, remove from front
 empty()
 size()
priority_queue
 priority_queue
Header <queue>
Insertions happen in sorted order, deletions from front
Implemented with vector (default) or deque
Highest priority element always removed first
Heapsort algorithm puts largest elements at front
less<T> default, programmer can specify other comparator
Functions
push(value), pop(value)
top()
 View top element
size()
empty()
Iterator
 Iterators similar to pointers
Point to first element in a container
Iterator operators same for all containers
* dereferences
++ points to next element
begin() returns iterator to first element
end() returns iterator to last element
Use iterators with sequences (ranges)
Containers
Input sequences: istream_iterator
Output sequences: ostream_iterator
Iterator Categories
 Input
Read elements from container, can only move forward
 Output
Write elements to container, only forward
 Forward
Combines input and output, retains position
Multi-pass (can pass through sequence twice)
 Bidirectional
Like forward, but can move backwards as well
 Random access
Like bidirectional, but can also jump to any element
Example
Here
Algorithms
 STL has algorithms used generically across contai
ners
Operate on elements indirectly via iterators
Often operate on sequences of elements
Defined by pairs of iterators
First and last element
Algorithms often return iterators
find()
Returns iterator to element, or end() if not found
Premade algorithms save programmers time and effort
Before STL
Class libraries incompatible among vendors
Algorithms built into container classes
STL separates containers and algorithms
Easier to add new algorithms
More efficient, avoids virtual function calls
<algorithm>
Algorithms
Function Object (Functor)
 Function objects (<functional>)
Contain functions invoked using operator()
STL function objects Type
divides< T > arithmetic
equal_to< T > relational
greater< T > relational
greater_equal< T > relational
less< T > relational
less_equal< T > relational
logical_and< T > logical
logical_not< T > logical
logical_or< T > logical
minus< T > arithmetic
modulus< T > arithmetic
negate< T > arithmetic
not_equal_to< T > relational
plus< T > arithmetic
multiplies< T > arithmetic
class FunctoinObject
{
public:
template<class T>
T operator() (T t) const{…;};
}
Demo
Function Object (Functor)
Using STL I
 class TMyClass;
 typedef list<TMyClass> TMyClassList;
// 用於存放對象的 list 容器
 typedef list<TMyClass*> TMyClassPtrList; // 用於存放
對象指針的 list 容器
Using STL II
class TMyClass {
private:
...
public:
TMyClass(..); // 拷貝構造函數
TMyClass(const TMyClass& obj)
{ *this = obj; } // 賦值操作符
TMyClass& operator=(const TMyClass& obj); ... };
Using STL III
TMyClass object;
TMyClassList myList;
TMyClassList::iterator it;
it = myList.insert(myList.end(), object);
TMyClass *pObject = &(*it);
Using STL IIII
TMyClassList::iterator it;
TMyClass *pObject;
for (it = myList.begin(); it != myList.end(); it ++)
{ pObject = &(*it); // 使用 pObject }
//pObject = *it;
Using STL V
TMyClassList::iterator it;
TMyClass *pObject;
for (it = myList.begin(); it != myList.end(); it ++)
{
pObject = &(*it);
if (pObject 滿足某些刪除的標註 ) then
myList.erase(it);
// 若 list 裡面保存的是指針,那麼增加下面代碼
delete pObject;
}
Demo
Example
測試結果
範例: Deitel C++ How To Program Ch21
Cl Version 14.00.50727.42 全通過
gcc version 3.4.2 (mingw-special) 十二個
bcc32 九個
Reference
C++ How to Program 3rd.
STL 源碼剖析
STL中文站
建議閱讀書籍
基礎
C++ 程式語言經典增訂版
C++ 標準程式庫
進階
Effective STL
泛型程式設計與 STL
STL 源碼剖析
Q & A
END

More Related Content

What's hot

How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)
Sangharsh agarwal
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
Kumar Gaurav
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
Michael Heron
 
Understanding the components of standard template library
Understanding the components of standard template libraryUnderstanding the components of standard template library
Understanding the components of standard template library
Rahul Sharma
 
An Introduction to the C++ Standard Library
An Introduction to the C++ Standard LibraryAn Introduction to the C++ Standard Library
An Introduction to the C++ Standard Library
Joyjit Choudhury
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-library
Hariz Mustafa
 
Lecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handlingLecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handling
Hariz Mustafa
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
Haitham El-Ghareeb
 
Java10 Collections and Information
Java10 Collections and InformationJava10 Collections and Information
Java10 Collections and Information
SoftNutx
 
LectureNotes-03-DSA
LectureNotes-03-DSALectureNotes-03-DSA
LectureNotes-03-DSA
Haitham El-Ghareeb
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
Hoang Nguyen
 
Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)
Peter Antman
 
List
ListList
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
KristinaBorooah
 
Java class 5
Java class 5Java class 5
Java class 5
Edureka!
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
Binoj T E
 
Scala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsScala Collections : Java 8 on Steroids
Scala Collections : Java 8 on Steroids
François Garillot
 
Java Collections
Java  Collections Java  Collections
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
Nguync91368
 
Priority Queue
Priority QueuePriority Queue
Priority Queue
Joyjit Choudhury
 

What's hot (20)

How to choose best containers in STL (C++)
How to choose best containers in STL (C++)How to choose best containers in STL (C++)
How to choose best containers in STL (C++)
 
Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
 
Understanding the components of standard template library
Understanding the components of standard template libraryUnderstanding the components of standard template library
Understanding the components of standard template library
 
An Introduction to the C++ Standard Library
An Introduction to the C++ Standard LibraryAn Introduction to the C++ Standard Library
An Introduction to the C++ Standard Library
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-library
 
Lecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handlingLecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handling
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
Java10 Collections and Information
Java10 Collections and InformationJava10 Collections and Information
Java10 Collections and Information
 
LectureNotes-03-DSA
LectureNotes-03-DSALectureNotes-03-DSA
LectureNotes-03-DSA
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)
 
List
ListList
List
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Java class 5
Java class 5Java class 5
Java class 5
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Scala Collections : Java 8 on Steroids
Scala Collections : Java 8 on SteroidsScala Collections : Java 8 on Steroids
Scala Collections : Java 8 on Steroids
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
1 list datastructures
1 list datastructures1 list datastructures
1 list datastructures
 
Priority Queue
Priority QueuePriority Queue
Priority Queue
 

Similar to C++ STL 概觀

C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
Abhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
GauravPandey43518
 
Object Oriented Programming Using C++: C++ STL Programming.pptx
Object Oriented Programming Using C++: C++ STL Programming.pptxObject Oriented Programming Using C++: C++ STL Programming.pptx
Object Oriented Programming Using C++: C++ STL Programming.pptx
RashidFaridChishti
 
Python for beginners
Python for beginnersPython for beginners
Python for beginners
Ali Huseyn Aliyev
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
BG Java EE Course
 
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docxAssg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
festockton
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
fawzmasood
 
16 Linear data structures
16 Linear data structures16 Linear data structures
16 Linear data structures
maznabili
 
C_STL_2.pptx
C_STL_2.pptxC_STL_2.pptx
C_STL_2.pptx
SOHANKUMARIETStudent
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
soniya555961
 
Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from java
IndicThreads
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
SivaSankar Gorantla
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
FALLEE31188
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
tarun308
 
collections
collectionscollections
collections
javeed_mhd
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
Intro C# Book
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
Ahmet Bulut
 
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template Library
Anirudh Raja
 
Sysprog 9
Sysprog 9Sysprog 9
Sysprog 9
Ahmed Mekkawy
 
Cs341
Cs341Cs341

Similar to C++ STL 概觀 (20)

C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
Object Oriented Programming Using C++: C++ STL Programming.pptx
Object Oriented Programming Using C++: C++ STL Programming.pptxObject Oriented Programming Using C++: C++ STL Programming.pptx
Object Oriented Programming Using C++: C++ STL Programming.pptx
 
Python for beginners
Python for beginnersPython for beginners
Python for beginners
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docxAssg 14 C++ Standard Template Library (STL)(Extra Credit .docx
Assg 14 C++ Standard Template Library (STL)(Extra Credit .docx
 
STL ALGORITHMS
STL ALGORITHMSSTL ALGORITHMS
STL ALGORITHMS
 
16 Linear data structures
16 Linear data structures16 Linear data structures
16 Linear data structures
 
C_STL_2.pptx
C_STL_2.pptxC_STL_2.pptx
C_STL_2.pptx
 
01-intro_stacks.ppt
01-intro_stacks.ppt01-intro_stacks.ppt
01-intro_stacks.ppt
 
Scala collections api expressivity and brevity upgrade from java
Scala collections api  expressivity and brevity upgrade from javaScala collections api  expressivity and brevity upgrade from java
Scala collections api expressivity and brevity upgrade from java
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
C1320prespost
C1320prespostC1320prespost
C1320prespost
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
 
collections
collectionscollections
collections
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
Programming with Python - Week 3
Programming with Python - Week 3Programming with Python - Week 3
Programming with Python - Week 3
 
Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template Library
 
Sysprog 9
Sysprog 9Sysprog 9
Sysprog 9
 
Cs341
Cs341Cs341
Cs341
 

More from PingLun Liao

深入探討 C 語言
深入探討 C 語言深入探討 C 語言
深入探討 C 語言
PingLun Liao
 
Git 程式碼版本控制軟體介紹
Git 程式碼版本控制軟體介紹Git 程式碼版本控制軟體介紹
Git 程式碼版本控制軟體介紹
PingLun Liao
 
給沒有程式設計經驗的人
給沒有程式設計經驗的人給沒有程式設計經驗的人
給沒有程式設計經驗的人
PingLun Liao
 
陣列與指標
陣列與指標陣列與指標
陣列與指標
PingLun Liao
 
Perl For Bioinformatics
Perl For BioinformaticsPerl For Bioinformatics
Perl For Bioinformatics
PingLun Liao
 
C++ Function
C++ FunctionC++ Function
C++ Function
PingLun Liao
 
C 檔案輸入與輸出
C 檔案輸入與輸出C 檔案輸入與輸出
C 檔案輸入與輸出
PingLun Liao
 
Win32 視窗程式設計基礎
Win32 視窗程式設計基礎Win32 視窗程式設計基礎
Win32 視窗程式設計基礎
PingLun Liao
 
Matlab 在機率與統計的應用
Matlab 在機率與統計的應用Matlab 在機率與統計的應用
Matlab 在機率與統計的應用
PingLun Liao
 
Android 2D 遊戲設計基礎
Android 2D 遊戲設計基礎Android 2D 遊戲設計基礎
Android 2D 遊戲設計基礎
PingLun Liao
 
Android 介面設計
Android 介面設計Android 介面設計
Android 介面設計
PingLun Liao
 
Java 視窗程式設計
Java 視窗程式設計Java 視窗程式設計
Java 視窗程式設計
PingLun Liao
 
Java 網路程式
Java 網路程式Java 網路程式
Java 網路程式
PingLun Liao
 
Android introduction
Android introductionAndroid introduction
Android introduction
PingLun Liao
 
RESTful
RESTfulRESTful
RESTful
PingLun Liao
 
Web service
Web serviceWeb service
Web service
PingLun Liao
 
How toprogram
How toprogramHow toprogram
How toprogram
PingLun Liao
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
PingLun Liao
 
Object-Oriented Programming
Object-Oriented ProgrammingObject-Oriented Programming
Object-Oriented Programming
PingLun Liao
 
Object-Based Programming Part II
Object-Based Programming Part IIObject-Based Programming Part II
Object-Based Programming Part II
PingLun Liao
 

More from PingLun Liao (20)

深入探討 C 語言
深入探討 C 語言深入探討 C 語言
深入探討 C 語言
 
Git 程式碼版本控制軟體介紹
Git 程式碼版本控制軟體介紹Git 程式碼版本控制軟體介紹
Git 程式碼版本控制軟體介紹
 
給沒有程式設計經驗的人
給沒有程式設計經驗的人給沒有程式設計經驗的人
給沒有程式設計經驗的人
 
陣列與指標
陣列與指標陣列與指標
陣列與指標
 
Perl For Bioinformatics
Perl For BioinformaticsPerl For Bioinformatics
Perl For Bioinformatics
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
C 檔案輸入與輸出
C 檔案輸入與輸出C 檔案輸入與輸出
C 檔案輸入與輸出
 
Win32 視窗程式設計基礎
Win32 視窗程式設計基礎Win32 視窗程式設計基礎
Win32 視窗程式設計基礎
 
Matlab 在機率與統計的應用
Matlab 在機率與統計的應用Matlab 在機率與統計的應用
Matlab 在機率與統計的應用
 
Android 2D 遊戲設計基礎
Android 2D 遊戲設計基礎Android 2D 遊戲設計基礎
Android 2D 遊戲設計基礎
 
Android 介面設計
Android 介面設計Android 介面設計
Android 介面設計
 
Java 視窗程式設計
Java 視窗程式設計Java 視窗程式設計
Java 視窗程式設計
 
Java 網路程式
Java 網路程式Java 網路程式
Java 網路程式
 
Android introduction
Android introductionAndroid introduction
Android introduction
 
RESTful
RESTfulRESTful
RESTful
 
Web service
Web serviceWeb service
Web service
 
How toprogram
How toprogramHow toprogram
How toprogram
 
Generic Programming
Generic ProgrammingGeneric Programming
Generic Programming
 
Object-Oriented Programming
Object-Oriented ProgrammingObject-Oriented Programming
Object-Oriented Programming
 
Object-Based Programming Part II
Object-Based Programming Part IIObject-Based Programming Part II
Object-Based Programming Part II
 

Recently uploaded

Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
jpupo2018
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
Hiroshi SHIBATA
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
Federico Razzoli
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
Wouter Lemaire
 

Recently uploaded (20)

Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Project Management Semester Long Project - Acuity
Project Management Semester Long Project - AcuityProject Management Semester Long Project - Acuity
Project Management Semester Long Project - Acuity
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Introduction of Cybersecurity with OSS at Code Europe 2024
Introduction of Cybersecurity with OSS  at Code Europe 2024Introduction of Cybersecurity with OSS  at Code Europe 2024
Introduction of Cybersecurity with OSS at Code Europe 2024
 
Webinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data WarehouseWebinar: Designing a schema for a Data Warehouse
Webinar: Designing a schema for a Data Warehouse
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
UI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentationUI5 Controls simplified - UI5con2024 presentation
UI5 Controls simplified - UI5con2024 presentation
 

C++ STL 概觀

  • 3. What is STL Standard Template Library. ANSI/ISO C++ Key Person Alexander A. Stepanov
  • 4. STL 的不同實現版本 HP STL :這是第一個版本 P.J. Plauger STL Rouge Wave STL SGI STL STLport Reference: C++ STL編程輕鬆入門基礎 1.4節
  • 5. Why do we learn STL  第一版:史前時代 -- 轉木取火  採用大容量的靜態數組分配。  限定輸入的數據個數。  採用動態內存分配。  第二版:工業時代 -- 組件化大生產  第三版:唯美主義的傑作  Demo
  • 6. Short Talk About STL  Container( 容器 ) :各種資料結構如 vector 、 li st 、 deque 、 set 、 map 。  Algorithm :各種常見的演算法如 sort 、 search 、 copy 、 erase… 等。  Iterators( 迭代器 ) :扮演容器與演算法之間的 膠著劑,所謂的「泛型指標」。  Functors( 仿函式 ) :行為類似函式,可做為演 算法的某種策略 (policy) 。  Adapters( 配接器 ) :一種用來修飾容器或仿函 式或迭代器介面的東西,如 stack 。  Allocators( 配置器 ) :負責空間配置與管理。
  • 8. Container Three types of containers Sequence containers Linear data structures (vectors, linked lists) First-class container Associative containers Non-linear, can find elements quickly Key/value pairs First-class container Container adapters
  • 9. STL Container Classes  Sequence containers  vector  deque  list  Associative containers  set  multiset  map  multimap  Container adapters  stack  queue  priority_queue
  • 10. Sequence Containers Three sequence containers vector - based on arrays deque - based on arrays list - robust linked list
  • 11. vector  vector <vector> Data structure with contiguous memory locations Access elements with [] Use when data must be sorted and easily accessible  When memory exhausted Allocates larger, contiguous area of memory Copies itself there Deallocates old memory  Has random access iterators
  • 12. list list container Header <list> Efficient insertion/deletion anywhere in contain er Doubly-linked list (two pointers per node) Bidirectional iterators std::list< type > name;
  • 13. deque  deque ("deek"): double-ended queue Header <deque> Indexed access using [] Efficient insertion/deletion in front and back Non-contiguous memory: has "smarter" iterators  Same basic operations as vector Also has push_front (insert at front of deque) pop_front (delete from front)
  • 14. Associative containers Associative containers Direct access to store/retrieve elements Uses keys (search keys) 4 types: multiset, set, multimap and map Keys in sorted order multiset and multimap allow duplicate keys multimap and map have keys and associated values multiset and set only have values Key Value
  • 15. multiset  multiset Header <set> Fast storage, retrieval of keys (no values) Allows duplicates Bidirectional iterators  Ordering of elements Done by comparator function object Used when creating multiset For integer multiset less<int> comparator function object multiset< int, std::less<int> > myObject; Elements will be sorted in ascending order
  • 16. set  set Header <set> Implementation identical to multiset Unique keys Duplicates ignored and not inserted Supports bidirectional iterators (but not random access) std::set< type, std::less<type> > name;
  • 17. multimap  multimap Header <map> Fast storage and retrieval of keys and associated values Has key/value pairs Duplicate keys allowed (multiple values for a single ke y) One-to-many relationship I.e., one student can take many courses Insert pair objects (with a key and value) Bidirectional iterators
  • 18. map  map Header <map> Like multimap, but only unique key/value pairs One-to-one mapping (duplicates ignored) Use [] to access values Example: for map object m m[30] = 4000.21; Sets the value of key 30 to 4000.21 If subscript not in map, creates new key/value pair  Type declaration  std::map< int, double, std::less< int > >;
  • 19. Container Adapters Container adapters stack, queue and priority_queue Not first class containers Do not support iterators Do not provide actual data structure Programmer can select implementation Member functions push and pop
  • 20. stack  stack Header <stack> Insertions and deletions at one end Last-in, first-out (LIFO) data structure Can use vector, list, or deque (default) Declarations stack<type, vector<type> > myStack; stack<type, list<type> > myOtherStack; stack<type> anotherStack; // default deque vector, list  Implementation of stack (default deque)  Does not change behavior, just performance (deque and vect or fastest)
  • 21. queue  queue  Header <queue>  Insertions at back, deletions at front  First-in-first-out (FIFO) data structure  Implemented with list or deque (default)  std::queue<double> values;  Functions  push( element )  Same as push_back, add to end  pop( element )  Implemented with pop_front, remove from front  empty()  size()
  • 22. priority_queue  priority_queue Header <queue> Insertions happen in sorted order, deletions from front Implemented with vector (default) or deque Highest priority element always removed first Heapsort algorithm puts largest elements at front less<T> default, programmer can specify other comparator Functions push(value), pop(value) top()  View top element size() empty()
  • 23. Iterator  Iterators similar to pointers Point to first element in a container Iterator operators same for all containers * dereferences ++ points to next element begin() returns iterator to first element end() returns iterator to last element Use iterators with sequences (ranges) Containers Input sequences: istream_iterator Output sequences: ostream_iterator
  • 24. Iterator Categories  Input Read elements from container, can only move forward  Output Write elements to container, only forward  Forward Combines input and output, retains position Multi-pass (can pass through sequence twice)  Bidirectional Like forward, but can move backwards as well  Random access Like bidirectional, but can also jump to any element
  • 26. Algorithms  STL has algorithms used generically across contai ners Operate on elements indirectly via iterators Often operate on sequences of elements Defined by pairs of iterators First and last element Algorithms often return iterators find() Returns iterator to element, or end() if not found Premade algorithms save programmers time and effort
  • 27. Before STL Class libraries incompatible among vendors Algorithms built into container classes STL separates containers and algorithms Easier to add new algorithms More efficient, avoids virtual function calls <algorithm> Algorithms
  • 28. Function Object (Functor)  Function objects (<functional>) Contain functions invoked using operator() STL function objects Type divides< T > arithmetic equal_to< T > relational greater< T > relational greater_equal< T > relational less< T > relational less_equal< T > relational logical_and< T > logical logical_not< T > logical logical_or< T > logical minus< T > arithmetic modulus< T > arithmetic negate< T > arithmetic not_equal_to< T > relational plus< T > arithmetic multiplies< T > arithmetic
  • 29. class FunctoinObject { public: template<class T> T operator() (T t) const{…;}; } Demo Function Object (Functor)
  • 30. Using STL I  class TMyClass;  typedef list<TMyClass> TMyClassList; // 用於存放對象的 list 容器  typedef list<TMyClass*> TMyClassPtrList; // 用於存放 對象指針的 list 容器
  • 31. Using STL II class TMyClass { private: ... public: TMyClass(..); // 拷貝構造函數 TMyClass(const TMyClass& obj) { *this = obj; } // 賦值操作符 TMyClass& operator=(const TMyClass& obj); ... };
  • 32. Using STL III TMyClass object; TMyClassList myList; TMyClassList::iterator it; it = myList.insert(myList.end(), object); TMyClass *pObject = &(*it);
  • 33. Using STL IIII TMyClassList::iterator it; TMyClass *pObject; for (it = myList.begin(); it != myList.end(); it ++) { pObject = &(*it); // 使用 pObject } //pObject = *it;
  • 34. Using STL V TMyClassList::iterator it; TMyClass *pObject; for (it = myList.begin(); it != myList.end(); it ++) { pObject = &(*it); if (pObject 滿足某些刪除的標註 ) then myList.erase(it); // 若 list 裡面保存的是指針,那麼增加下面代碼 delete pObject; }
  • 36. 測試結果 範例: Deitel C++ How To Program Ch21 Cl Version 14.00.50727.42 全通過 gcc version 3.4.2 (mingw-special) 十二個 bcc32 九個
  • 37. Reference C++ How to Program 3rd. STL 源碼剖析 STL中文站
  • 39. Q & A
  • 40. END