What is the type of std::cout? Where does a stream go?

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

This algorithm is used for dimension reduction. Input: a set of vectors {Xn є }, and dimension d,d
Chapter 2: Basic Elements of C++
Chapter 21 The STL (maps and algorithms) Bjarne Stroustrup
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.
Thrust & Curand Dr. Bo Yuan
Behavioral Pattern: Visitor C h a p t e r 5 – P a g e 224 When an algorithm is separated from an object structure upon which it operates, new operations.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
C++ provides the following classes to perform output and input of characters to/from files: ofstream: Stream class to write on filesofstream ifstream:
Vectors, lists and queues
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline some useful problems.
Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : 1.Start at the beginning of the data set. 2.Compare the first two elements,
Exercise 2.
STL Antonio Cisternino. Introduction The C++ Standard Template Library (STL) has become part of C++ standard The main author of STL is Alexander Stephanov.
Multimaps. Resources -- web For each new class, browse its methods These sites are richly linked, and contain indexes, examples, documents and other resources.
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
LECTURE 4: The VHDL N-bit Adder
Add two strings representing numbers Carry = 0 S1 = S2 = S3 =
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
More on the STL vector list stack queue priority_queue.
Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps.
Announcements Final Exam:. Review Passing Arrays as Parameters Pass Array Name into Function int intArray[100]; func(intArray); Accept into Function as.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
Object-Oriented Programming in C++
CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
STL Standard Template Library ● Good reference book: – The C++ Standard Library ● A Tutorial and Reference ● by Nicolai M. Josuttis ● 1999 – Addison Wesley.
 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.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Ticket Booth Base Level 3 1. In the completed program, the ticket seller will: Select a venue from a menu of all venues. Select a show from a menu of.
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
Computer Science 101 More Devices: Arithmetic. From 1-Bit Equality to N-Bit Equality = A B A = B Two bit strings.
1 Manipulators manipulators are used only in input and output statements endl, fixed, showpoint, setw, and setprecision are manipulators that can be used.
A recap of the STL and more containers Plus an intro to string and file input and output! Lecture 8.
Topic: Code Essential Questions Digital Cornell Notes.
1 The Standard Template Library Drozdek Section 3.7.
Files To read a file – We must know its name – We must open it (for reading) – Then we can read – Then we must close it That is typically done implicitly.
Standard Form Objective: To be able to understand and use standard form.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
ΜΕΤΑΣΥΛΛΕΚΤΙΚΗ ΦΥΣΙΟΛΟΓΙΑ ΕΡΓΑΣΤΗΡΙΟ 3. Μετασυλλεκτική Εργ3-Λιοσάτου Γ.2 ΒΙΟΛΟΓΙΚΟΙ ΠΑΡΑΓΟΝΤΕΣ ΠΟΥ ΕΠΗΡΕΑΖΟΥΝ ΤΗ ΦΘΟΡΑ ΤΩΝ ΟΠΩΡΟΚΗΠΕΥΤΙΚΩΝ Αναπνοή Η λειτουργία.
Lecture 7-2 : STL Iterators
C++ Standard Library.
Hire Toyota Innova in Delhi for Outstation Tour
Generic Programming Techniques in C++
مقدمه تفکر استراتژیک چیست؟. فرامین و فنون تفکر استراتژیک ده فرمان و بیست فکرافزار کاربردی.
Lecture 7-2 : STL Iterators
GTECH 709 Vector data models
إعداد المشرفة التربوية نجلاء الجارد
Topological Ordering Algorithm: Example
مقدمه ای بر مهارت های زندگی
اثرات گرمايش جهاني تغييرات آب و هوا، تأثيرات عميق و شديدي بر بسياري از عوامل اساسي موثر بر سلامت از جمله : آب، غذا، هوا و محيط زيست دارد كه اين مورد خود.
10:00.
CSE 303 Lecture 25 Loose Ends and Random Topics
Circles! You are going to create an “image” with circle(s)
Theorems about LINEAR MAPPINGS.
Domain range A A-1 MATRIX INVERSE.
Lecture 8-2 : STL Iterators and Algorithms
Topological Ordering Algorithm: Example
STL Библиотека стандартных шаблонов
Topological Ordering Algorithm: Example
Print the following triangle, using nested loops
STL and Example.
Standard C++ Library Part II.
An Introduction to STL.
Introduction to Algorithms and Programming COMP151
Recitation Outline Hash tables in C++ STL Examples Recursive example
Topological Ordering Algorithm: Example
CSE Module 1 A Programming Primer
Presentation transcript:

What is the type of std::cout? Where does a stream go?

Paul and Jez's Stream-a-poloza Paul Grenyer Jez Higgins

Let's talk about you...

● std::string In the last three months, have you used...

Let's talk about you... ● std::string ● std::vector In the last three months, have you used...

Let's talk about you... ● std::string ● std::vector ● std::map In the last three months, have you used...

Let's talk about you... ● std::string ● std::vector ● std::map ● an iterator In the last three months, have you used...

Let's talk about you... ● std::string ● std::vector ● std::map ● an iterator ● an algorithm In the last three months, have you used...

Let's talk about you... ● std::string ● std::vector ● std::map ● an iterator ● an algorithm ● operator<< In the last three months, have you used...

Let's talk about you... ● std::string ● std::vector ● std::map ● an iterator ● an algorithm ● operator<< ● a custom stream In the last three months, have you used...

ios_base basic_ios basic_iostream basic_istream basic_ostream virtual

● Streams the write std::cout << “Hello, world!”; ● Stream the read int n; std::cin >> n; ● Streams that do both std::stringsteam ss;... ss > some_string;

