SSE2034: System Software Experiment 3

Similar documents
Template based set of collection classes STL collection types (container types)

Standard Template Library

STL: C++ Standard Library

Associative Containers and Iterators. Ali Malik

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

CS183 Software Design. Textbooks. Grading Criteria. CS183-Su02-Lecture 1 20 May by Eric A. Durant, Ph.D. 1

Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

STL Standard Template Library

Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

CS 103 Unit 12 Slides

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

Lectures 11,12. Online documentation & links

Templates and Vectors

2014 Spring CS140 Final Exam - James Plank

Chapter 17: Linked Lists

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

1. The term STL stands for?

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

CS197c: Programming in C++

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

Chapter 17: Linked Lists

CPSC 427a: Object-Oriented Programming

COEN244: Class & function templates

The Standard Template Library Classes

Chapter 5. The Standard Template Library.

Programming in C++ using STL. Rex Jaeschke

Computer Programming

Outline. Variables Automatic type inference. Generic programming. Generic programming. Templates Template compilation

vector<int> second (4,100); // four ints with value 100 vector<int> third (second.begin(),second.end()); // iterating through second

Introduction to C++ SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

Queue Implementations

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

Sequential Containers. Ali Malik

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

Standard Template Library STL

To know the relationships among containers, iterators, and algorithms ( 22.2).

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

CS 103 Unit 11. Linked Lists. Mark Redekopp

Abstract Data Types 1

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Concurrent Data Structures in C++ CSInParallel Project

STL. Prof Tejada Week 14

Standard Template Library

Computer Science II Lecture 2 Strings, Vectors and Recursion

Abstract Data Types 1

PIC 10A. Lecture 23: Intro to STL containers

Programming with Haiku

by Pearson Education, Inc. All Rights Reserved. 2

Sequential Containers. Ali Malik

CSCI-1200 Data Structures Fall 2017 Lecture 9 Iterators & STL Lists

CS11 Advanced C++ Spring 2018 Lecture 2

Unit 4 Basic Collections

