You are on page 1of 21

Th vin STL (Standard Template Library)

EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

Khi nim

STL l th vin chun ca C++, c xy dng sn Ci t cc cu trc d liu v thut ton thng dng Bao gm cc lp v hm khun mu, cho php lm vic vi d liu tng qut Nm trong mt namespace c tn std Cc phn chnh:

Cc lp d liu c bn: string, complex Xut nhp (IO) Cc lp cha (containers): list, vector, deque, stack, map, set, Duyt phn t ca cc lp cha (iterators) Mt s thut ton thng dng: tm kim, so snh, sp xp, Qun l b nh, con tr X l ngoi l (exception handling)
EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

X l chui

#include <string>

Lp string cho chui ASCII v wstring cho Unicode Cc thao tc c bn: +, += (ni chui); ==, !=, <, >, >=, <= (so snh); << (xut), >> (nhp)
i s sang chui: to_string(), to_wstring()

i chui sang s: stod(), stof(), stoi(), stold(), stoll(), stoul(), stoull()


i sang chui ca C: const char* string::c_str() const V d: string s1, s2("test"); cin >> s1; for (int i=1; i<=9; i++) s2 += to_string(i); cout << (s2==s1 ? "same":"different");

char s3[100]; strcpy(s3, s1.c_str());


3
EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

Cc lp cha (Containers)

EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

Mng: vector

L mng ng C th cha d liu kiu bt k (template): vector<type> #include <vector>

V d s dng:

int p[] = {4, 2, 6}; vector<int> a(p, p+3); a.push_back(1); a.insert(a.begin() + 2, 3); a.insert(a.end() - 1, 5); a[3] = 10; //

// khi // thm // thm // thm phn t

to t mng C vo cui v tr 2 v tr 1 t cui th 4

vector<int>::iterator i; // duyt xui for (i = a.begin(); i != a.end(); i++) *i += 5; vector<int>::reverse_iterator j; // duyt ngc for (j = a.rbegin(); j != a.rend(); j++) cout << *j << ' ';
5
EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

iterator

Cc lp cha ca STL (vector, list,) c nh ngha kiu iterator tng ng duyt cc phn t (theo th t xui)

Mi iterator cha v tr ca mt phn t Cc hm begin() v end() tr v mt iterator tng ng vi cc v tr u v cui Cc ton t vi iterator:
i++ i-*i phn t k tip phn t lin trc gi tr ca phn t

Tng t, c reverse_iterator duyt theo th t ngc

Cc hm rbegin() v rend()
EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

Danh sch lin kt: list


C th cha d liu kiu bt k (template): list<type> #include <list>

Duyt danh sch dng iterator tng t nh vi vector V d s dng:

double p[] = {1.2, 0.7, list<double> l(p, p+5); l.push_back(3.4); l.pop_front();

2.2, 3.21, 6.4}; // khi to t mng C // thm vo cui // xo phn t u

list<double>::iterator i = l.begin(); // phn t u *i = 4.122; // gn gi tr i++; // phn t k tip l.insert(i, 5.0); // chn phn t l.erase(i); // xo phn t l.sort(); // sp xp (tng dn) for (i = l.begin(); i != l.end(); i++) // duyt xui cout << *i << ' ';
7
EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

Thut ton: tm kim

Phn t ln nht, b nht:

vector<float>::iterator p = max_element(a.begin()+2, a.end()-3); list<string>::iterator p = min_element(l.begin(), l.end()); Da trn cc ton t so snh cn nh ngha nu cha c

Tm ng gi tr:

list<float>::iterator p = find(p1, p2, 2.5f);


bool isOdd(int i) { return i%2 == 1; } list<int>::iterator p = find_if(p1, p2, isOdd);

Tm theo tiu chun: cn nh ngha mt hm nh gi

Tm kim v thay th, xo:

replace_if(p1, p2, isOdd, 10); remove_if(p1, p2, isOdd);


EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

