Programming and Data Structures Programming and Data Structures
Lecture 10 Lecture 10
Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>
Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>
Standard Template Library (STL) Standard Template Library (STL)
●
input/output streams: iostream, fstream, sstream, iomanip
●
string management: string
●
standard exceptions: stdexcept
●
containers:
●
sequence containers: vector, deque, list
●
container adapters: stack, queue, priority_queue
●
associative containers: set, map
●
iterators: iterator
●
advanced numerics: complex, numeric, limit
●
generalized algorithms: algorithm, functional
istream, ostream istream, ostream
●
#include <iostream>
●
standard input/output streams (cin of istream class, cout of ostream class)
●
oparators overloaded (>> for istream, << for ostream)
double n;
char str[80];
cout << "Give a number: ";
cin >> n;
cout << "Given number: " << n << endl;
cout << "Give a text: ";
cin >> str; //only one word (until space)
cin.getline(str, 80); //whole sentence (until end of line) cout << cin.gcount(); //the number of characters read
cout << "Given text: " << str << endl;
ifstream, ofstream ifstream, ofstream
●
#include <fstream>
●
ifstream inherited from istream (file reading)
●
ofstream inherited from ostream (writing to file)
char c;
ifstream in("filepath\\filename"); //file opening
if (!in.is_open()) throw runtime_error("File not found");
while (!in.eof()) //end of file check { in >> c; cout << c;
}in.close();
ofstream out;
out.open("filepath\\filename"); //opening after stream creation out << "text " << 7 << endl;
out.close();
string string
●
#include <string>
●
sequence of characters
●
operators overloaded: + (concatenation), [] (item extraction), = (assignment),
<<, >> (streaming)
string str1 = "Hello", str2 = "world", str;
str = str1 + " " + str2;
for (int i = 0; i < str.size(); i++) { cout << str[i];
}cout << str.substr(0, 5); //from first character, 5 characters str.clear();
if (str.empty())
{ cout << "Give a text: ";
cin >> str;
cout << str << endl;
}
istringstream, ostringstream istringstream, ostringstream
●
#include <sstream>
●
istringstream inherited from istream (reading from string)
●
ostringstream inherited from ostream (writing to string)
string str = "1 2 3 4 5";
int a, b;
istringstream in(str); //input stream with str copy in >> a >> b;
cout << a << b << endl;
ostringstream out; //output stream with empty string out << "text " << a;
cout << out.str() << endl; //access to the internal string