auto map<deque<string>, vector<string>> mymap; for(auto iter = mymap.begin(); iter!= mymap.end(); ++iter) {

Computational Physics

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

Exceptions, Templates, and the STL

Intermediate Programming, Spring 2017*

Git. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

19.1 The Standard Template Library

CSCI-1200 Data Structures Spring 2018 Lecture 10 Vector Iterators & Linked Lists

CSI33 Data Structures

Lecture on pointers, references, and arrays and vectors

Dynamic Data Structures

CS 103 Unit 11. Linked Lists. Mark Redekopp

Lecture-5. STL Containers & Iterators

GridKa School 2013: Effective Analysis C++ Standard Template Library

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

Vectors. CIS 15 : Spring 2007

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

Standard Template Library. Containers, Iterators, Algorithms. Sequence Containers. Containers

Standard Template Library. Outline

Template. SSE2034: System Software Experiment 3, Fall 2018, Jinkyu Jeong

This is a CLOSED-BOOK-CLOSED-NOTES exam consisting of five (5) questions. Write your answer in the answer booklet provided. 1. OO concepts (5 points)

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

MODULE 33 --THE STL-- ALGORITHM PART I

Arrays - Vectors. Arrays: ordered sequence of values of the same type. Structures: named components of various types

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Standard Template Library

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. C++ Kitchen Sink. Lecture 14.

Function Templates. Consider the following function:

The Standard Template Library. An introduction

Due Date: See Blackboard

G Programming Languages Spring 2010 Lecture 11. Robert Soulé, New York University

CSCI-1200 Data Structures Fall 2013 Lecture 9 Iterators & Lists

Teaching with the STL

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Simulations. buffer of fixed capacity using the STL deque. modeling arrival times generating and processing jobs

Sequence Containers. Cristian Cibils

Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

CSCI-1200 Data Structures Fall 2017 Lecture 10 Vector Iterators & Linked Lists

General Advise: Don t reinvent the wheel

G52CPP C++ Programming Lecture 18

Lesson 13 - Vectors Dynamic Data Storage

Transcription:

SSE2034: System Software Experiment 3 Spring 2016 Jinkyu Jeong (jinkyu@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

STL Collection Types Template based set of collection classes STL collection types (container types) Sequences vector - collection of elements of type T list - doubly linked list, only sequential access deque - fast insert at either end Associative containers map set

Template Skeleton class for multiple types template < class T > class Value { T _value; public: Value ( T value ) { _value = value; } T getvalue (); }; void setvalue ( T value ); template < class T > T Value<T>::getValue () { return _value; } template < class T > void Value<T>::setValue ( T value ) { _value = value; } Value<float> values[10]; // array of values of type float

Iterators Allows access of elements in a collection forward iterator bi-directional iterator random access iterator bi-directional + constant time access

STL Collections Methods General class methods empty True if collection is empty size number of elements in the collection begin start of collection in forward iterator end one past the last in forward iterator rbegin last of collection in backward iterator rend one before the start in backward iterator clear erases all elements erase erase an element or range of collection Insert insert an element

Add and Remove vector, deque, list front - get a reference to first element back - get a reference to last element push_front - add to the start (NOT for vector) push_back - add to the end pop_front - remove from the start (NOT for vector) pop_back - remove from the end

Iterators vector<int> v; // input from console while (cin >> input) v.push_back(input); // sort elements in vector sort(v.begin(), v.end()); // print out - version 1 for (int i=0; i<v.size(), i++) cout << v[i] << \n ; size() v 10 20 30 begin() end() // print out - version 2 vector<int>::iterator ii; for (ii=v.begin(); ii!=v.end(); ii++) cout << *ii << \n ;

Operator [ ] Vector, deque, map can use [ ] to access element Similar semantics in C array element access Not available for list at - similar to operator [ ] v.at(index) vs. v[index] Available for vector, deque Perform bound checking unlike operator [ ]

Vector Automatically take care of resizing Constructor vector<t> vec; // empty vector vector<t> vec(10); // capacity of 10 elements vector<t> vec(10, 1); // and initialized with 1 Add / remove elements push_back(e), pop_back() insert(pos, e), erase(pos)

Vector capacity - allocate space of vector #include <iostream> #include <vector> using namespace std; main() { vector<int> iv1; vector<int> iv2(10); cout << iv1: capacity = << iv1.capacity() << \n ; cout << iv2: capacity = << iv2.capacity() << \n ; } iv1.push_back(10); iv2[0] = 10; iv2.push_back(20); iv1[1] = 20; // can increase size and capacity // iv2[1] or iv2[10] // segmentation fault

List Doubly linked list Constructor list<t> li; // empty list (size = 0) list<t> li(10); // list with size = 10 list<t> li(10, 1); // and initialized with 1 Add / remove elements push_front(e), push_back(e), insert(pos, e) pop_front(), pop_back(), erase(pos)

List Test if list is empty // std::list<int> il(100); il.size() takes O(n) il.empty() takes O(1) // why? Useful algorithms for list sort() on list - uses O(nlogn) algorithm reverse() on list - reverse the order unique() on list - remove repeating elements

List insert (pos, e) : insert e before position pos #include <iostream> #include <list> using namespace std; main() { list<int> L; L.push_back(0); L.push_back(0); L.insert(++L.begin(), 2); L.push_back(5); L.push_back(6); } list<int>::iterator i; for (i=l.begin(); i!=l.end(); i++) cout << *i << ; cout << endl;

Set Sorted Associative Container Sorted at insert by using default compare-operator Unique No two elements are same Fast add & remove Add/remove a sorted range in linear time

Set Sorted output with iterator #include <iostream> #include <set> using namespace std; int main(){ set<int> myset; myset.insert(7); myset.insert(2); myset.insert(-6); } set<int>::const_iterator it; it = myset.begin(); while (it!= myset.end()) { cout << *it << " "; ++it; } cout << endl;

Map Associative container <key, value> pair Unique element (key) Sorted on key value

Map Associative key can be any type #include <iostream> #include <map> #include <string> using namespace std; int main(){ map<string, double> market; market["apple"]= 2.30; market["orange"] = 1.20; market["melon"] = 3.30; } map<string, double>::const_iterator it; it = market.begin(); while (it!= market.end()) { cout << it->first << ": " << it->second << endl; ++it; } cout << endl;

[Lab Practice #1] Schedule earlier and shorter job first (use vector) A job has to be executed as much as period. The job which starts early has the highest priority If two jobs start time are same, the job which has less period has higher priority Input: two integers (start, period) & string of name of jobs until -1 Output: Execution order of jobs If there is no job to handle in specific time unit, print *

[Lab Practice #1] $./sort_job input: 4 2 A input: 2 1 B input: 4 1 C input: -1 B * C A A $./sort_job input: 1 2 D input: 5 1 E input: 4 3 F Input: 4 2 G input: -1 D D * F F F G G E

[Lab Practice #2] From left to right of given circular list, list people s ID in stride Use list to manage people s id Input: stride number and people s id Output : the order of people s id in defined stride, from left to right $./stride stride: 3 id : 1 5 9 8 4 3 2 10 7 6-1 9 3 7 5 2 1 10 4 6 8 $./stride stride: 2 id : 1 2 3 4 5 6-1 2 4 6 3 1 5