The Standard C++ Library

Similar documents
The Standard C++ Library

Function Templates. Consider the following function:

use static size for this buffer

Standard Library Reference

COMP322 - Introduction to C++

Chapter 5. The Standard Template Library.

G52CPP C++ Programming Lecture 18

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

STL: C++ Standard Library

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

Topics. bool and string types input/output library functions comments memory allocation templates classes

C++ 11 and the Standard Library: Containers, Iterators, Algorithms

CSE 100: STREAM I/O, BITWISE OPERATIONS, BIT STREAM I/O

Standard Library. Lecture 27. Containers. STL Containers. Standard Library

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

Introduction to C++ (Extensions to C)

CS197c: Programming in C++

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

Document Number: P0429R4 Date: Reply to: 0.1 Revisions... 1

When we program, we have to deal with errors. Our most basic aim is correctness, but we must

CS11 Advanced C++ Spring 2018 Lecture 2

Bruce Merry. IOI Training Dec 2013

4 Strings and Streams. Testing.

File I/O Christian Schumacher, Info1 D-MAVT 2013

List, Stack, and Queues

CS11 Advanced C++ Lecture 2 Fall

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

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

Programming with Haiku

A Standard flat_map. 1 Revisions. 2 Introduction. 3 Motivation and Scope. 1.1 Changes from R Changes from R0

CPSC 427a: Object-Oriented Programming

Working with Batches of Data

CSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials)

Sets and MultiSets. Contents. Steven J. Zeil. July 19, Overview of Sets and Maps 4

PHY4321 Summary Notes

Exceptions, Templates, and the STL

CS2141 Software Development using C/C++ Stream I/O

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

C++ Input/Output: Streams

COEN244: Class & function templates

CS

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

The Standard Template Library. An introduction

CS11 Advanced C++ Fall Lecture 1

COMP322 - Introduction to C++

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

CS242 COMPUTER PROGRAMMING

CSE 100: STREAM I/O, BITWISE OPERATIONS, BIT STREAM I/O

The Standard Template Library Classes

Chapter 15 - C++ As A "Better C"

Introduction to C++ Systems Programming

Computational Physics

Containers in C++ and Java

This chapter serves mainly to gather and organize information about iterators. Some new concepts are also introduced for completeness.

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

Lecture 5 Files and Streams

EL2310 Scientific Programming

19.1 The Standard Template Library

Object-Oriented Programming for Scientific Computing

More on Templates. Shahram Rahatlou. Corso di Programmazione++

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

1. The term STL stands for?

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Piyush Kumar. input data. both cout and cin are data objects and are defined as classes ( type istream ) class

IS 0020 Program Design and Software Tools

CSE 100: HUFFMAN CODES

I source_beg, source_end; // defines a range of values in a source container // defines the beginning of a range in a destination container

CS3157: Advanced Programming. Outline

Computer Science II Lecture 2 Strings, Vectors and Recursion

7 TEMPLATES AND STL. 7.1 Function Templates

C++ TEMPLATES. Templates are the foundation of generic programming, which involves writing code in a way that is independent of any particular type.

COMP6771 Advanced C++ Programming

The following is an excerpt from Scott Meyers new book, Effective C++, Third Edition: 55 Specific Ways to Improve Your Programs and Designs.

Streams - Object input and output in C++

Starting to Program in C++ (Basics & I/O)

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

Short Notes of CS201

C++ basics Getting started with, and Data Types.

Lecture-5. STL Containers & Iterators

Lecture 21 Standard Template Library. A simple, but very limited, view of STL is the generality that using template functions provides.

SETS AND MAPS. Chapter 9

Scientific Computing

COMP322 - Introduction to C++ Lecture 01 - Introduction

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CS201 - Introduction to Programming Glossary By

Introduction. Programming in C++ Pointers and arrays. Pointers in C and C++ Session 5 - Pointers and Arrays Iterators

Generic Programming. Akim D le Étienne Renault Roland Levillain

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

