Programowanie i struktury danych Programowanie i struktury danych
Wykład 9 Wykład 9
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)
●
Standardowa Biblioteka Szablonów języka C++
●
strumienie wejścia/wyjścia: iostream, fstream, sstream, iomanip
●
łańcuchy: string
●
wyjątki standardowe: stdexcept
●
kontenery:
●
kontenery sekwencyjne: vector, deque, list
●
adaptery kontenerów: stack, queue, priority_queue
●
kontenery asocjacyjne: set, map
●
iteratory: iterator
●
operacje numeryczne: complex, numeric, limit
●
uogólnione algorytmy: algorithm, functional
istream, ostream istream, ostream
●
#include <iostream>
●
strumienie standardowego wejścia/wyjścia (cin jest obiektem klasy istream, cout jest obiektem klasy ostream)
●
przeciążone operatory (operator>> dla istream, operator<< dla 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.ignore(); //ignore one character (end of line) 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 używamy tak jak istream (odczyt z pliku)
●
ofstream używamy tak jak ostream (zapis do pliku)
char c;
ifstream in("filepath\\filename"); //file opening
if (!in) throw runtime_error("File not found"); //operator!
while (!in.eof()) //end of file check { in >> c; cout << c;
}in.close();
ofstream out;
out.open("filepath\\filename"); //opening after construction out << "text " << 7 << endl;
out.close();
string string
●
#include <string>
●
ciąg znaków (łańcuch znakowy)
●
przeciążone operatory: + (łączenie, konkatenacja), [] (wyodrębnianie znaków),
= (przypisanie), <, > (porównanie), >>, << (strumieniowe wejście/wyjście)
string str1 = "Hello", str2 = "world", str;
str = str1 + " " + str2;
for (size_t i = 0; i < str.size(); i++) { cout << str[i];
}cout << str.substr(0, 5); //from first character, 5 characters cout << str.find("world"); //first position of the pattern str.clear();
if (str.empty()) {
cout << "Give a text: ";
cin >> str; //only one word (until space)
getline(cin, str); //whole sentence (until end of line) cout << str << endl;
istringstream, ostringstream istringstream, ostringstream
●
#include <sstream>
●
istringstream używamy tak jak istream (odczyt z łańcucha)
●
ostringstream używamy tak jak ostream (zapis do łańcucha)
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