Bevezetés a C++ Standard Template Library-be



Similar documents
Cours de C++ Utilisations des conteneurs

CSI33 Data Structures

Comp151. Definitions & Declarations

Answers to Review Questions Chapter 7

! " # $ %& %' ( ) ) *%%+, -..*/ *%%+ - 0 ) 1 2 1

Ch 7-1. Object-Oriented Programming and Classes

Functional Programming in C++11

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Why you shouldn't use set (and what you should use instead) Matt Austern

sqlpp11 - An SQL Library Worthy of Modern C++

Reading and Writing PCD Files The PCD File Format The Grabber Interface Writing a Custom Grabber PCL :: I/O. Suat Gedikli, Nico Blodow

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Note: Syntactically, a ; is needed at the end of a struct definition.

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Passing 1D arrays to functions.

7.7 Case Study: Calculating Depreciation

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

2009 Tutorial (DB4O and Visual Studio 2008 Express)

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

Object Oriented Software Design II

CSCI-B 565 DATA MINING Project Report for K-means Clustering algorithm Computer Science Core Fall 2012 Indiana University

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Queue Implementations

An Incomplete C++ Primer. University of Wyoming MA 5310

Help on the Embedded Software Block

The if Statement and Practice Problems

Answers to Mini-Quizzes and Labs

CORBA Programming with TAOX11. The C++11 CORBA Implementation

While Loop. 6. Iteration

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Chapter 5 Functions. Introducing Functions

EP241 Computer Programming

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

C++ INTERVIEW QUESTIONS

Moving from C++ to VBA

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Fondamenti di C++ - Cay Horstmann 1

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Appendix M: Introduction to Microsoft Visual C Express Edition

Member Functions of the istream Class

N3458: Simple Database Integration in C++11

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

C++FA 5.1 PRACTICE MID-TERM EXAM

COSC 181 Foundations of Computer Programming. Class 6

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

Cpt S 223. School of EECS, WSU

C++ Crash Kurs. C++ Object-Oriented Programming

Java technológiák 7. előadás A Struts keretrendszer

CISC 181 Project 3 Designing Classes for Bank Accounts

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

The separation principle : a principle for programming language design

Sample Questions Csci 1112 A. Bellaachia

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Schedule. Structures and Classes in C++ Outline. Goals for This Topic. Another Example of a Structure. What is a Structure? Classes May 12-17, 2005

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

COMPUTER SCIENCE 1999 (Delhi Board)

Basics of I/O Streams and File I/O

Binary storage of graphs and related data

CS 241 Data Organization Coding Standards

Basics of C++ and object orientation in OpenFOAM

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Sequences in the C++ STL

Linked Lists: Implementation Sequences in the C++ STL


Chapter 5. Selection 5-1

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

C++ Programming Language

recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1

Data Structures using OOP C++ Lecture 1

Common Data Structures

Chapter 8 Selection 8-1

Conditions & Boolean Expressions

Channel Access Client Programming. Andrew Johnson Computer Scientist, AES-SSG

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

Jzyk C++ cz 7. Jarosław Gramacki Instytut Informatyki i Elektroniki

Introduction to Programming (in C++) Multi-dimensional vectors. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Konzepte objektorientierter Programmierung

Compiler Construction

F ahrenheit = 9 Celsius + 32

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

How to: Use Basic C++, Syntax and Operators

Example of a Java program

Object-Oriented Programming

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Transcription:

C++, 1/ 33 Bevezetés a C++ Standard Template Library-be Pataki Norbert 2013. május 17.

C++, 2/ 33 STL STL vs. STL implementáció Konténerek Funktorok Algoritmusok Iterátorok

STL Konténerek C++, 3/ 33 Szekvenciális: vector, list, deque, basic_string Asszociatív: set, multiset, map, multimap Adapter: stack, queue, priority_queue C++11: unordered_set, unordered_multiset, unordered_map, unordered_multimap forward_list array

C++, 4/ 33 Vector Egybefüggő tárterületen azonos típusú elemek sorozata Elemek indexelve (0-tól kezdve) Elemek elérése gyors Elem beszúrása vector végére gyors Elem beszúrása máshova lassabban

Vector C++, 5/ 33 #include <vector> using namespace std; vector<int> v; // üres vector létrehozása v.push_back( 4 ); // elem beszúrása vector végére v.pop_back(); // utolsó elem törlése // Ötelem"u vector létrehozása, minden eleme 3.2 vector<double> vd( 5, 3.2 ); int s = vd.size(); vd[1] = 4.76; // Elemek elérése vd.back() = 4.17; // Utolsó elem elérése vd.front() = 1.2; // Els"o elem elérése

C++, 6/ 33 list Elemek szétszórtan helyezkednek el Elemek nincsen indexelve i-edik elérése lassú Elem beszúrása bárhova gyors Következő / előző elem érhető el gyorsan

list #include <list> using namespace std; list<double> d; // üres list létrehozása d.push_front( 4.5 ); // elem beszúrása lista elejére d.pop_front(); // utolsó elem törlése d.push_back( 1.1 ); // elem beszúrása lista végére list<double> vl( 3, 6.32 ); int s = vl.size(); d.back() = 4.17; d.front() = 1.2; d.sort(); d.remove( 1.2 ); d.reverse(); // Utolsó elem elérése // Els"o elem elérése // Lista rendezése // Adott értékek törlése // Lista megfordítása C++, 7/ 33

C++, 8/ 33 deque Kettős végű sor Egybefüggő tárterületek szétszórtan helyezkednek el Elemek indexelve vannak Elemek elérése gyors Elem beszúrása tároló elejére / végére gyors Elem beszúrása közepére lassabban

