The Standard C++ Library

Slides:



Advertisements
Similar presentations
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Advertisements

C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
. Plab – Tirgul 8 I/O streams Example: string class.
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
. The Standard C++ Library. 2 Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
. STL: C++ Standard Library. Main Ideas u General purpose: generic data structures & algorithms, templates u Flexibility: Allows for many combinations.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
Standard library types Practical session # 3 Software Engineering
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Data Structures Using C++ 2E
CNS  Sequences  vector,deque,list,(string),forward_list  Container Adapters  queue, stack, priority_queue  Associative Containers  set, unordered_set.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
SNU OOPSLA Lab. Chap17. Standard Containers © copyright 2001 SNU OOPSLA Lab.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Templates code reuse - inheritance - template classes template classes - a class that is not data-type specific - eg. a class of Array of any type - intArray,
CS Midterm Study Guide Fall General topics Definitions and rules Technical names of things Syntax of C++ constructs Meaning of C++ constructs.
An Introduction to STL. The C++ Standard Template Libraries  In 1990, Alex Stepanov and Meng Lee of Hewlett Packard Laboratories extended C++ with a.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
 2003 Prentice Hall, Inc. All rights reserved.m ECE 2552 Dr. Këpuska based on Dr. S. Kozaitis Summer Chapter 15 - Class string and String Stream.
A gentle introduction to the standard template library (STL) Some references:
Concepts in C++. Templates in current C++ C++ template is typeless No language support for constrained generics Accidental errors found in instantiation.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Managers and “mentors” identified on projects page. All member accounts created and projects populated.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things –Point to the start of a range of elements (in a container)
Chapter 8 Writing Generic Functions. Objectives Understand the use of generic functions. Learn about the use of templates, their advantages and pitfalls.
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Hank Childs, University of Oregon
Motivation for Generic Programming in C++
CS212: Object Oriented Analysis and Design
“Generic Programming” ECE 297
Introduction to C++ (Extensions to C)
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
The Standard C++ Library
Programming with ANSI C ++
C++ Templates.
Exceptions, Templates, and the Standard Template Library (STL)
C++ Standard Library.
Standard Template Library (STL)
Starting Out with C++ Early Objects Eighth Edition
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
The C++ Algorithm Libraries
What remains Topics Assignments Final exam
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Associative Structures
A Sorted, Unique Key Container
Elements are always copied when they are put into a container
Lists - I The List ADT.
Lists - I The List ADT.
Standard Template Library (STL)
COP 3330 Object-oriented Programming in C++
Lab4 problems More about templates Some STL
Standard Template Library
Standard Template Library
An Introduction to STL.
Chapter 3 Lists, Stacks, and Queues
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
SPL – PS1 Introduction to C++.
A dictionary lookup mechanism
Standard Template Library
Presentation transcript:

The Standard C++ Library

Main Ideas Purpose Flexibility Efficiency Simple & Uniform Interface

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

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

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

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

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 “were not ready” for C++11 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 To activate it define:_GLIBCXX_CONCEPT_CHECKS 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 STL defines a hierarchy of concepts for containers, 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 Iterators Function Objects Adaptors Containers Algorithms Streams Strings

Holds copies of elements. 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

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_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) insert is linear time in the beginning or in the middle. } amortized constant time

} 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) emplace is linear time in the beginning or in the middle. } amortized constant time

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

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

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

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

Example class c_str_less { public: bool operator()(const char* s1, 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) ) … if( less<int>()(1,2) ) Creates temporary objects, and then call operator()

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; MyComp cmp(42); set<int,MyComp> s4(cmp); Creates a default constructed MyComp object. Use given MyComp object.

Why should we use classes as function objects? So we get the “power” of classes. Examples: Inheritance. 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

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

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 vector i is not a legal iterator vector i points to the element after 1 2 3 4 5 6 7

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

this is not what we want… 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: What is the type of *it ? 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 Algorithms depend on iterator type not on container type p q sequence [p,q)

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 What's wrong with this ? 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 What's wrong with this ? 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 ? OUT OF BOUND ! void foo(const vector<char>& v) { vector<char> v2; ... copy(v.begin(),v.end(), v2.begin());

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); sort< vector<int>::iterator > (vec.begin(), vec.end()); Example usage(the hard way):

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

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

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

list iterators are bidirectional and not random access ! 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>]’ /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>]’ /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++ Main.cpp:17: instantiated from here /usr/lib/gcc/i486-linux- gnu/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>)’ ...

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: You can use ostream_iterator: vector<int> vec; // ... for (const auto& val:vec) cout << val << " "; 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 255 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.

storing images First try: text files 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] 3 2 1 255 height [0,…,0,0,1,0] 255 100

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

Images as binary files pixel 1 byte [1,1,1,1,1,1,1,1] 3 2 1 255 pixel pixel 1 byte [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 pre- installed, fast recompilation (recompilation currently only for windows)