• Nie Znaleziono Wyników

Programming and Data Structures Programming and Data Structures

N/A
N/A
Protected

Academic year: 2021

Share "Programming and Data Structures Programming and Data Structures"

Copied!
6
0
0

Pełen tekst

(1)

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>

(2)

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

(3)

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;

(4)

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();

(5)

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;

}

(6)

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

Cytaty

Powiązane dokumenty

● strumienie wejścia/wyjścia: iostream, fstream, sstream, iomanip. ●

● automatically created if (and only if) there are no explicit constructors, but without any initialisation of the fields (risky when there are any dynamically allocated fields in

● the public method changing the state of an object (modifier) is to check the compliance of such a change with the rules described by invariants. ● if the rules are exceeded,

● destructor, copy constructor and assignment operator (as for bounded) insertion/removal

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer

● during instantiation of a template with a specific type, the compiler replaces each instance of a parametric type with the specified type and compiles the data structure

(1) set the pred pointer on the node preceding the deleted node, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the

(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list