chapter 14 introduction to the standard template library
Download
Skip this Video
Download Presentation
Chapter 14 Introduction to the Standard Template Library

Loading in 2 Seconds...

play fullscreen
1 / 31

Chapter 14 Introduction to the Standard Template Library - PowerPoint PPT Presentation


  • 169 Views
  • Uploaded on

Chapter 14 Introduction to the Standard Template Library. §14.1 Introduction §14.2 Containers §14.3 Iterators §14.4 Algorithms §14.5 Function Objects. §14.1 Introduction. Standard Template Library ( STL ) 标准模板库 A software library included in the C++ Standard Library Purpose

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 14 Introduction to the Standard Template Library' - olympia-clarke


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 14 introduction to the standard template library

Chapter 14 Introduction to the Standard Template Library

§14.1 Introduction

§14.2 Containers

§14.3 Iterators

§14.4 Algorithms

§14.5 Function Objects

14 1 introduction
§14.1 Introduction
  • Standard Template Library (STL) 标准模板库
    • A software library included in the C++ Standard Library
  • Purpose
    • To standardize commonly used components
  • History
    • Originally developed by Alexander Stepanov in 1980s
    • Accepted as part of C++ standard in Feb. 1994
components of stl
Components of STL
  • Containers(容器)
    • A container object, such as vector, used to store a collection of data, often referred to as elements
  • Algorithms(算法)
    • Functions to manipulate data such as sorting, searching, and comparing elements.
    • Most of them use iterators to access the elements in the container.
  • Iterators(迭代器)
    • Objects that facilitate traversing through the elements in a container
    • Like built-in pointers that provide a convenient way to access and manipulate the elements in a container
components of stl1
Components of STL

Algorithm 1

Algorithm 2

Object 1

Object 2

Iterator 2

Iterator 1

Container

Object 3

Iterator 3

Algorithm 3

14 2 containers
§14.2 Containers

Containers

  • vector
  • deque
  • list

Sequence containers

  • set
  • multiset
  • map
  • multimap

Associative containers

  • stack
  • queue
  • priority_queue

Derived containers

linear data structures

Non-linear (tree) containers

Suitable for searching

  • Constrained versions of sequence containers
sequence containers
Sequence Containers
  • Represent linear data structures
  • Three sequence containers
    • vector 向量, list 表, and deque 双端队列
  • Differ only in performance

Element 0

Last Element

Element 2

Element 1

iterator

Begin()

end()

int values[] = {1, 2, 3, 4 };

list intList(values, values + 4);

associative containers
Associative Containers
  • Represent non-linear data structures (tree)
  • Elements are sorted by keys
    • E.g. the name of student objects
  • Suitable for searching via keys
  • Set/Multiset: set of items
  • Map/Multimap: set of item pairs

int values[] = {3, 5, 1, 7, 2, 2};

set set1(values, values + 6);

map map1;

map1.insert(map::value_type(100, "John Smith"));

derived containers
Derived Containers
  • Containers adapted from the sequence containers
    • For handling special cases
  • Programmer can choose an appropriate sequence container for a derived containers

stack > stack2;

*: default one

14 3 iterators
§14.3 Iterators
  • Iterators are objects to facilitate traversing through the elements in a container
  • A iterator is like a built-inpointer that can manipulate the elements in a container

random access

bidirectional

forward

input

output

iterator demo
Iterator Demo

vector intVector;

intVector.push_back(10);

intVector.push_back(20);

intVector.push_back(30);

intVector.push_back(40);

intVector.push_back(50);

intVector.push_back(60);

vector::iterator p1 = intVector.begin();

for (; p1 != intVector.end(); p1++){

cout << *p1 << " ";

}

cout << endl << *(--p1) << endl;

cout << *(p1 - 3) << endl;

cout << p1[-3] << endl;

*p1 = 1234;

cout << *p1 << endl;

IteratorDemo

14 4 algorithms
§14.4 Algorithms
  • More than sixty algorithms in STL
  • Five groups
    • Retrieve or non-mutating Algorithms
    • Mutating Algorithms
    • Sorting Algorithms
    • Set Algorithms
    • Relational Algorithms
the copy algorithm
The copy Algorithm

template

copy(InputIterator beg, InputIterator end, OutputIterator targetPostition)

int values[] = {1,2,3,4,5,6};

vector intVector(5);

list intList(5);

copy(values+2, values+4, intVector.begin());

copy(values, values+5, intList.begin());

#include

#include

#include

#include

copy to output stream
Copy to Output Stream

intVector.insert(intVector.begin(), 747);

ostream_iterator output(cout, " ");

cout<<"\nAfter the insertion funciton, intVector: ";

copy(intVector.begin(), intVector.end(), output);

the copy demo
The copy Demo

int values[] = {1,2,3,4,5,6};

vector intVector(5);

list intList(5);

copy(values+2, values+4, intVector.begin());

copy(values, values+5, intList.begin());

intVector.insert(intVector.begin(), 747);

copy(intVector.begin(), intVector.begin()+4, intList.begin());

After initial copy intVector: 3 4 0 0 0

After initial copy intList: 1 2 3 4 5

After the insertion function, intVector: 747 3 4 0 0 0

After the copy function, intList: 747 3 4 0 5

caution
Caution
  • The target container in copying must contains enough elements to store the values from the source container

int values[] = {1,2,3,4,5}

vector intVector;

copy(values+2, values+4, intVector.begin());

sort and binary search
“sort” and “binary_search”

template

void sort(RandomAccessIterator beg, RandomAccessIterator end)

template

void sort(RandomAccessIterator beg, RandomAccessIterator end, relationalOperator op)

template

bool binary_search(ForwardIterator beg, ForwardIterator end, const T &value)

template

bool binary_search(ForwardIterator beg, ForwardIterator end, const T &value, strickWeakOrdering op)

SortDemo

demo of sort binary search
Demo of sort, binary_search

int array1[] = {1, 7, 3, 4, 3, 3, 1, 2};

sort(array1, array1 + 8);

binary_search(array1, array1 + 8, 4)

sort(array1, array1 + 8, greater_equal());

binary_search(array1, array1 + 8, 4, greater_equal())

A function operator!

14 5 function objects
§14.5 Function Objects
  • Object can be used as a function
    • “function”  “object”
  • Passing a function as a parameter
  • In header

int main(){

int x[] = {10,50,30,40,20};

int y[] = {70,90,60,80};

sort(x,x+5,greater());

sort(y,y+4);

for (inti=0;i<5;i++) cout<

for (int j=0;j<4;j++) cout<

int z[9];

merge(x,x+5,y,y+4,z);

for (inti=0;i<9;i++) cout<

}

customized function objects
Customized Function Objects
  • By defining member function operator() in a class

class My {

public:

intoperator()(int a) {return a;}

} ;

My mo;

int x = mo(0);

class BiggerThan{

public:

const int testValue;

BiggerThan(int x) : testValue(x) { }

bool operator()(int val) const {

return val > testValue; }

};

template

class greater : binary_function {

public:

bool operator() (const T& x, const T& y) const{return x>y;}

};

int my[] = {1, 7, 3, 4, 3, 3, 1, 2};

sort(my, my + 8, Mygreater());

BiggerThan bt(10);

cout<

My m;

cout<

1 0

4

a summary
A Summary
  • Concepts
    • STL, Container, Iterator, Algorithm
    • Three types of containers
    • Function, Operation, Algorithm
    • Functional object
  • Features of different containers
  • Main functions of containers
  • Features of STL algorithm
  • Use of functional object
ad