Introduction to Standard Template Library (STL)

23
1 Introduction to Standard Template Library (STL) Wenguang Wang and Yanping Zhao March 20, 2000

description

Introduction to Standard Template Library (STL). Wenguang Wang and Yanping Zhao March 20, 2000. What Is STL?. A new C++ Library, ANSI standard Support complex data structures Vector (array), list, string, hash, set, map, queue, stack, priority queue... Support various algorithms - PowerPoint PPT Presentation

Transcript of Introduction to Standard Template Library (STL)

Page 1: Introduction to Standard Template Library (STL)

1

Introduction toStandard Template Library (STL)

Wenguang Wang and Yanping Zhao

March 20, 2000

Page 2: Introduction to Standard Template Library (STL)

2

What Is STL?

• A new C++ Library, ANSI standard• Support complex data structures

– Vector (array), list, string, hash, set, map, queue, stack, priority queue...

• Support various algorithms– Searching, sorting, heap, merging,

copying, transforming…

Page 3: Introduction to Standard Template Library (STL)

3

Why STL was developed?

• Some basic data structures are used everyday in your programs

• Programming them from the scratch is time consuming and error-prone

• Most of operations on these structures can be standardized

Page 4: Introduction to Standard Template Library (STL)

4

Why we use STL?

• More reliable and efficient implementations

• Automatic memory management• Constructing complex data

structures easily• Saving programming time and

efforts

Page 5: Introduction to Standard Template Library (STL)

5

Where are STL and resources?• GNU g++ on skorpio, ultra*…• Visual C++• Wenguang Wang’s home page

– http://www.cs.usask.ca/grads/wew036/stl

• Standard Template Library Programmer’s Guide– http://www.sgi.com/Technology/STL/

Page 6: Introduction to Standard Template Library (STL)

6

Fundamental Elements in STL?• Containers (data structures)

– vector dynamic size– list doubly linked list– string, hash

• Algorithms – sorting, heap manipulation

• Iterators (pointers)– A bridge to connect algorithms and

containers– Example

Page 7: Introduction to Standard Template Library (STL)

7

How to Use STL? (example)• On Unix

– GNU g++ 2.7.0 and later version

• On Windows– Visual C++ 5.0 and later version– not fully supported even in Visual C++

6.0• no hash table• no rope• more?

• Example

Page 8: Introduction to Standard Template Library (STL)

8

Why I am here?• I learned STL two weeks ago• I never heard of STL before• After a five minutes STL tour, I can

use it !• I found that STL helped me a lot in

programming• I want to share it with you

Page 9: Introduction to Standard Template Library (STL)

9

Vector -- Advantages

• Data type– basic type: integer, double, pointer, …– user-defined structures and classes

• Dynamic size

Page 10: Introduction to Standard Template Library (STL)

10

int *array = malloc(size*sizeof(int));//allocate

int *temp = realloc(array, new_size*sizeof(int));if (temp) array = temp; //reallocate

free(array); // deallocate

Estimate

#define SIZE 2000

Count

Count = 0;while(not end) count++;

How to get the size of the array?

vector<int> vec;vec.push_back(i);

Page 11: Introduction to Standard Template Library (STL)

11

Vector -- Operations

• Property operations– [ ] , size, empty

• Iterator operations– begin, end

• Manipulation operations– push_back, pop_back, insert, erase

00 1 2 3 4 5 6 7

size=8

begin endv[3]

Page 12: Introduction to Standard Template Library (STL)

12

#include <vector>#include <iostream>

typedef vector<int> VecInt;void main(){ VecInt vInt; VecInt::iterator it; vInt.push_back(0); vInt.push_back(1); for (it=vInt.begin(); it!=vInt.end(); it++) cout << *it << endl; for (int i=0; i<vInt.size(); i++) cout << vInt[i] << endl; it = vInt.insert(vInt.begin(), 2); vInt.erase(it);}

Page 13: Introduction to Standard Template Library (STL)

13

List -- Operations• Property operations

– size, empty– back, front

• Iterator operations– begin, end

• Manipulation operations– push_back, pop_back, push_front,

pop_front– insert, erase

Page 14: Introduction to Standard Template Library (STL)

14

#include <list>#include <iostream>typedef list<int> ListInt;void main(){ ListInt lInt; ListInt::iterator it; // stack lInt.push_front(0); lInt.push_front(1); cout << lInt.front() << endl; lInt.pop_front(); lInt.pop_front(); // queue lInt.push_back(2); lInt.push_back(3); cout << lInt.front() << endl; lInt.pop_front(); lInt.pop_front(); // iterator for (it=lInt.begin(); it!=lInt.end(); it++) cout << *it << endl;}

Page 15: Introduction to Standard Template Library (STL)

15

Algorithm -- Heap

• Data structure: vector or C++ array

• make_heap

• pop_heap

• push_heap

4 1 7 5 3 6 8 9

9 5 8 4 3 6 7 1

8 5 7 4 3 6 1 9

9 8 7 5 3 6 1 4

Page 16: Introduction to Standard Template Library (STL)

16

#include <vector>#include <algorithm>#include <iostream>typedef vector<int> VecInt;void main(){ VecInt vInt(4); int i; vInt[0]=7; vInt[1]=4; vInt[2]=9; vInt[3]=1; make_heap(vInt.begin(), vInt.end()); vInt.push_back(3); push_heap(vInt.begin(), vInt.end()); pop_heap(vInt.begin(), vInt.end()); cout << vInt.back() << endl; vInt.pop_back();}

Page 17: Introduction to Standard Template Library (STL)

17

User-defined Data Type

struct Event {

long timestamp;

int type;

};

typedef Event* PEvent;

typedef vector<Event> VecEvent;

typedef list<PEvent> ListPEvent;

Page 18: Introduction to Standard Template Library (STL)

18

User-defined Data Type (cont.)class Page {

int page_num;

int modify_flag;

int reference_cnt;

};

typedef list<Page> ListPage;

typedef ListPage* PListPage;

typedef vector<PListPage> VecPListPage;

What is the structure of VecPListPage?

page

page

page

page

page

page

Page 19: Introduction to Standard Template Library (STL)

19

Iterator

• Similar to pointer– *it

• Iterator for list (bidirectional iterator)– ++it, it++, --it, it--

• Iterator for vector (random access iterator)– ++it, it++, --it, it--– it+n, itBegin-itEnd

Page 20: Introduction to Standard Template Library (STL)

20

More details in:http://www.cs.usask.ca/grads/wew036/stl

Page 21: Introduction to Standard Template Library (STL)

21

Page 22: Introduction to Standard Template Library (STL)

22

#include <vector>#include <algorithm>void main(void){ vector<int> vInt; vector<int>::iterator begin, end; begin = vInt.begin(); end = vInt.end(); sort(begin, end);}

Page 23: Introduction to Standard Template Library (STL)

23

#include <iostream> //not iostream.h!#include <cstdio>#include <cstdlib>#include <vector>#include <list>#include <set>#include <map>#include <string>#include <algorithm>#ifdef _MSC_VER // for Visual C++

using namespace std;#endif