ios_base basic_ios basic_iostream basic_istream basic_ostream virtual basic_ostringstream

ios_base basic_ios basic_iostream basic_istream basic_ostream virtual basic_ostringstream basic_istringstream

ios_base basic_ios basic_iostream basic_istream basic_ostream virtual basic_stringstream basic_ostringstream basic_istringstream

“The header defines a type and several function signatures that control output to a stream buffer.” explicit basic_ostream( basic_streambuf * sb);

ios_base basic_ios basic_iostream basic_istream basic_ostream virtual basic_stringstream basic_ostringstream basic_istringstream

basic_streambuf

“The header defines types that control input from and output to character sequences”

basic_streambuf basic_stringbuf

This is all very well, but why? ● Known interface ● All the formatting stuff works ● Do you want printf/sprintf/fprintf/snprintf? ● Write to something other than the console, memory or a file?

Digging A Ditch Writing a Custom Stream A Logging Stream must: Buffer the characters streamed to it Flush to another output stream Send time and date characters to an output stream

Stream Buffer The heart of a stream ● Inherit from std::basic_streambuf ● Implement a buffer ● Override virtual functions: – overflow – sync – xsputn ● Flush to another output stream

Stream Buffer Implement a buffer #include #include template > class logoutbuf : public std::basic_streambuf { private: typedef std::vector buffer_type; buffer_type buffer_;... };

Stream Buffer Adding to the buffer virtual int_type overflow( int_type c ) { if ( !traits::eq_int_type( c, traits::eof() ) ) { buffer_.push_back( c ); } return traits::not_eof( c ); }

Stream Buffer Setting the output stream private: typedef typename std::basic_streambuf ::int_type int_type; std::basic_streambuf * out_; public: logoutbuf() : out_( 0 ), buffer_() { } void init( std::basic_ostream * out ) { out_ = out; }

Stream Buffer Flushing the buffer virtual int sync() { if ( !buffer_.empty() && out_ ) { out_->sputn( &buffer_[0], static_cast ( buffer_.size() ) ); buffer_.clear(); } return 0; }

Stream Buffer When to flush the buffer template > class logoutbuf : public std::basic_streambuf {... public:... ~logoutbuf() { sync(); }... };

Stream Buffer Generating time data string private: std::basic_string format_time() { // Get current time and date time_t ltime; time( &ltime ); // Convert time and date to string std::basic_stringstream time; time << asctime( gmtime( &ltime ) ); // Remove LF from time date string and // add separator std::basic_stringstream result; result << time.str().erase( time.str().length() - 1 ) << " - "; return result.str(); }

Stream Buffer Prepending the time date string virtual int sync() { if ( !buffer_.empty() && out_ ) { const std::basic_string time = format_time(); out_->sputn( time.c_str(), static_cast ( time.length() ) ); out_->sputn( &buffer_[0], static_cast ( buffer_.size() ) ); buffer_.clear(); } return 0; }

Stream Buffer In the name of efficiency virtual std::streamsize xsputn( const char_type* s, std::streamsize num ) { std::copy( s, s + num, std::back_inserter ( buffer_ ) ); return num; }

The Stream The conventional Method template > class ostream : public std::basic_ostream { private: logoutbuf buf_; public: ostream() : std::basic_ostream ( &buf_ ), buf_() { } }; What’s wrong with this?

The Stream Private Base Class (1) template > struct logoutbuf_init { private: logoutbuf buf_; public: logoutbuf * buf() { return &buf_; } };

The Stream Private Base Class (2) template > class logostream : private logoutbuf_init, public std::basic_ostream { private: typedef outbuf_init logoutbuf_init; public: logostream() : logoutbuf_init(), std::basic_ostream ( logoutbuf_init::buf() ) { } };

The Stream basic_ios ios_base basic_ios basic_ostream virtual /2 - Effects: Constructs an object of class basic_ios ( ) leaving its member objects uninitialized. The object must be initialized by calling its init member function. If it is destroyed before it has been initialized the behavior is undefined.

The Stream Initialisation Order (1) … basic_ios logoutbuf logoutbuf_init basic_ostream logostream

The Stream Inherit Virtually and Privately t emplate > class logostream : private virtual logoutbuf_init, public std::basic_ostream { private: typedef outbuf_init logoutbuf_init; public: logostream() : logoutbuf_init(), std::basic_ostream ( logoutbuf_init::buf ) ) { } } ;

The Stream Initialisation Order (2) logoutbuf logoutbuf_init … basic_ios basic_ostream Logostream

The Stream Initialising the output stream public: logostream( std::basic_ostream & out ) : logoutbuf_init(), std::basic_ostream ( logoutbuf_init::buf() ) { logoutbuf_init::buf()->init( out.rdbuf() ); }

Using the Stream typedef logostream clogostream; typedef logostream wlogostream;... int main() { clogostream;out( std::cout ); out << "31 hexadecimal: " << std::hex << 31 << std::endl; return 0; }

Using the Stream Output Fri Apr 20 16:00: hexadecimal: 1f

Custom IOStream Applications ● Click here to get audience to write slide

Transcoding ● ISO to UTF-8 ● ASCII to UTF-16 ● Windows-1252 to ISO ● Newline conversions ● HTML entity escaping

Compression/Encryption ● Base64 ● zlib ● LZH ● Error correction

Sources and Sinks ● sockets ● a database ● a CVS repository ● memory-mapped file ● a vector ● file descriptor ● an array ● a window

Wacky Stuff ● Occam2 style channels

Code you can use ● Boost::IOStreams ● Aeryn ● Arabica ● Standard C++ IOStreams and Locales by Langer & Kreft

You. Again. ● a custom IOStream In the last three months, might you use...