STL Quick Reference for CS 241

Introduction. Lecture 5 Files and Streams FILE * FILE *

Lecture 9. Introduction

Computer Programming : C++

Lecture on pointers, references, and arrays and vectors

G52CPP C++ Programming Lecture 17

C++ does not, as a part of the language, define how data are sent out and read into the program

Fundamentals of Type-Dependent Code Reuse in C++ Mark Isaacson

Informatik I (D-ITET) Übungsstunde 9, Hossein Shafagh

Transcription:

The Standard C++ Library

Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface

Q: What types can we put in a template param? A: Type which models the requested concept.

Concepts - Example Consider: template<typename T> const T& min(const T& x, const T& y) { return x < y? x : y; } The user must provide a type that has a lessthan operator (<) to compare two values of type T, the result of which must be convertible to a Boolean value

Concepts - Example Consider: template<typename T> const T& min(const T& x, const T& y) { return x < y? x : y; } The user problem: must provide a type that has a lessthan cryptic operator error messages (<) to compare two values of type from T, the the implementation result of which of must the function be convertible to instead a Boolean of a clear value error message

Concepts What we would like it to be: Not C++ syntax: template<lessthancomparable T> const T& min(const T& x, const T& y) { return x < y? x : y; } The user must provide a type that has a lessthan operator (<) to compare two values of type T, the result of which must be convertible to a Boolean value

Concepts and C++11 was not ready for C++11 Can be found in boost template<typename T> const T& min(const T& x, const T& y) { BOOST_CONCEPT_ASSERT((LessThanComparable <T>)); return x < y? x : y; }

Concepts Concepts are not a yet part of the C++ language, were not ready for C++11 There is no (standard) way to declare a concept in a program, or to declare that a particular type is a model of a concept.

Concepts - advanced gcc has a new mechanism for concept checking. To activate it define:_glibcxx_concept_checks Warning: not clear if it helps or not. Boost library has a Concept Check Library: http://www.boost.org/libs/concept_check/concept_check.htm template<typename T> const T& min(const T& x, const T& y) { BOOST_CONCEPT_ASSERT((LessThanComparable <T>)); return x < y? x : y; }

Concepts A concept is a list of requirements on a type. STL defines a hierarchy of concepts for containers, iterators, and element types. Concepts for element types include: Equality Comparable - types with operator==,... LessThan Comparable - types with operator<,... Assignable - types with operator= and copy Ctor

Concepts Cleanly separate interface from implementation. Primitives can also conforms to a concept.

Concepts refinement Concept B is a refinement of concept A Concept B imposes some additional requirements on A Similar to inheritance.

Main Components Function Objects Iterators Adaptors Containers Algorithms Streams Strings

Containers Holds copies of elements. Assumes elements have: Copy Ctor & operator = The standard defines the interface. Two main classes Sequential containers: list, vector,... Associative containers: map, set... Assignable - types with operator= and copy Ctor

Containers documentation

Sequential Containers Maintain a linear sequence of objects

Sequential Containers list - a linked list of objects Efficient insertion/deletion in front/end/middle vector - an extendable sequence of objects Efficient insertion/deletion at end Random access deque double-ended queue Efficient insertion/deletion at front/end Random access

vector<t> Contiguous array of elements of type T Random access Can grow on as needed basis std::vector<int> v(2); v[0]= 45; v[1]= 32; v.emplace_back(60); //C++11

vector<t> Contiguous array of elements of type T Random access Can grow on as needed basis std::vector<int> v(2); v[0]= 45; v[1]= 32; v.push_back(60);// old style, // we will talk about // the difference

size and capacity size capacity The first size elements are constructed (initialized) The last capacity - size elements are uninitialized

size and capacity size capacity size_type size() const size_type capacity() const

Creating vectors Empty vector: std::vector<int> vec; vector with 10 ints each one of value int()==0: std::vector<int> vec(10); std::vector<int> vec(10,0); // better vector with 10 ints with the value of 42: std::vector<int> vec(10, 42);

