Вы находитесь на странице: 1из 12

5

. .
.
(STL) C++.

.
STL.
STL,
STL.

.
(STL).
STL , ,

.
STL ,
.
STL.
: ,
.
(containers) ,
. , , , .
(associative containers)
.
- .
, , .
(algorithms) .
, , ,
.
(sequence), .
(iterators) ,
.
, .
, .
*, , .
iterator, .
:
1. (input_iterator) ,
.
==, !=, *i, ++i, i++, *i++
istream_iterator.
2

2. (output_iterator) ,
, .
++i, i++, *i=t, *i++=t
ostream_iterator.
3. (forward_iterator)
/ , ,
.
==, !=, =, *i, ++i, i++, *i++
4. (biderectional_iterator)
forward-,
(--i, i--, *i--), .
5. (random_access_iterator)
biderectional-,
, .
i+=n, i+n, i-=n, i-n, i1-i2, i[n], i1<i2, i1<=i2, i1>i2, i1>=i2

STL (reverse iterators).


,
, .
, STL
.
, .

(allocator), .
allocator.
.
,
. .
: .
. UnPred, BinPred.
.

. (comparison function).
, .
Comp.
STL -.
- ,
(). -.
- ,
operator ().
1.
class less{
public:
bool operator()(int x,int y)
{return x<y;}
};
3

3. -.
STL :
.
,
, .
,
. ,
, map ( ).
, , ,
list.
, queue,
deque, stack.
vector; ,
.
, ,

. STL
.
.
STL - (
, ):

bitset <bitset.h>
vector <vector.h>
list <list.h>
deque <deque.h>
stack <stack.h>
queue <queue.h>
priority_queue <queue.h>
map /
, <map.h>
multimap <map.h>
set <set.h>
multiset ,
<set.h>



value_type
allocator_type
size_type , ..
4

iterator value_type*
reverse_iterator
reference value_type&
key_type ( )
key_compare ( )
mapped_type

begin()
end() ,
rbegin()
rend() ,


front()
back()
operator[](i)
at(i)


insert(p,x) ,
insert(p,n,x) n
insert(p,first,last) [first:last]
push_back(x)
push_front(x) (
)

pop_back()
pop_front() (
)
erase(p)
erase(first,last) [first:last]
clear()

size()
empty() ?
capacity() , ( )
reserve(n) n
resize(n) ( ,
)
swap(x)
==, !=, <

operator=(x)
assign(n,x) n (
)
assign(first,last) [first:last]
5


operator[](k) k
find(k) k
lower_bound(k) k
upper_bound(k) , k
equal_range(k) lower_bound ( ) upper_bound (
) k

vector-.
vector STL
.
template<class T,class Allocator=allocator<T>>class std::vector{};
T .

Allocator , .
vector :
explicit vector(const Allocator& a=Allocator());
explicit vector(size_type , const T&= T(), const Allocator&a= =Allocator());
vector(const vector<T,Allocator>&);
template<class InIter>vector(InIter , InIter , const Allocator&a= =Allocator());
.
,
.
.
.
, ,
.
2.
vector<int> a;
vector<double> x(5);
vector<char> c(5,*);
vector<int> b(a); //b=a
, ,
. ,
< ==.
:
==, <, <=, !=, >, >=.
, vector [].

insert(), push_back(), resize(), assign().

erase(), pop_back(), resize(), clear().

begin(), end(), rbegin(), rend(),
, ,
<algorithm.h>.
3.
#include<iostream.h>
6

#include<vector.h>
using namespace std;
void main()
{vector<int> v;
int i;
for(i=0;i<10;i++)v.push_back(i);
cout<<size=<<v.size()<<\n;
for(i=0;i<10;i++)cout<<v[i]<< ;
cout<<endl;
for(i=0;i<10;i++)v[i]=v[i]+v[i];
for(i=0;i<v.size();i++)cout<<v[i]<< ;
cout<<endl;
}
4. .
#include<iostream.h>
#include<vector.h>
using namespace std;
void main()
{vector<int> v;
int i;
for(i=0;i<10;i++)v.push_back(i);
cout<<size=<<v.size()<<\n;
vector<int>::iterator p=v.begin();
while(p!=v.end())
{cout<<*p<< ;p++;}
}
5. .
#include<iostream.h>
#include<vector.h>
using namespace std;
void main()
{vector<int> v(5,1);
int i;
//
for(i=0;i<5;i++)cout<<v[i]<< ;
cout<<endl;
vector<int>::iterator p=v.begin();
p+=2;
// 10 9
v.insert(p,10,9);
//
p=v.begin();
while(p!=v.end())
{cout<<*p<< ;p++;}
//
p=v.begin();
p+=2;
v.erase(p,p+10);
7

//
p=v.begin();
while(p!=v.end())
{cout<<*p<< ;p++;}
}
6. .
#include<iostream.h>
#include<vector.h>
#includestudent.h
using namespace std;
void main()
{vector<STUDENT> v(3);
int i;
v[0]=STUDENT(,45.9);
v[1]=STUDENT(,30.4);
v[0]=STUDENT(,55.6);
//
for(i=0;i<3;i++)cout<<v[i]<< ;
cout<<endl;
}