Thut ton: sp xp

Sp xp mng:

Dng ton t so snh:

sort(a.begin(), a.end());

Phi nh ngha ton t < cho kiu d liu c cha


bool compare(const table& a, const table& b) { return a.c1 < b.c1 || (a.c1 == b.c1 && a.c2 < b.c2); } sort(a.begin(), a.end(), compare); l.sort(); l.sort(compare);
EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

Dng hm so snh t nh ngha:

Sp xp danh sch:

Xut/nhp (Input/Output)

10

EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

Tng quan

Trong STL, vic xut nhp c thng qua cc lung thng tin (data streams) #include <iostream>

Lung xut (output streams): xut d liu


Ton t << Lp c s: basic_istream<type>

Lung nhp (input streams): nhp d liu


Ton t >> Lp c s: basic_ostream<type> Lp c s: basic_iostream<type>

Lung xut nhp (input/output streams): c xut v nhp

Mi kiu i tng mun lm vic c vi cc lp trn cn phi c nh ngha ton t >> v <<

11

EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

S cc lp xut/nhp
ios_base basic_streambuf<> streambuf/wstreambuf

basic_ios<> ios/wios

basic_filebuf<> filebuf/wfilebuf

basic_stringbuf<> stringbuf/ wstringbuf

basic_istream<> istream/wistream

basic_ostream<> ostream/wostream

cin

basic_iostream<> iostream/woistream

cout, cerr, clog/ wcout, wcerr, wclog

basic_ifstream<> ifstream/wifstream

basic_fstream<> fstream/wfstream

basic_ofstream<> ofstream/wofstream

vo/ra file

basic_istringstream<> istringstream/ wistringstream

basic_stringstream<> stringstream/ wstringstream

basic_ostringstream<> ostringstream/ wostringstream

vo/ra chui

12

EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

Vo/ra chun

Cc i tng

cin thuc kiu istream, tng ng vi stdin cout thuc kiu ostream, tng ng vi stdout cerr thuc kiu ostream, tng ng vi stderr Cc i tng wcin, wcout, wcerr lm vic vi Unicode
int i; float a[10]; cout << "Nhap i va a[i]: "; if (cin >> i >> a[i]) { cout << "a[" << i << "] = " << a[i] << endl; cout.flush(); } else cerr << "Nhap du lieu loi" << endl; string s; getline(cin, s)
EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

V d:

c mt dng:

Ch trnh s dng ln ln vi cc hm ca C
13

nh dng d liu xut

Cc hm thay i nh dng:

setf(fmtflags flag): thay i cc c nh dng

dec/hex/oct: s nguyn h c s 10/16/8 fixed/scientific: s thc dng thp phn hoc k php khoa hc internal/left/right: cn l

width(int w): thay i rng ca trng precision(int p): thay i chnh xc

V d:
cout.width(10); cout.setf(ios::right | ios::fixed); cout.precision(3); cout << 34.5678;

Dng cc manipulator:

Bao gm: internal, left, right, dec, hex, oct, fixed, scientific, setprecision(p), setw(w), setiosflag(flags), endl, ends, flush #include <iomanip> V d:
cout << setw(10) << right << fixed << setprecision(3) << 34.5678;
EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

14

c/ghi file

#include <fstream>

S dng ifstream (file ch c), ofstream (ch ghi), fstream (c/ghi) c/ghi d liu dng cc ton t >> v << tng t nh vi vo/ra chun M file:

ifstream f1("ten file", ios::in | ios::binary); ofstream f2; f2.open("ten file", ios::out | ios::trunc);

Cc mode:
app ate
in

Lun nhy con tr ti cui file khi ghi Con tr ti cui file
Cho php c

trunc binary
out

Xo ni dung c khi m File nh phn


Cho php ghi

ng file:

f.close();
C th ng file t ng trong destructor khi cc i tng b hu

Ch khi dng fstream dng c c v ghi: trc khi chuyn t vic c sang ghi hoc ngc li, phi dng hm seekg/seekp(...)
15
EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