Creating vectors Empty vector: std::vector<int> vec; vector with 10 ints each one of value int()==0: std::vector<int> vec(10); std::vector<int> vec(10,0); // better Notice: int() is NOT a default constructor of int. Uninitialized ints (int k;) contain junk.

Creating vectors Empty vector: std::vector<fred> vec; vector with 10 default constructed Fred objects. i.e: 10 Fred() objects: std::vector<fred> vec(10); vector with 10 non-default constructed Fred objects: std::vector<fred> vec(10, Fred(5,7));

Creating vectors: C++11 vector with different ints inside it: std::vector<int> vec{1, 5, 10, -2, 0, 3}; vector with different Fred objects: std::vector<fred> vec{fred(5,7), Fred(2,1) } Or std::vector<fred> vec{ {5,7}, {2,1} }

Associated types in vector vector<typename T>:: value_type - The type of object, T, stored reference - Reference to T const_reference - const Reference to T iterator - Iterator used to iterate through a vector...

Time complexity Random access to elements. Amortized constant time insertion and removal of elements at the end. Linear time insertion and removal of elements at the beginning or in the middle. vector is the simplest of the STL container classes, and in many cases the most efficient.

Adding elements Inserts a new element at the end: void push_back(const T&) a.push_back(t) equivalent to: a.insert(a.end(), t) } amortized constant time insert is linear time in the beginning or in the middle.

Adding elements-c++11 Construct and insert a new element at the end: template<typename Args> void emplace_back(args&& args) a.emplace_back(t) equivalent to: } a.emplace(a.end(), t) amortized constant time emplace is linear time in the beginning or in the middle.

Accessing elements Without boundary checking: reference operator[](size_type n) const_reference operator[](size_type n) const With boundary checking: reference at(size_type n) const_reference at(size_type n) const