().
. ,
(key), ,
(mapped value).
,
:
V& operator[](const K&) V, K.
.
map (, ),
. map
.
map ,
<. ,
.
map:
template<class Key,classT,class Comp=less<Key>,class Allocator=allocator<pair> >
class std::map
map :
explicit map(const Comp& c=Comp(),const Allocator& a=Allocator());
map(const map<Key,T,Comp,Allocator>& ob);
template<class InIter> map(InIter first,InIter last,const Comp& c=Comp(),const
Allocator& a=Allocator());

, ,
, .
:
map& operator=(const map&);
8

: ==, <, <=, !=, >, >=.


map / pair.
/
pair, make_pair, pair,
.

([]).
mapped_type& operator[](const key_type& K);

set ,
, .
template<classT,class Cmp=less<T>,class Allocator=allocator<T>>class std::set{};
, , , T
(<). ,
.

.

. , ,
. ,
, ,
.
. , ,
. STL
, , , ,
.
<algorithm.h>.
-
STL.
I. .
for_earch()
find()
find_if()
count()
count_if()
search()
search_n() n-

II. .
copy() ,
swap()
replace()
replace_if()
replace_copy() ,

replace_copy_if() ,

9

fill()
remove()
remove_if()
remove_copy() ,

remove_copy_if() ,

reverse()
random_shuffle()
( )

transform()

unique()
unique_copy() ,

III. .
sort() -
partial_sort()
stable_sort() , -

lower_bound()

upper_bound() ,
binary_search() ,

merge()

IV. .
includes()
set_union()
set_intersection()
set_difference()

V. .
min()
max()
min_element()
max_element()

VII. .
next_permutation()
pred_permutation()

.
.
.
10


.
STL.
1 :
1. -
, .
2. .
3. , .
4. , .
5.
, .
6. , n
.
7. .
2 ,
.
3 :
1. , .
.
2. .
3. .
4. , ,
.
5. ,
( ) .
.
6. .
7. .
8. .
9. .
10. .
11 ., , ,
.
12., ,
.

.
1. EasyWin- Borland C++5.02.
3 ( ).
2.
7.
3. 2 ,
7.
4. 2
, .
5. 3
remove_copy_if, copy_if, STL.
11

6. find_if,
for_each, binary_search, .
7.
<, .
comp
sort.
8.
-.
9. -
>> <<.
10.
. , sort ,
.
. ,
: , ,
.
10.
( ). ,

.

.
1. .
2. .
3. .
4.
, .
5. .
6. STL.
7. , .

. .


/
1 vector list int
2 list deque long
3 deque stack float
4 stack queue double
5 queue vector char
6 vector stack string
7 map list long
8 multimap deque float
9 set stack int
10 multiset queue char
11 vector map double
12 list set int
12
13 deque long
multiset
14 stack vector float
15 queue map int
16 priority_queue stack char
17 map queue char
18 multimap list int
19 set map char
20 multiset vector int



1. . -
++. . .: , 1998.
2. ., . Visual C++6: . : BHV,
2000.
3. .. ++ .: , 1996.
4. . ++. , .: ,
1999.


1. . STL ++. ., , 1999.
2. . Visual C++6. ., , 1999.
3. . Borland C++5. .: BHV, 1997.
4. . Borland C++: .: , 1997.
5. . Visual C++6. , , 1999.
6. . ++. . : , 1997.
7. . - ++.
. .: , 1999.
8. .. Visual C++6. , BHV, 1999.
9. .. ++ . : , 1997.
10. . - ++ 4.5. :
, 1996.
11. . ++. . .: BHV, 1998.
12. . ++. .: BHV, 1996.
13. . ++: : , 1999.

Вам также может понравиться