c/ghi file dng nh phn


M file: thm c ios::binary c d liu:

file.read(char* buffer, int size) file.gcount() // s byte c c


file.write(char* buffer, int size)

Ghi d liu:

Kim tra li c/ghi:

file.read/write(...) if (!file) {...}


if (!file.read/write(...)) {...}

Di chuyn con tr file: C++ phn bit con tr c v con tr ghi

Di chuyn con tr c file: file.seekg(int pos, ios::beg/cur/end) V tr con tr c hin ti: file.tellg()
Di chuyn con tr ghi file: file.seekp(int pos, ios::beg/cur/end)

V tr con tr ghi hin ti: file.tellp()


EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

16

c/ghi file: v d copy file

bool copy_file(const char* src, const char* dst) { ifstream fs(src, ios::in | ios::binary);

ofstream fd(dst, ios::out | ios::binary | ios::trunc); if (!fs || !fd) return false;


char buf[1024]; while (fs) { fs.read(buf, sizeof(buf))) fd.write(buf, fs.gcount());

} return true; }
17
EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

Vo/ra vi chui

#include <sstream> S dng istringstream (file ch c), ostringstream (ch ghi), stringstream (c/ghi)

c t chui:

string s("10 3.56 y"); istringstream str(s);

int i; double d; char c; str >> i >> d >> c;

Dng trch d liu t chui

Ghi ra chui:

ostringstream str; str << "i=" << i << ", d=" << d << ", c=" << c;
string s = str.str(); Dng nh dng d liu ra chui
EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

18

nh ngha ton t >> v <<

Vic xut/nhp d liu da trn nh ngha chng cc ton t >> v <<

ostream& ostream& ostream& ... istream& istream& istream& ...

operator <<(ostream& os, char c); operator <<(ostream& os, const char* s); operator <<(ostream& os, double n);
operator >>(istream& is, char& c); operator >>(istream& is, const char* s); operator >>(istream& is, double& n);

Cc ton t ny tr v chnh i tng ostream/istream nhn tham s c th mc ni nhiu ln:

is >> a >> b >> c; os << a << b << c; ostream& operator <<(ostream& os, const Ellipse& e) { return os << e.rx << e.ry; } istream& operator >>(istream& is, Ellipse& e) { return is >> e.rx >> e.ry; }

Cn nh ngha cc ton t ny cho cc lp mi nh ngha:

19

EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

Khi nim v serialize


L vic chuyn i mt i tng bt k thnh mt lung thng tin c th t c th ghi ra ri c li ng dng trong vic truyn tin v lu tr d liu Vi STL, ta c th nh ngha cc ton t >> v << thc hin serialize V d:

istream& return } ostream& return }

operator >>(istream& is, SinhVien& sv) { is >> sv.ten >> sv.khoa >> sv.nam_sinh;
operator <<(ostream& os, SinhVien& sv) { os << sv.ten << sv.khoa << sv.nam_sinh;

20

EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

Bi tp
1.

2.

Vit chng trnh nhp mng s nguyn c s phn t bt k t bn phm ri in ra cc s chn bng cch duyt mng Sa bi trn thay duyt mng bng dng hm find_if(...) in ra cc s chn Sa li chng trnh trn dng DSLK thay cho mng Cho hai mng a1 v a2 u c gi tr tng dn, vit hm trn hai mng ny thnh mng a3 cng c gi tr tng dn c d liu t file v lu di dng danh sch cc dng, sau in ra cc dng c di t 10 n 20 k t

3. 4.

5.

6.

7.

nh ngha ton t << v >> cho lp Fraction v th dng n xut/nhp d liu vi cin/cout, file, chui nh ngha cc ton t << v >> cho lp Complex c/ghi d liu di dng nh phn
EE3490: K thut lp trnh HK1 2011/2012 o Trung Kin H Bch khoa H Ni

21

You might also like