deque C++, 9/ 33 #include <deque> #include <string> using namespace std; deque<string> d; d.push_front( "Hello" ); d.push_back( "World" ); d[0] = "Goodbye"; d.back() = "Cruel World"; d.pop_back(); d.pop_front();

set C++, 10/ 33 Elemek sorrendje: rendezettség Minden elem legfeljebb egyszer szerepelhet Műveletek kihasználják a rendezettséget: gyors keresés, gyors beszúrás, stb.

set C++, 11/ 33 #include <set> #include <cstdlib> using namespace std; srand( time( 0 ) ); set<int> nums; while( nums.size() < 5 ) nums.insert( (rand() % 90 ) + 1 ); }

C++, 12/ 33 multiset Elemek sorrendje: rendezettség Azonos elemek többször is szerepelhetnek Műveletek kihasználják a rendezettséget: gyors keresés, gyors beszúrás, stb.

multiset C++, 13/ 33 #include <set> using namespace std; multiset<int> m; m.insert( 3 ); int s = m.size(); int i = m.count( 3 );

map C++, 14/ 33 Asszociatív tömb: elemek indexelve Nem feltétlenül 0-tól kezdve Nem feltétlenül egymás utáni indexek Nem feltétlenül egészek Kulcs alapján rendezett tárolás

map C++, 15/ 33 #include <map> #include <string> #include <iostream> using namespace std; map<string, string> phones; phones["bela"] = "36(20)555-1234"; phones["lajos"] = "36(30)555-5555; //... cout << phones["bela"];

Rendezés megadása class Student std::string name; int height; double avg; public: std::string get_name() const return name; } int get_height() const return height; } double get_average() const return avg; } }; C++, 16/ 33

Rendezés megadása C++, 17/ 33 struct NameComparator bool operator()( const Student& a, const Student& b ) const return a.get_name() < b.get_name(); } }; std::set<student, NameComparator> a;

Rendezés megadása C++, 18/ 33 struct HeightComparator bool operator()( const Student& a, const Student& b ) const return a.get_height() < b.get_height(); } }; std::set<student, HeightComparator> b;

Rendezés megadása C++, 19/ 33 struct AverageComparator bool operator()( const Student& a, const Student& b ) const return a.get_average() < b.get_average(); } }; std::set<student, AverageComparator> c;

C++, 20/ 33 STL Algoritmusok Globális műveletek Konténer-függetlenek Nem biztos, hogy egy algoritmus az összes konténerrel működik

Algoritmusok C++, 21/ 33 #include <algorithm> using namespace std; //... vector<int> v; list<double> s; multiset<char> m; int i = count( v.begin(), v.end(), 2 ); int j = count( s.begin(), s.end(), 6.5 ); int k = count( m.begin(), m.end(), T );

Algoritmusok folyt. köv. C++, 22/ 33 #include <algorithm> using namespace std; bool even( int x ) return 0 == x % 2; } bool upper( char c ) return c>= A && c<= Z ; } int a = count_if( v.begin(), v.end(), even ); int b = count_if( m.begin(), m.end(), upper );

Algoritmusok C++, 23/ 33 void print( int i ) cout << i << ; } for_each( v.begin(), v.end(), print );

Algoritmusok C++, 24/ 33 #include <numeric> #include <algorithm> bool comp( int i, int j ) return i > j; } vector<int> v; //... int sum = accumulate( v.begin(), v.end(), 0 ); sort( v.begin(), v.end() ); sort( v.begin(), v.end(), comp );

C++, 25/ 33 Iterátorok Elemek elérése a konténerekben Összekapcsolja a konténereket és az algoritmusokat

Iterátorok C++, 26/ 33 vector<int> v; list<double> l; set<int> s; //... vector<int>::iterator vi = v.begin(); list<double>::iterator li = l.begin(); set<int>::iterator i = s.begin(); cout << *vi << << *li << << *si << endl;

Iterátorok C++, 27/ 33 ++vi; // következ"o elemre lép vi++; // következ"o elemre lép --vi; // el"oz"o elemre lép vi--; // el"oz"o elemre lép ++li; // következ"o elemre lép si++; // el"oz"o elemre lép

Konténerek bejárása list<int> l; set<double> s; //... for( list<int>::iterator i = l.begin(); i!= l.end(); ++i ) cout << *i; } for( set<double>::iterator i = s.begin(); i!= s.end(); ++i ) cout << *i; } C++, 28/ 33

Konténerek bejárása C++, 29/ 33 map<int, string> m; //... for( map<int, string>::iterator i = m.begin(); i!= m.end(); ++i) cout << i->first << << i->second; }

Algoritmusok C++, 30/ 33 list<int> l; int x, y; //... // y els"o el"ofordulásának megkeresése list<int>::iterator i = find( l.begin(), l.end(), y ); l.erase( i ); i = find( l.begin(), l.end(), x ); if ( i!= l.end() ) *i = y;

Asszociatív konténerek C++, 31/ 33 set<int> s; int i; //... set<int>::iterator it = s.find( i ); if ( it!= s.end() ) s.erase( it, s.end() ); }

Asszociatív konténerek C++, 32/ 33 map<string, int> m; string str; //... map<string, int>::iterator it = m.find( str ); if ( it!= l.end() ) m.erase( m.begin(), it ); } m.erase( "Hello" );

Iterátorok C++, 33/ 33 const iteratorok nem módosíthatják a konténereket: vector<int> v;... vector<int>::const_iterator i = v.begin(); *i = 10; // ford. hiba.