Tutorial by Examples

std::ostringstream is a class whose objects look like an output stream (that is, you can write to them via operator<<), but actually store the writing results, and provide them in the form of a stream. Consider the following short code: #include <sstream> #include <string> ...
Reading a text file line-by-line A proper way to read a text file line-by-line till the end is usually not clear from ifstream documentation. Let's consider some common mistakes done by beginner C++ programmers, and a proper way to read the file. Lines without whitespace characters For the sake o...
Basic printing std::ostream_iterator allows to print contents of an STL container to any output stream without explicit loops. The second argument of std::ostream_iterator constructor sets the delimiter. For example, the following code: std::vector<int> v = {1,2,3,4}; std::copy(v.begin(), v...
Parsing files into STL containers istream_iterators are very useful for reading sequences of numbers or other parsable data into STL containers without explicit loops in the code. Using explicit container size: std::vector<int> v(100); std::copy(std::istream_iterator<int>(ifs), std::...

Page 1 of 1