What about checking boundaries only in DEBUG mode? - Linux g++ has a standard library in DEBUG mode, to activate it define _GLIBCXX_DEBUG (g++ -D_GLIBCXX_DEBUG ) stlport is an implementation of the standard library which includes DEBUG mode (havn't checked it myself yet): http://www.stlport.org/

What about checking boundaries only in DEBUG mode? - MS In MSVS 2012 Debug build is automatically safe and Release build mode is not Other versions also have these kind of Safe STL modes but might require defining some flags to turn off or on.

vector<t> Contiguous array of elements of type T We can get the underlining T* pointer: if size()>0 T* p= &(v[0]) c++11: T* p= v.data()

vector<t> Contiguous array of elements of type T We can get the underlining T* pointer: if size()>0 T* p= &(v[0]) c++11: T* p= v.data() Useful for interfacing with C or wrappers that work with C like Matlab s mex

vector<t> v

vector<t> v v.shrink_to_fit() // c++11

vector<t> v v.shrink_to_fit() // c++11 or

Associative Containers Supports efficient retrieval of elements (values) based on keys. (Typical) Implementation: red-black binary trees hash-table (added in c++11)

Sorted Associative Containers Set A set of unique keys Map Associate a value to key (associative array) Unique value of each key Multiset Multimap Same, but allow multiple values

Sorted Associative Containers & Order Sorted Associative containers use operator< as default order We can control order by using our own comparison function To understand this issue, we need to use function object Function Objects

Anything that can be called as if a function. For example: Pointer to function Function Objects A class that implements operator() Lambda expressions (c++11)

Example class c_str_less { public: bool operator()(const char* s1, const char* s2) { return (strcmp(s1,s2) < 0); } }; c_str_less cmp; // declare an object if(cmp( aa, ab )) if( c_str_less()( a, b ) ) Creates temporary objects, and then call operator()

Template comparator example template<typename T> class less { public: bool operator()(const T& lhs, const T& rhs) { return lhs < rhs; } }; less<int> cmp; // declare an object if( cmp(1,2) ) Creates temporary objects, and then call operator() if( less<int>()(1,2) )

Using Comparators // ascending order // uses operator < for comparison set<int> s1; set<int,less<int>> s1; // same // descending order // uses operator > for comparison set<int,greater<int>> s2;

Using Comparators set<int,mycomp> s3; Creates a default constructed MyComp object. MyComp cmp(42); set<int,mycomp> s4(cmp); Use given MyComp object.

Why should we use classes So we get the power of classes. Examples: Inheritance. as function objects? To parameterize our functions in run time or in compile time. To accumulate information.

Why should we use classes as function objects?

Function object example: dice code example

input Iterators ++, input Trivial Iterator ++, output Input Iterator Output Iterator Forward Iterator Bi-directional Iterator Random-Access Iterator ++, I/O ++, --, I/O Pointer arithmetic

Iterator Types Output: write only and can write only once Input: read many times each item Forward supports both read and write Bi-directional support also decrement Random supports random access (just like C pointer)

Iterators & Containers Bidirectional iterators: list, map, set Random access iterators: vector Input/output/forward iterators: iostreams

Iterators & Containers class NameOfContainer { typedef iterator; // iterator type iterator begin(); // first element iterator end(); // element after last NameOfContainer<...> c NameOfContainer<...>::iterator it; for( it= c.begin(); it!=c.end(); ++it) // do something that changes *it

Iterators & Containers: c++11 class NameOfContainer { typedef iterator; // iterator type iterator begin(); // first element iterator end(); // element after last NameOfContainer<...> c for(auto it= c.begin(); it!=c.end(); ++it) // do something that changes *it

Iterators & Containers: c++11 class NameOfContainer { typedef iterator; // iterator type iterator begin(); // first element iterator end(); // element after last NameOfContainer<...> c for(auto& val : c) // do something that changes val

const_iterators & Containers class NameOfContainer { typedef const_iterator; // iterator type const_iterator begin() const; // first element const_iterator end() const; // element after last NameOfContainer<...> c NameOfContainer<...>::const_iterator it; for( it= c.begin(); it!=c.end(); ++it) // do something that does not change *it

const_iterators & Containers: c++11 class NameOfContainer { typedef const_iterator; // iterator type const_iterator cbegin() const; // first element const_iterator cend() const; // element after last NameOfContainer<...> c for(auto it= c.cbegin(); it!=c.cend(); ++it) // do something that does not change *it

const_iterators & Containers: c++11 class NameOfContainer { typedef const_iterator; // iterator type const_iterator cbegin() const; // first element const_iterator cend() const; // element after last NameOfContainer<...> c for(const auto& val : c) // do something that does not change val

const_iterators & Containers const_iterator cbegin() const; const_iterator cend() const; const_iterator begin() const; const_iterator end() const; iterator begin(); iterator end(); Note that the begin() and end() methods that return regular iterator are not const methods. i.e: if we get a container by const (const ref,...) we can't use these methods. We have to use the methods that return const_iterator

Iterators & Sequence Containers SeqContainerName<...> c; SeqContainerName<...>::iterator i,j; c.insert(i,x) inserts x before i c.insert(i,first,last) inserts elements in [first,last) before i c.erase(i) erases the element that i points to c.erase(i,j) erase elements in range [i,j)

Iterators & Sequence Containers c++11 SeqContainerName<...> c; SeqContainerName<...>::iterator i,j; c.emplace(i,p1,...,pn): Constructs and inserts before i an object with a constructor that gets p1,...,pn parameters

Iterators & other Containers insert and erase has the same ideas, except they keep the invariants of the specific container. For example, a Sorted Associative Container will remain sorted after insertions and erases.

Iterators & other Containers So what does c.insert(pos,x) does, when c is a Unique Sorted Associative Container? Inserts x into the set, using pos as a hint to where it will be inserted.

Iterators & other Containers: c++11 So what does c.emplace_hint(pos,x) does, when c is a Unique Sorted Associative Container? Constructs and Inserts x into the set, using pos as a hint to where it will be inserted.

Iterator validity When working with iterators, we have to remember that their validity can change What is wrong with this code? Container<...> c;... for(auto i= c.begin(); i!=c.end(); ++i ) if( f( *i ) ) { // some test c.erase(i); }

Iterator validity Two cases: list, set, map i is not a legal iterator

Iterator validity Two cases: list, set, map i is not a legal iterator

Iterator validity Two cases: list, set, map i is not a legal iterator

Iterator validity Two cases: list, set, map i is not a legal iterator vector i points to the element after 0 1 2 3 4 5 6 7

Iterator validity Two cases: list, set, map i is not a legal iterator vector i points to the element after 0 1 3 4 5 6 7

Iterator validity Two cases: list, set, map i is not a legal iterator vector i points to the element after In either case, this is not what we want

Iterator validity second try... Container<...> c; auto i= c.begin(); while( i!=c.end() ) { auto j= i; ++i; if( f( *j ) ) { // some test c.erase(j);... }Works for set, map, list, not vector or deque

Iterators & Map Suppose we work with: map<string,int> dictionary; map<string,int>::iterator it; it = dictionary.begin(); What is the type of *it?

Iterators & Map Every STL container type Container defines Container::value_type Type of elements stored in container This is the type returned by an iterator Container::value_type operator*();

Iterators & Map Ok, so what type of elements does a map return? map<keytype,valuetype> keeps pairs KeyType key key of entry ValueType value value of entry

Pairs template< typename T1, typename T2> struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; pair( const T1& x, const T2& y ) : first(x), second(y) {} };

Map value_type template< typename Key, typename T, typename Cmp = less<key> > class map { public: typedef pair<const Key, T> value_type; typedef Key key_type; typedef T mapped_type; typedef Cmp key_compare; };

Using map iterator map<string,int> dict; for( auto i = dict.cbegin(); i!= dict.cend(); ++i ) { cout << i->first << << i->second << \n ; }

Using map iterator map<string,int> dict; } for( const auto& val : dict) { cout << val.first << << val.second << \n ;

Iterators and Assoc. Containers Additional set of operations: iterator C::find(key_type const& key) Return iterator to first element with key. Return end() if not found iterator C::lower_bound(key_type const& key) Return iterator to first element greater or equal to key iterator C::upper_bound(key_type const& key) Return iterator to first element greater than key

Adaptors Good functionality, wrong interface For example, adaptors of basic containers with limited interface: stack<t,seq> queue<t,seq>

stack<t,seq> provides emplace, push, pop, top, size, empty,... Notice that unlike java, pop, is not returning a value. i.e: it's a void function. The reason (historic with c++-11?): to make pop return a value it would be either inefficient or wrong: http://www.sgi.com/tech/stl/stack.html#3

Most STL algorithms works on sequences Sequences are passed as two iterators: beginning element element one after last p Algorithms q sequence [p,q) Algorithms depend on iterator type not on container type

Example merge documentation

copy template< typename In, typename Out> Out copy(in first, In last, Out res) { while (first!= last) *res++ = *first++; return res; }

copy template< typename In, typename Out> Out copy(in first, In last, Out res) { } while (first!= last) *res++ = *first++; return res; What's wrong with this? void foo(const vector<char>& v) { vector<char> v2;... copy(v.begin(),v.end(), v2.begin());

copy template< typename In, typename Out> Out copy(in first, In last, Out res) { } while (first!= last) *res++ = *first++; return res; What's wrong with this? void foo(const vector<char>& v) { vector<char> v2;... copy(v.begin(),v.end(), v2.begin()); OUT OF BOUND!

So how can we copy and insert? Solution #1: Use insert explicitly void foo(const vector<char>& v) { vector<char> v2;... v2.insert(v2.end(),v.begin(),v.end());

So how can we copy and insert? Solution #2: Use insert_iterator<container>. In my humble opinion, not so good as it's not so readable): void foo(const vector<char>& v) { vector<char> v2;... copy(v.begin(),v.end(), insert_iterator<vector<char>>(v2,v2.begin()) ;

sort using operator < template <class RandomAccessIterator> void sort(randomaccessiterator first, RandomAccessIterator last); Example usage(the hard way): sort< vector<int>::iterator > (vec.begin(), vec.end());

sort using operator < template <class RandomAccessIterator> void sort(randomaccessiterator first, RandomAccessIterator last); Example usage: sort(vec.begin(), vec.end());

sort using comparator template <class RandomAccessIterator, class StrictWeakOrdering> void sort(randomaccessiterator first, RandomAccessIterator last, StrictWeakOrdering comp); Example usage: sort(vec.begin(), vec.end(), greater<int>());

sort compile error list<int> l(nums, nums+size); sort(l.begin(), l.end());

sort compile error list<int> l(nums, nums+size); sort(l.begin(), l.end()); list iterators are bidirectional and not random access!

g++ /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function void std::sort(_randomaccessiterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_list_iterator<int>] : Main.cpp:17: instantiated from here /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:2713: error: no match for operator- in last - first /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_bvector.h:182: note: candidates are: ptrdiff_t std::operator-(const std::_bit_iterator_base&, const std::_bit_iterator_base&) /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function void std:: final_insertion_sort(_randomaccessiterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_list_iterator<int>] : /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:2714: instantiated from void std::sort(_randomaccessiterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_list_iterator<int>] Main.cpp:17: instantiated from here /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:2357: error: no match for operator- in last - first...

g++ /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function void std::sort(_randomaccessiterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_list_iterator<int>] : Main.cpp:17: instantiated from here /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:2713: error: no match for operator- in last - first /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_bvector.h:182:??? note: candidates are: ptrdiff_t std::operator-(const std::_bit_iterator_base&, const std::_bit_iterator_base&) /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function void std:: final_insertion_sort(_randomaccessiterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_list_iterator<int>] : /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:2714: instantiated from void std::sort(_randomaccessiterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_list_iterator<int>] Main.cpp:17: instantiated from here /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:2357: error: no match for operator- in last - first...

g++ /usr/lib/gcc/i486-linuxgnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_alg o.h: In function void std::sort(_randomaccessiterator, _RandomAccessIterator) [with _RandomAccessIterator = std::_list_iterator<int>] : Main.cpp:17: instantiated from here /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:2713: error: no match for operator- in last - first...

g++ -D_GLIBCXX_CONCEPT_CHECKS and STLFilt BD Software STL Message Decryptor v2.47a for gcc stl_algo.h: In function void sort(_list_iterator<int>, _List_iterator< int>) : Main.cpp:17: instantiated from here stl_algo.h:2713: error: no match for operator- in last - first stl_algo.h: In function void final_insertion_sort( _List_iterator<int>, _List_iterator<int>) : stl_algo.h:2714: instantiated from void sort( _List_iterator<int>, _List_iterator<int>) Main.cpp:17: instantiated from here...

g++ -D_GLIBCXX_CONCEPT_CHECKS and STLFilt... Main.cpp:17: instantiated from here boost_concept_check.h:223: error: conversion from bidirectional_iterator_tag to non-scalar type random_access_iterator_tag requested

Cryptic error messages STLFilt: An STL Error Message Decryptor for C++: http://www.bdsoft.com/tools/stlfilt.html

Cryptic error messages Different compilers: clang++ (free) intel c++ (not free)

Streams

Output stream ostream is the type defined by the library for output streams. cout is a global object of type ostream attached to stdout. Examples: #include <iostream> std::cout << 7; std::cout << Hello World\n ;

Output stream continued The ostream overloads the << operator for each basic type. The operator returns a reference to the output stream, which allows combined output: std::cout << 2 + 3 = << 2 + 3 << std::endl;

Error output stream objects cerr is similar to cout, it is attached to stderr. cerr is unbuffered. clog is like cerr, but uses buffered output.

Output stream continued The ostream overloads the << operator for each basic type. The operator returns a reference to the output stream, which allows combined output: std::cout << 2 + 3 = << 2 + 3 << std::endl; endl is an example of manipulator. endl sends a \n character to the stream and flushes it. In fact endl is a function: << operator has a version that accepts a function pointe

Input stream istream is the type defined by the library for input streams. cin is a global object of type istream attached to stdin. Example: #include <iostream> int i; std::cin >> i; // reads an int Note that >> skips whitespaces.

Input stream continued When an error occurs (typically because the input format is not what we expect) cin enters a failed state and evaluates to false. istream overloads the! opeator and the void* (conversion) operator normal usage: while (std::cin >>...

Input stream errors In failed state istream will produce no input. istream rewinds on error. Use clear() method to continue reading.

More I/O methods Both ostream and istream have additional methods: ostream& put(char ch) ostream& write(char const *str, int length) int get() // read one char istream& get(char& ch) // read one char getline(char *buffer, int size, int delimiter = '\n') Examples: std::cout.put( a ); char ch1, ch2, str[256]; std::cin.get(ch1).get(ch2); std::cin.getline(str, 256);

File streams To read/write from/to files we can use the ifstream, ofstream and fstream. Example: std::ofstream outfile( out.txt ); outfile << Hello world << endl; int i; std::ifstream infile( in.txt ); while (infile >> i) {... }

Containers output Assuming that the element type can be output to ostream using << You can simply iterate through the container and output each element: vector<int> vec; //... for (const auto& val:vec) cout << val << " "; You can use ostream_iterator: vector<int> vec; //... copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, " "));

Containers output You can write ostream << operator for the containers with your specific format: ContainersOutput example

Binary files

Leading example: image files Images are stored as matrices of numbers (pixels) Here, we deal with gray-scale images 8 bits per pixel i.e. each pixel between 0 and 255 255 is white, 0 is black, others are gray 255 0 0 255 0 100

storing images How can we store images on files? For each image we want to store: width height number of bytes per pixel the pixels Requirements: read/write easily, save space, save computation, etc.

First try: text files storing images cons: long needs parsing pros: readable by humans easy to edit myimg.txt width = 3 height = 2 bytes_per_pixel = 1 255 0 0 0 255 100

storing images Better solution: Binary files Save the data the way the computer holds it pros: Smaller No parsing (faster) cons: hard to read for humans Machine dependant Widely used: JPEG, mp3, BMP, other data

Images as binary files width 4 bytes [0,,0,0,1,1] height 4 bytes [0,,0,0,1,0] 3 2 1 255 0 0 0 255 100

Images as binary files 3 2 bytes per pixel 1 byte [0,0,0,0,0,0,0,1] 1 255 0 0 0 255 100

Images as binary files pixel 1 byte [1,1,1,1,1,1,1,1] 3 2 1 255 0 0 pixel 1 byte [0,0,0,0,0,0,0,0] 0 255 100

Binary example

Strings

What is a string? a typedef for basic_string<char> The basic_string class represents a Sequence of characters. It contains: all the usual operations of a Sequence. standard string operations such as search and concatenation.

Rope: non standard! Scalable string implementation. Efficient operation that involve the string as a whole. A rope is a reasonable representation for very long strings such as edit buffers or mail messages.

Rope: non standard! #include <ext/rope> #include <iostream> using namespace std; int main() { gnu_cxx::crope r("this should be longer!"); cout << r << endl; }

How to convert something to a string? c++11: to_string for primitives Using std::ostringstream We can encapsulate the string conversion and format into stringify functions stringify example

More? Lots of other features, especially in c++11 (threads, ) Other libraries: Boost opencv, dlib, armadillo, zlib, Ceemple: c++ IDE with lots of libraries preinstalled, fast recompilation (recompilation currently only for windows)