Sunteți pe pagina 1din 47

S1L2 - plan

ConLalnere S1L
deque
llsL
map, mulumap
seL, muluseL
AlgorlLml in S1L
Cpera(ll ce nu modlc secven(a
Cpera(ll ce modlc secven(a
SorLare
CuLare blnar
Merge
Peap
Mln/max
Lxemple
hup://www.cplusplus.com/reference/algorlLhm/
ConLalnerul ueque
coad cu dou capeLe
suporL lnserare/sLergere eclenL la ambele
capeLe
permlLe lnserare/sLergere la orlce pozl(le vla
lLeraLor
SunL lmplemenLaLe meLodele push_front()
sl pop_front() impreun cu cele de la vecLor
(push_back(), pop_back())
Ma[orlLaLea meLodelor de la vecLorl sunL vallde sl
alcl.
ConsLrucLorl asemnLorl cu cel de la vecLor
Lxemplu
int main ()
{
deque<int> first;
deque<int> second;
deque<int> third;
first.assign (7,100); // de 7 ori 100
deque<int>::iterator it;
it=first.begin()+1;
second.assign (it,first.end()-1); // 5 valori din first
int myints[] = {1776,7,4};
third.assign (myints,myints+3); // assign din tablou
cout << "Size of first: " << int (first.size()) << endl; //7
cout << "Size of second: " << int (second.size()) << endl; //5
cout << "Size of third: " << int (third.size()) << endl; //3
return 0;
}
ConLalnerul LlsL
LlsL dublu inln(ulL.
nu esLe permls acces aleaLorlu, lnserarea sl
sLergerea la pozl(la curenL a lLeraLorulul se
face in ump consLanL
SuporL lLeraLorl bldlrec(lonall
nu esLe denlL operaLorul [ ], nlcl meLodele
reserve(), capaclLy()
lnserarea sl sLergerea nu lnvalldeaz lLeraLorll
ca la vecLorl
ConLalnerul LlsL
MeLode speclce llsLelor:
spllce(pos, x) : muL LoaLe elemenLele dln llsLa x
inalnLe de pos in aceeasl llsL
spllce(pos, x, p): muL *p dln llsLa x inalnLe de pos
spllce(pos, x, rsL,lasL)
merge(llsL&) : lnLerclasare llsLe sorLaLe
template<class Cmp> merge(list&, Cmp)
sorL()
template <class Cmp> sort(Cmp)
fronL(): referln( la prlmul elemenL dln llsL
Suva <sLack>
Suva (ca sl coada) esLe denlL ca un conLalner
dlferlL dar ca adapLor al conLalnerelor de baz
secven(lale (vecLor, llsL, deque)
un adapLor al unul conLalner furnlzeaz o lnLerfa(
resLrlcuv la un conLalner
adapLorll nu furnlzeaz lLeraLorl
Suva esLe o lnLerfa( la un conLalner de up
generlc - Lransmls ca argumenL LemplaLe
Suva <sLack>
template<class T,class C = deque<T> > class stack{
public:
typedef C container_type;
typedef typename C::value_type value_type;
typedef typename C::size_type size_type;
typedef typename C::reference reference;
typedef typename C::const_reference const_reference;
stack() : c() {}
explicit stack(const C& _Cont) : c(_Cont){}
bool empty() const{return (c.empty());}
size_type size() const{ return (c.size());}
reference top(){ return (c.back());}
const_reference top() const { return (c.back());}
void push(const value_type& _Val){c.push_back(_Val);}
void pop() {c.pop_back();}
protected:
C c;
};
Suva <sLack>
stack<char> s1;
// se foloseste o coada deque<char> pentru
//a stoca elemente de tip char
stack<int, vector<int>>s2;
// se foloseste un vector<int> pentru
// reprezentarea stivei
Suva, ca sl celelalLe conLalnere adapLaLe, nu are
parameLru LemplaLe de Llp allocaLor. enLru alocarea
spa(lulul se bazeaz pe alocaLorul pe care-l folosesLe
conLalnerul ce lmplemenLeaz suva
Coada <queue>
template<class T,class C = deque<T> > class queue {
//
};
lnLerfa( la un conLalner ce permlLe lnserarea
la sfrslL back() sl exLragerea la incepuL fronL()
esLe foloslL lmpllclL deque penLru a psLra
elemenLele cozll, dar se poaLe folosl orlce
secven( ce furnlzeaz opera(llle fronL(), back
(), push_back(), pop_fronL()
asadar nu vecLor ( nu are pop_fronL())
Coada cu prlorlLaLe
template<class T,class C = vector<T>, class Cmp =
less<Typename C::value_type>> class priority_queue {
//
};
ecare elemenL are aLasaL o prlorlLaLe care conLroleaz
ordlnea in care elemenLele a[ung in capul cozll
declara(la se a in slerul <queue>
in mod lmpllclL se folosesLe < penLru comparare
ca alLernauv se poaLe da in argumenLul LemplaLe alL rela(le
lmpllclL se folosesLe un vecLor penLru sLocarea elemenLelor,
dar orlce secven( ce are fronL(), push_back(), pop_back() sl
lLeraLorl random poaLe foloslL
esLe lmplemenLaL foloslnd un heap
ConLalnere asoclauve <map>
map sau dlc(lonar
perechl de valorl (chele, daL) asuel c, penLru ecare
chele exlsL cel mulL o slngur pereche cu acea chele
lmplemenLa(l cu arborl speclall
template <class Key, class T,class Cmp = less<Key>,
class A = allocator<pair<const Key,T>>>class std::map {
public:
typedef Key key_type;
typedef T maped_type;
typedef pair<const Key, T> value_type;
//. . .
};
un oblecL inLr-un conLalner map esLe o valoare de up
pair<const Key, T>
odaL lnLrodus un oblecL in conLalner se poaLe modlca
doar componenLa a doua, chela esLe consLanL
erechl
template<class T, class U>
struct pair
{
typedef T first_type;
typedef U second_type
T first;
U second;
pair():first(T()), second(U()){};
pair(const T& x, const U& y):first(x), second(y) {};
template<class V, class W>
pair(const pair<V, W>& pr)
:first(pr.first), second(pr.second) {};
};
Accesul la elemenLe
lLeraLorl:
begln(), end(), rbegln(), rend()
varlanLe consL
un lLeraLor la un conLalner map prezlnL elemenLele in
ordlnea crescLoare a chellor
operaLorul []: x_map[key]
se cauL key in x_map
se reLurneaz valoarea corespunzLoare chell gslLe
dac nu exlsL un elemenL cu chela daL, esLe creaL unul care
are ca valoare, valoarea lmpllclL a upulul mapped_Lype
nu exlsL varlanLa consL a operaLorulul []
MeLoda nd() permlLe locallzarea unel chel fr a
modlca conLalnerul
Comparrl
gslrea unul elemenL in map se face
comparnd chel
lmpllclL se face compararea cu < (less in
LemplaLe) dar se poaLe speclca sl alL mod
exlsL 2 func(ll membru care fac poslbll
lnLerogarea unul map penLru modul in care se
face compararea(penLru chel sl valorl):
key_compare key_comp() consL,
value_compare value_comp() consL,
lnserare
Angajat ion(Ion", Popescu", "000-00-0000");
Angajat vasile(Vasile", Manea", "111-11-1111");
Angajat dan(Dan", Barbu", "888-88-8888");
map<const string, Angajat*> S;
S.insert(pair<const string, Angajat*> (ion.getID(), &ion));
S.insert(pair<const string, Angajat*> (dan.getID(), &dan));
S.insert(pair<const string, Angajat*> (vasile.getID(), &vasile));
mapPrint(S, cout);
map<const string, Angajat>::const_iterator It;
It = S.find("111-11-1111");
cout << (*It).second.getName() << endl;
000-00-0000 Ion Popescu
111-11-1111 Vasile Manea
888-88-8888 Dan Barbu
Vasile Manea
<mulumap>
conLalner asoclauv (ca sl map) in care poL
exlsL oblecLe cu aceeasl chele
nu suporL operaLorul [ ]
argumenLul func(lel lnserL() esLe lnseraL
inLoLdeauna
penLru map m.lnserL(val) reLurneaz o valoare
palr<lLeraLor, bool>
penLru mulumap mm.lnserL(val) inLoarce doar
un lLeraLor
Cpera(ll speclallzaLe
iterator find(const key_type& k);
cauL elemenLul cu chela k
size_type count(const key_type& k) const;
numrul elemenLelor cu chela k
iterator lower_bound(const key_type& k);
prlmul elemenL cu chela k
iterator upper_bound(const key_type& k);
prlmul elemenL cu chela mal mare decL k
pair<iterator,iterator> equal_range(const
key_type& k);
incepuLul sl sfrslLul secven(el cu chela k
Lxemplu
int main (){
multimap<char,int> mymm;
multimap<char,int>::iterator it;
pair<multimap<char,int>::iterator,multimap<char
,int>::iterator> ret;
mymm.insert(pair<char,int>('a',10));
mymm.insert(pair<char,int>('b',20));
mymm.insert(pair<char,int>('b',30));
mymm.insert(pair<char,int>('b',40));
mymm.insert(pair<char,int>('c',50));
mymm.insert(pair<char,int>('c',60));
mymm.insert(pair<char,int>('d',60));
cout << "mymm contains:\n";
for (char ch='a'; ch<='d'; ch++)
{
cout << ch << " =>";
ret = mymm.equal_range(ch);
for (it=ret.first; it!=ret.second; ++it)
cout << " " << (*it).second;
cout << endl;
}
return 0;
}
mymm contains:
a => 10
b => 20 30 40
c => 50 60
d => 60
seL, muluseL
seL: un conLalner map in care valorlle nu sunL
relevanLe, se psLreaz doar chelle
elemenLele in seL se psLreaz ordonaLe
sunL denl(l operaLorll ==, !=, <, >, <=, >=, swap()
muluseL: un conLalner mulumap in care
valorlle nu sunL relevanLe, se psLreaz doar
chelle
accesarea chellor se face cu equal_range(),
lower_bound(), upper_bound()
Cpera(ll
lnserL , erase
prln valoare:
S.lnserL(k), S.erase(k),
M. lnserL(k), M.erase(k), (sLerge LoaLe lnLrrlle k)
prln lLeraLorl:
S.lnserL(l), S.erase(l),
M.lnserL(l), M.erase(l), (se sLerge doar *l)
nd
S.nd(k), M.nd(k) ( reLurneaz end() dac nu-l
gslL)
Lxemplu
multiset<int> mymultiset;
multiset<int>::iterator it;
// insert some values:
mymultiset.insert (40); // 40
for (int i=1; i<7; i++) mymultiset.insert(i*10); // 10 20 30 40 40 50 60
it=mymultiset.begin();
it++;
mymultiset.erase (it); // 10 30 40 40 50 60
mymultiset.erase (40); // 10 30 50 60
it=mymultiset.find (50);
mymultiset.erase ( it, mymultiset.end() ); // 10 30
SLandard 1emplaLe Llbrary: AlgorlLhms
Peader-ul <algorlLhm> denesLe o colec(le de func(ll
prolecLaLe penLru a apllcaLe unul domenlu de
elemenLe ale unul conLalner
uomenlul (range) esLe orlce secven( de oblecLe, dlnLr-
un Lablou sau conLalner, ce poL accesaLe vla polnLer
sau lLeraLor
AlgorlLmll lmplemenLa(l de acesLe func(ll opereaz
dlrecL pe valorlle polnLaLe, nu sunL afecLaLe in nlcl un
fel sLrucLura conLalnerulul (dlmenslune, memorla
alocaL) .
Cpera(ll ce nu modlc secven(a
"#$%&'() *++,- "./(0#/ 1# $'/2&
3/4 56/4 7',.& 6/ $'/2&
3/4%6" 56/4 &,&8&/1 6/ $'/2&
3/4%&/4 56/4 ,'91 9.:9&;.&/(& 6/ $'/2&
3/4%3$91%#" 56/4 &,&8&/1 "$#8 9&1 6/ $'/2&
'4<'(&/1%3/4 56/4 &;.', '4<'(&/1 &,&8&/19 6/ $'/2&
(#./1 =#./1 '++&'$'/(&9 #" 7',.& 6/ $'/2&
(#./1%6" >&1.$/ /.8:&$ #" &,&8&/19 6/ $'/2&
9'09"-6/2 (#/460#/
8698'1() >&1.$/ 3$91 +#960#/ ?)&$& 1?# $'/2&9 46@&$
&;.', A&91 ?)&1)&$ 1)& &,&8&/19 6/ 1?# $'/2&9 '$&
&;.',
9&'$() 56/4 9.:9&;.&/(& 6/ $'/2&
9&'$()%/ 56/4 9.((&996#/ #" &;.', 7',.&9 6/ $'/2&
for_each
template <class InputIterator, class Function>
Function for_each (InputIterator first,
InputIterator last, Function f);
ComporLarea esLe echlvalenL cu:
template<class InputIterator, class Function> Function for_each
(InputIterator first, InputIterator last, Function f)
{
for ( ; first!=last; ++first ) f(*first);
return f;
}
Lxemple
void myfunction (int i) {
cout << " " << i;
}
struct myclass {
void operator() (int i) {cout << " " << i;}
} myobject;
int main () {
vector<int> myvector;
myvector.push_back(10);
myvector.push_back(20);
myvector.push_back(30);
cout << "myvector contains:";
for_each (myvector.begin(), myvector.end(), myfunction);
// or:
cout << "\nmyvector contains:";
for_each (myvector.begin(), myvector.end(), myobject);
cout << endl;
return 0;
}
counL_lf
template <class InputIterator, class Predicate>
typename iterator_traits<InputIterator>::difference_type
count_if ( ForwardIterator first, ForwardIterator last,
Predicate pred );
ComporLarea esLe echlvalenL cu:
template <class InputIterator, class Predicate>
ptrdiff_t count_if ( InputIterator first, InputIterator last, Predicate pred )
{
ptrdiff_t ret=0;
while (first != last) if (pred(*first++)) ++ret;
return ret;
}
search_n
template <class ForwardIterator, class Size, class T>
ForwardIterator search_n ( ForwardIterator first,
ForwardIterator last, Size count, const T& value );
template <class ForwardIterator, class Size, class T,
class BinaryPredicate> ForwardIterator search_n
( ForwardIterator first, ForwardIterator last, Size
count, const T& value, BinaryPredicate pred );
Searches Lhe range [rsL,lasL) for a successlon of !"#$%
elemenLs, each of Lhem comparlng equal (or some oLher
predlcaLe) Lo &'(#).
search_n
ComporLarea esLe echlvalenL cu:
template<class ForwardIterator, class Size, class T> ForwardIterator search_n
( ForwardIterator first, ForwardIterator last, Size count, const T& value )
{
ForwardIterator it, limit;
Size i;
limit=first; advance(limit,distance(first,last)-count);
while (first!=limit)
{
it = first; i=0;
while (*it==value) // or: while (pred(*it,value)) for the pred version
{ ++it; if (++i==count) return first; }
++first;
}
return last;
}
Lxemple
vector<int> myvector;
for (int i=1; i<10; i++) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
mycount = (int) count_if (myvector.begin(), myvector.end(), IsOdd);
cout << "myvector contains " << mycount << " odd values.\n";
int myints[]={10,20,30,30,20,10,10,20};
vector<int> myvector (myints,myints+8);
vector<int>::iterator it;
it = search_n (myvector.begin(), myvector.end(), 2, 30); // 2 (== 30)
it = search_n (myvector.begin(), myvector.end(), 2, 10, mypredicate);
Cpera(ll ce modlc secven(a(1)
(#+- =#+- $'/2& #" &,&8&/19
(#+-%:'(B?'$4 =#+- $'/2& #" &,&8&/19 :'(B?'$49
9?'+ CD()'/2& 7',.&9 #" 1?# #:<&(19
9?'+%$'/2&9 CD()'/2& 7',.&9 #" 1?# $'/2&9
61&$%9?'+ CD()'/2& 7',.&9 #" #:<&(19 +#6/1&4 :- 1?#
61&$'1#$9
1$'/9"#$8 *++,- "./(0#/ 1# $'/2&
$&+,'(& >&+,'(& 7',.& 6/ $'/2&
$&+,'(&%6" >&+,'(& 7',.&9 6/ $'/2&
$&+,'(&%(#+- =#+- $'/2& $&+,'(6/2 7',.&
$&+,'(&%(#+-%6" =#+- $'/2& $&+,'(6/2 7',.&
3,, 56,, $'/2& ?61) 7',.&
3,,%/ 56,, 9&;.&/(& ?61) 7',.&
2&/&$'1& E&/&$'1& 7',.&9 "#$ $'/2& ?61) "./(0#/
2&/&$'1&%/ E&/&$'1& 7',.&9 "#$ 9&;.&/(& ?61) "./(0#/
Lxemple
int myints[]={10,20,30,40,50,60,70};
vector<int> myvector;
vector<int>::iterator it;
myvector.resize(7);
copy ( myints, myints+7, myvector.begin() );
int RandomNumber () { return (rand()%100); }
struct c_unique {
int current; c_unique() {current=0;}
int operator()() {return ++current;}
} UniqueNumber
vector<int> myvector (8);
vector<int>::iterator it;
generate (myvector.begin(), myvector.end(), RandomNumber);
// functie generator
cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
cout << " " << *it;
generate (myvector.begin(), myvector.end(), UniqueNumber);
// obiect dintr-o clasa cu operator()
Lxemple
int myints[]={10,20,30,40,50 }; // myints: 10 20 30 40 50
vector<int> myvector (4,99); // myvector: 99 99 99 99
iter_swap(myints,myvector.begin()); // myints: [99] 20 30 40 50
// myvector: [10] 99 99 99
iter_swap(myints+3,myvector.begin()+2); // myints: 99 20 30 [99]
// myvector: 10 99 [40] 99
vector<int> myvector (8); // myvector: 0 0 0 0 0 0 0 0
fill (myvector.begin(),myvector.begin()+4,5);// myvector: 5 5 5 5 0 0 0 0
fill (myvector.begin()+3,myvector.end()-2,8);// myvector: 5 5 5 8 8 8 0 0
Cpera(ll ce modlc secven(a(2)
$&8#7& >&8#7& 7',.& "$#8 $'/2&
$&8#7&%6" >&8#7& &,&8&/19 "$#8 $'/2&
$&8#7&%(#+- =#+- $'/2& $&8#76/2 7',.&
$&8#7&%(#+-%6" =#+- $'/2& $&8#76/2 7',.&9
./6;.& >&8#7& (#/9&(.07& 4.+,6('1&9 6/ $'/2&
./6;.&%(#+- =#+- $'/2& $&8#76/2 4.+,6('1&9
$&7&$9& >&7&$9& $'/2&
$&7&$9&%(#+- =#+- $'/2& $&7&$9&4
$#1'1& >#1'1& &,&8&/19 6/ $'/2&
$#1'1&%(#+- =#+- $#1'1&4 $'/2&
$'/4#8%9).F& >&'$$'/2& &,&8&/19 6/ $'/2& $'/4#8,-
+'$00#/ G'$00#/ $'/2& 6/ 1?#
91':,&%+'$00#/ G'$00#/ $'/2& 6/ 1?# H 91':,& #$4&$6/2
Lxemple
template <class ForwardIterator> void rotate ( ForwardIterator first,
ForwardIterator middle, ForwardIterator last );
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
rotate(myvector.begin(),myvector.begin()+3,myvector.end());
// 4 5 6 7 8 9 1 2 3
vector<int> myvector;
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
reverse(myvector.begin(),myvector.end()); // 9 8 7 6 5 4 3 2 1
for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
bound = partition (myvector.begin(), myvector.end(), IsOdd);
// se rearanjeaza elementele vectorului
// bound = adresa de inceput a partii a doua
// 1 9 3 7 5 6 4 8 2
SorLare
sorL SorL elemenLs ln range
sLable_sorL SorL elemenLs preservlng order of
equlvalenLs
parual_sorL arually SorL elemenLs ln range
parual_sorL_copy Copy and parually sorL
range
nLh_elemenL SorL elemenL ln range
Lxemple
template <class RandomAccessIterator>
void sort ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
vector<int>::iterator it;
// using default comparison (operator <):
sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
// using function as comp
sort (myvector.begin()+4, myvector.end(), myfunction);
// 12 32 45 71(26 33 53 80)
// using object as comp
sort (myvector.begin(), myvector.end(), myobject);
//(12 26 32 33 45 53 71 80)
Lxemple
double mydoubles[] = {3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58};
vector<double> myvector;
vector<double>::iterator it;
bool compare_as_ints (double i,double j)
{
return (int(i)<int(j));
}
myvector.assign(mydoubles,mydoubles+8);
stable_sort (myvector.begin(), myvector.end());
// 1.32 1.41 1.62 1.73 2.58 2.72 3.14 4.67
myvector.assign(mydoubles,mydoubles+8);
stable_sort (myvector.begin(), myvector.end(), compare_as_ints);
// elementele cu aceeasi valoare raman in aceeasi ordine
// 1.41 1.73 1.32 1.62 2.72 2.58 3.14 4.67
Lxemple
int myints[] = {9,8,7,6,5,4,3,2,1};
vector<int> myvector (myints, myints+9);
vector<int>::iterator it;
// using default comparison (operator <):
partial_sort (myvector.begin(), myvector.begin()+5, myvector.end());
// using function as comp
partial_sort (myvector.begin(), myvector.begin()+5, myvector.end
(),myfunction);
//1 2 3 4 5 9 8 7 6
vector<int> myvector(15);
generate (myvector.begin(), myvector.end(), RandomNumber);
//41 67 34 0 69 24 78 58 62 64 5 45 81 27 61
partial_sort (myvector.begin(), myvector.begin()+5, myvector.end());
//0 5 24 27 34 69 78 67 62 64 58 45 81 41 61
CuLare blnar (pe secven(e sorLaLe):
,#?&$%:#./4 8eLurn lLeraLor Lo lower bound
.++&$%:#./4 8eLurn lLeraLor Lo upper bound
&;.',%$'/2& CeL subrange of equal elemenLs
:6/'$-%9&'$() 1esL lf value exlsLs ln sorLed array
Lxemple
int myints[] = {10,20,30,30,20,10,10,20};
vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20
vector<int>::iterator low,up;
sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30
low=lower_bound (v.begin(), v.end(), 20); // 3
up= upper_bound (v.begin(), v.end(), 20); // 6
bounds=equal_range (v.begin(), v.end(), 20); // 3 6
template <class ForwardIterator, class T>
bool binary_search ( ForwardIterator first, ForwardIterator last,
const T& value );
template <class ForwardIterator, class T, class Compare>
bool binary_search ( ForwardIterator first, ForwardIterator last,
const T& value, Compare comp );
if (binary_search (v.begin(), v.end(), 3)) //
if (binary_search (v.begin(), v.end(), 6, myfunction)) //
Merge (pe secven(e sorLaLe)
8&$2& Merge sorLed ranges
6/+,'(&%8&$2& Merge consecuuve sorLed ranges
6/(,.4&9 1esL wheLher sorLed range
lncludes anoLher sorLed range
9&1%./6#/ unlon of Lwo sorLed ranges
9&1%6/1&$9&(0#/ lnLersecuon of Lwo sorLed
ranges
9&1%46@&$&/(& ulerence of Lwo sorLed ranges
9&1%9-88&1$6(%46@&$&/(& SymmeLrlc dlerence
of Lwo sorLed ranges
Lxemple
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result );
template <class InputIterator1, class InputIterator2,
class OutputIterator, class Compare>
OutputIterator merge ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp );
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
vector<int> v(10);
sort (first,first+5); // 5,10,15,20,25
sort (second,second+5); // 10,20,30,40,50
merge (first,first+5,second,second+5,v.begin());
//5 10 10 15 20 20 25 30 40 50
Peap
+.9)%)&'+ ush elemenL lnLo heap range
+#+%)&'+ op elemenL from heap range
8'B&%)&'+ Make heap from range
9#$1%)&'+ SorL elemenLs of heap
Lxemple
template <class RandomAccessIterator>
void make_heap ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void make_heap ( RandomAccessIterator first, RandomAccessIterator last,
Compare comp );
int myints[] = {10,20,30,5,15};
vector<int> v(myints,myints+5);
make_heap (v.begin(),v.end());
cout << "initial max heap : " << v.front() << endl; // 30
pop_heap (v.begin(),v.end()); v.pop_back();
cout << "max heap after pop : " << v.front() << endl; // 20
v.push_back(99); push_heap (v.begin(),v.end());
cout << "max heap after push: " << v.front() << endl; // 99
sort_heap (v.begin(),v.end());
cout << "final sorted range :";
for (unsigned i=0; i<v.size(); i++) cout << " " << v[i]; // 5 10 15 20 99
Mln/max
86/ 8eLurn Lhe lesser of Lwo argumenLs
8'D 8eLurn Lhe greaLer of Lwo argumenLs
86/%&,&8&/1 8eLurn smallesL elemenL ln range
8'D%&,&8&/1 8eLurn largesL elemenL ln range
,&D6(#2$'+)6(',%(#8+'$& Lexlcographlcal less-
Lhan comparlson
/&D1%+&$8.1'0#/ 1ransform range Lo
nexL permuLauon
+$&7%+&$8.1'0#/ 1ransform range Lo
prevlous permuLauon
Lxemple
template <class ForwardIterator>
ForwardIterator min_element ( ForwardIterator first, ForwardIterator last );
template <class ForwardIterator, class Compare>
ForwardIterator min_element ( ForwardIterator first, ForwardIterator last,
Compare comp );
int myints[] = {3,7,2,5,6,4,9};
// using default comparison:
cout << *min_element(myints,myints+7) << endl; // 2
cout << *max_element(myints,myints+7) << endl; // 9
char first[]="Apple"; // 5 letters
char second[]="apartment"; // 9 letters
cout << "Using default comparison (operator<): ";
if (lexicographical_compare(first,first+5,second,second+9))
cout << first << " is less than " << second << endl;
else
if (lexicographical_compare(second,second+9,first,first+5))
cout << first << " is greater than " << second << endl;
else
cout << first << " and " << second << " are equivalent\n";
Lxemple
template <class BidirectionalIterator>
bool next_permutation (BidirectionalIterator first,
BidirectionalIterator last );
template <class BidirectionalIterator, class Compare>
bool next_permutation (BidirectionalIterator first,
BidirectionalIterator last, Compare comp);
int myints[] = {1,2,3};
cout << "The 3! possible permutations with 3 elements:\n;
sort (myints,myints+3);
do {
cout << myints[0] << " " << myints[1] << " " << myints[2] << endl;
} while ( next_permutation (myints,myints+3) );

S-ar putea să vă placă și