You are on page 1of 22

<https://vi.scribd.

com/>
Ti ln <https://vi.scribd.com/upload-document>Sign in
<https://vi.scribd.com/login>Join <https://vi.scribd.com/login>
Books<https://vi.scribd.com/books>Audiobooks<https://vi.scribd.com/audiobooks>
Scribd Selects
Hand-picked favorites from our editors
Scribd Selects Audio
Hand-picked favorites
from our editors
<https://vi.scribd.com/books/scribd-selects>
Top Books
What's trending, bestsellers, award-winners & more
Top Audiobooks
What's trending, bestsellers, award-winners & more
<https://vi.scribd.com/books/top>
Book Categories
Arts & Ideas <https://vi.scribd.com/books/Arts-Ideas>
Biography & Memoir <https://vi.scribd.com/books/Biography-Autobiography>
Business & Leadership <https://vi.scribd.com/books/Business-Leadership>
Children's <https://vi.scribd.com/books/Children-s>
Comics <https://vi.scribd.com/books/Comics>
Computers & Technology <https://vi.scribd.com/books/Computers-Technology>
Cooking & Food <https://vi.scribd.com/books/Cooking-Food>
Crafts & Hobbies <https://vi.scribd.com/books/Crafts-Hobbies>
Fantasy <https://vi.scribd.com/books/Fantasy>
Fiction & Literature <https://vi.scribd.com/books/Fiction-Literature>
Happiness & Self-Help <https://vi.scribd.com/books/Happiness-Self-Help>
Health & Wellness <https://vi.scribd.com/books/Health-Wellness>
History <https://vi.scribd.com/books/History>
Home & Garden <https://vi.scribd.com/books/Home-Garden>
Humor <https://vi.scribd.com/books/Humor>
LGBT <https://vi.scribd.com/books/LGBT>
Mystery, Thriller & Crime
<https://vi.scribd.com/books/Mystery-Thriller-Crime>
Politics & Economy <https://vi.scribd.com/books/Politics-Economy>
Reference <https://vi.scribd.com/books/Reference>
Religion <https://vi.scribd.com/books/Religion>
Romance <https://vi.scribd.com/books/Romance>
Science & Nature <https://vi.scribd.com/books/Science-Nature>
Science Fiction <https://vi.scribd.com/books/Science-Fiction>
Society & Culture <https://vi.scribd.com/books/Society-Culture>
Sports & Adventure <https://vi.scribd.com/books/Sports-Adventure>
Travel <https://vi.scribd.com/books/Travel>
Young Adult <https://vi.scribd.com/books/Young-Adult>
Audiobook Categories
Arts & Ideas <https://vi.scribd.com/audiobooks/Arts-Ideas>
Biography & Memoir
<https://vi.scribd.com/audiobooks/Biography-Autobiography>
Business & Leadership <https://vi.scribd.com/audiobooks/Business-Leadership>
Children's <https://vi.scribd.com/audiobooks/Children-s>
Computers & Technology

<https://vi.scribd.com/audiobooks/Computers-Technology>
Cooking & Food <https://vi.scribd.com/audiobooks/Cooking-Food>
Fantasy <https://vi.scribd.com/audiobooks/Fantasy>
Fiction & Literature <https://vi.scribd.com/audiobooks/Fiction-Literature>
Happiness & Self-Help <https://vi.scribd.com/audiobooks/Happiness-Self-Help>
Health & Wellness <https://vi.scribd.com/audiobooks/Health-Wellness>
History <https://vi.scribd.com/audiobooks/History>
Home & Garden <https://vi.scribd.com/audiobooks/Home-Garden>
Humor <https://vi.scribd.com/audiobooks/Humor>
LGBT <https://vi.scribd.com/audiobooks/LGBT>
Mystery, Thriller & Crime
<https://vi.scribd.com/audiobooks/Mystery-Thriller-Crime>
Politics & Economy <https://vi.scribd.com/audiobooks/Politics-Economy>
Reference <https://vi.scribd.com/audiobooks/Reference>
Religion <https://vi.scribd.com/audiobooks/Religion>
Romance <https://vi.scribd.com/audiobooks/Romance>
Science & Nature <https://vi.scribd.com/audiobooks/Science-Nature>
Science Fiction <https://vi.scribd.com/audiobooks/Science-Fiction>
Society & Culture <https://vi.scribd.com/audiobooks/Society-Culture>
Sports & Adventure <https://vi.scribd.com/audiobooks/Sports-Adventure>
Travel <https://vi.scribd.com/audiobooks/Travel>
Young Adult <https://vi.scribd.com/audiobooks/Young-Adult>
Book Categories
Welcome to Scribd. Sign in or start your free trial to enjoy unlimited
e-books, audiobooks & documents.Find out more <https://vi.scribd.com/>
Ti v <#>
Standard view
Full view
of 16
Add to collection <#>
Mark as finished <#>
Marked as finished. Undo <#>
Remove from library <#>
Look up keyword
Like this
Share on social networks
1Hot ng

Share to your social networks.


Tweet <https://twitter.com/share>
Gn vo
0of .
Results for:
No results containing your search query
P. 1
Stl Vector
Stl Vector
Ratings:
(0)|Views: 111|Likes: 0
c xut bn biNgo Anh Tuan <https://www.scribd.com/ngo_tuan_19>

More info:
Published by: Ngo Anh Tuan <https://www.scribd.com/ngo_tuan_19> on Apr
08, 2011
Bn quyn:Attribution Non-commercial
Availability:
Read on Scribd mobile: iPhone
<https://itunes.apple.com/us/app/scribd-worlds-largest-online/id542557212?mt=8&u
o=4&at=11lGEE>,
iPad
<https://itunes.apple.com/us/app/scribd-worlds-largest-online/id542557212?mt=8&u
o=4&at=11lGEE>
and Android
<https://play.google.com/store/apps/details?id=com.scribd.app.reader0&hl=en>.
download as PDF, TXT or read online from Scribd
Flag for inappropriate content|Thm vo b su tp <#>
See more
See less
https://vi.scribd.com/doc/52580991/Stl-Vector
04/08/2011
pdf
text
original
STL Vector | 1
S

NG STL VECTOR
I.Gi
i thiu :
L
p mng ng
vector<T>
c s
n trong th vin chun STL ca C++ cho php nh ngha mt mng ng
cc phn
t
kiu T,
vector c cc tnh ch
t sau
:- Khng c
n phi khai bo kch thc ca mng
vector c th
t ng
c
p pht
b

nh
, b

n s

khng ph

i quan tm

n qu

n l
kch th
c c

a n.- Vector cn c th
cho bn bit s lng cc phn t m
b
n ang lu trong n.
Vector c cc phng thc
c
a
stack.- H
tr tt c cc thao tc c bn nh chn ,xa, sao chp
..
II.V sao dng vector :
Ki

u vector c th

coi l ki

u m

ng trong l

p trnh C truy

n th

ng. M

ng l t

p h

p cc gi tr

cng ki
u,
c s

px

p n

i ti

p nhau. Cc ph


n t

a m

ng c th

c truy c

p ng

u nhin qua ch

.V
n

t ra: N

u vector l m

ng th t

i sao l

i ph

i s

ng vector khi b
n
qu quen thu

c v

i m

ng? Xin phntch m

t s

nhc i
m sau c

a m

ng:- N

u b

n s

ng m

ng t
nh: Mng ny lun
c khai bo v
i kch th
c t
i a m b
n c th

dng d

n t

nnhi

u vng nh

th

a.- N

u b

n s

ng m
ng
ng: B

n ph

i xin c

p pht b

nh

, lm vi

c v

i con tr

. Con tr

lkhi ni
m hay trong C, C++, nhng n l nguy
n nhn c

a r

t nhi


u r

c r

i trong l

p trnh.- Khng thu


n tin trong vic truyn tham s kiu mng v
o hm hay tr
li kiu mng t h
m.Nhc i
m quan tr

ng nh

t: N

u b

n s

ng m
ng v
t ch

vt qu kch thc
khai bo, C++ s

khngthng bo l
i, i
u ny d
n
n l

i dy chuy

n do cc l

nh l
i
tc
ng
n cc bi

n khc trong ch
ng tr
nh(trong Pascal b

n c th

ki

m tra trn ch

ng b

ng d

n bin d

ch range check).Vector l m

t container cung c

p kh

nng s
d

ng m

ng m

m d

o, c ki

m sot range check khi c

n thi

t, v

i kch
th
c ty (m khng c

n ph

i s

ng con tr

). Ngoi ra vector cho php b

n chn thm ho
c xa i m
t s

ph

nt

ch


ng 1 l

nh (khng ph

i s

ng vng l
p nh
i v

i m

ng).
III.C php :
c th d
ng vector th b
n phi th
m 1 header#include <vector>v ph
i c
using std::vector;v vector
c nh
ngh
a trong STL( Standard Template Library).
C php c
a vector cng rt n gin v d :
vector<int>A; Cu l
nh trn nh ngha 1 vector c kiu int. Ch kiu ca vector c
trong 2 ci ngoc nhn. V
kck th
c
c
a vector c th n
ng ln, cho nn khng c
n khai bo cho n c bao nhi
u ph
n t cng c, hoc nu thch khai
bo th b
n cng c th khai bo nh sau :
vector<int>A(10); Cu l
nh tr
n khai bo A l 1 vector ki
u int c 10 phn t. Tuy nhin nh
ni
tr
n, m
c d size = 10, nhng khi
b
n add v
o th n v
n cho php nh thng.
C
ng c th khi to cho cc phn t trong vector bng c php n gin
nh sau :
vector<int>A(10,2);
You're reading a free preview.
Pages 2 to 16 are not shown in this preview.

c phin bn y
<#>
STL Vector | 2
Trong cu l
n tr
n th 10 ph
n t ca vector A s c khi to bng 2.
ng thi ta cng c th khi to cho 1 vector
s
l
b
n sao
c
a 1
ho
c 1 phn
vector khc, v d
:
vector<int>A(10,2); vector<int>B(A); vector<int>C(A.begin(),A.begin() +5);

hi

u r h
n v
vector, b

n hy theo di v d

sau:#include<iostream>/
/ Th vi
n iostream ph

c v

ghi d

li

u ra mn h.nh#include<vector>
// Th vi
n vector, s

ng ki

u vector#include<conio.h>
// Th vi
n conio (s

d
ng hm getchar()
d

ng ct)
using namespace
std;// S

ng namespace std
int
main(){vector<
int
> V(3);// V ki

u vector s

nguyn (s

ng gi

ng m

ng int[3])V[0] = 5;// Gn gi tr

cho cc ph

n t

a bi

n VV[1] = 6;// S

ng d

u mc [] hon ton gi

ng v

i m

ngV[2] = 7;
for
(
int
i=0; i<V.size(); i++)// Ghi gi tr

cc ph

n t

a V ra mn h.nhcout << V[i] << endl;// N

u s

ng m

ng, b

n ph

i c bi
n lu kch th
cgetchar();// D
ng chng tr
nh

xem k

t qu

}V d

trn cho b

n th

y vi

c s

ng vector r
t n gi
n, hon ton gi

ng v

i m
ng nhng b
nh

c qu

n l t

ng, b

n khng ph
i quan tm
n gi

i phng cc vng b

nh

xin c

p pht.
Tr

ng h
p xc nh kch th
c m
ng khi chng tr
nh
ang ch
y, chng ta dng hm d

ng m
c
nh
(default constructor)

khai bo m
ng cha xc nh kch thc, sau dng phng th
c
resize()

xc nh kch th
cc

a m

ng khi c
n. Chng tr
nh sau
y nh
p vo n t

(word) m

i t

l m

t chu

i ki

u string:#include <iostream>#include <string>#include


<vector>using namespacestd; intmain() { intiWordNum;
vector<string>arrWords; cout << "Enter number of words = ";
cin >>iWordNum; arrWords.resize(iWordNum);
for (inti= 0;i<arrWords.size();i++) { cout << "Enter word " <<i<< " =
"; cin >>arrWords[i]; } cout << "After entering data..." <<endl;
for (inti= 0;i<arrWords.size();i++) cout <<arrWords[i] <<endl; }
Output
Enter number of words = 3Enter word 1 = helloEnter word 1 = c++s
You re Reading a Free Preview
Page 2 is not shown in this preview.
Ti v
STL Vector | 3
Enter word 1 = worldAfter entering data...helloc++sworldPress any key

to continue . . .
IV.Cc
phng thc:
Cc phng thc ca stack:
push_back() v pop_back()
#include<iostream>#include<vector>
using namespace
std;
int
main(){
int
i;vector<
int
> V;
for
(i=0; i<5; i++)// L

p 5 l

n, m

i l
n a thm 1 s
vo vectorV.push_back(i);// N
h v
y, vector c th

c s

d
ng nh stack
cout << endl <<"Mang ban dau:"<< endl;
for
(i=0; i<V.size(); i++)// Ghi l

i n

i dung c

a m

ng ra mn h.nhcout << V[i] << endl;V.pop_back( );// Xa ph

n t

v
a chn vo i
cout << endl <<"Xoa phan tu cuoi:"<< endl;
for
(i=0; i<V.size(); i++)// In n

i dung c

a vector sau khi xacout << V[i] << endl;return 0; }V

i v d

trn, b

n c th

th

y ta c th

d
ng vector nh 1 stack
:- Khng nn dng ton t
[] truy xut cc phn t m
n khng t
n ti,
ngh
a l
v d
vector size = 10, m
b
n truy
xu
t 11 l sai. th
m vo 1 gi tr
cho vector m n khng c size trc hoc
full th ta dng hm thnh vinpush_back(), hm ny s
th
m 1 ph
n t v
o cu
i vector.
Tng t
v

i thao tc xa m

t ph

n t

cu

i ra kh

i vector, b

n c
ng ch
c

n s

ng 1 l

nh: pop_back( )
L

p tr.nh C++ Nguy

n Ph Qu
Xa t

i v

tr b

t k

, xa tr

ng

You're Reading a Free Preview


Page 3 is not shown in this preview.
Ti v
STL Vector | 4
#include <iostream> #include <vector> using namespacestd;
template <classT> voidprint(constvector<T>&v)
{ for (inti=0;i<v.size();i++) cout <<v[i] <<endl; } intmain()
{ char *chao[] = {"Xin","chao","tat","ca","cac","ban"};
intn= sizeof(chao)/ sizeof(*chao); vector<char*>v(chao, chao+n); //
y l 1 cch khi to vector
cout << "vector truoc khi xoa" <<endl; print(v); v.erase(v.begin()+2,
v.begin()+5); //xa t

ph
n t
th

2
n
ph
n
t
th 5
v.erase(v.begin()+1 ); //xa ph
n t th 1
cout << "vector sau khi xoa" <<endl; print(v); v.clear();//Xa ton b

cc ph

n t

cout << "Vector sau khi clear co " <<v.size() << " phan tu" <<endl;
return 0; }
Output:
vector truoc khi xoaXinchaotatcacacbanvector sau khi xoaxinbanVector sau
khi clear co 0 phan tu

P
hng thc
chn
iteratorinsert( iteratorposition,
const
T&x);
void
insert( iteratorposition,size_typen,
const
T&x);
void
insert( iteratorposition,InputIteratorfirst,InputIteratorlast);
V d
:
// inserting into a vector
#include <iostream> #include <vector> using namespace std; intmain
(){vector<int>v1(4,100); v1.insert( v1.begin()+3,200);
//
chn 200 vo trc v tr th 3
v1.insert( v1.begin()+2,2,300);
You're Reading a Free Preview
Page 4 is not shown in this preview.
Ti v
Hot ng (1)
Filters
Thm vo b su tpReviewAdd NoteLike
Showing
All
Gn y nht
Reviews
All Notes
Likes
You've already reviewed this. Edit your review <#>.
Post notePost reviewPost replyPost note and like
1 hundred reads
Ti thm
Similar to Stl Vector <https://vi.scribd.com/doc/52580991/Stl-Vector>
STL String <https://www.scribd.com/doc/14721319/STL-String>
river11576
<https://www.scribd.com/doc/14721319/STL-String>
STL String
Stl Vector <https://www.scribd.com/doc/14626851/Stl-Vector>
river11576

<https://www.scribd.com/doc/14626851/Stl-Vector>
Stl Vector
Bai+tap+Cau+truc+du+lieu+va+giai+thuat+-+SGU2009_2
<https://www.scribd.com/doc/39195156/Bai-tap-Cau-truc-du-lieu-va-giai-thuat-SGU2
009-2>
Tn Li
<https://www.scribd.com/doc/39195156/Bai-tap-Cau-truc-du-lieu-va-giai-thuat-SGU2
009-2>
Bai+tap+Cau+truc+du+lieu+va+giai+thuat+-+SGU2009_2
Lap trinh C++ <https://www.scribd.com/doc/67265684/Lap-trinh-C>
phanthithe
<https://www.scribd.com/doc/67265684/Lap-trinh-C>
Lap trinh C++
Nen_Huffman <https://www.scribd.com/doc/73790116/Nen-Huffman>
conghoang9x
<https://www.scribd.com/doc/73790116/Nen-Huffman>
Nen_Huffman
Cu Trc2 <https://www.scribd.com/doc/62332913/Cu-Truc2>
Tien Duc Bui
<https://www.scribd.com/doc/62332913/Cu-Truc2>
Cu Trc2
GT_KTLT2 <https://www.scribd.com/doc/61845213/GT-KTLT2>
NGAYNGO
<https://www.scribd.com/doc/61845213/GT-KTLT2>
GT_KTLT2
Thuat Toan va Giai Thuat
<https://www.scribd.com/doc/26755/Thuat-Toan-va-Giai-Thuat>
anon-706195
<https://www.scribd.com/doc/26755/Thuat-Toan-va-Giai-Thuat>
Thuat Toan va Giai Thuat
Thm t Ngi dng ny
<https://vi.scribd.com/ngo_tuan_19/content?content_key=documents>
TKB_thuc_hanh_10HC-HK1_cap_nhat
<https://www.scribd.com/doc/55996604/TKB-thuc-hanh-10HC-HK1-cap-nhat>
Ngo Anh Tuan
<https://www.scribd.com/doc/55996604/TKB-thuc-hanh-10HC-HK1-cap-nhat>
TKB_thuc_hanh_10HC-HK1_cap_nhat
LRC <https://www.scribd.com/doc/54986707/LRC>
Ngo Anh Tuan
<https://www.scribd.com/doc/54986707/LRC>
LRC
ngon_ngu_R_2 <https://www.scribd.com/doc/50849921/ngon-ngu-R-2>
Ngo Anh Tuan
<https://www.scribd.com/doc/50849921/ngon-ngu-R-2>
ngon_ngu_R_2

Ti xung v in ti liu ny
* c v in khng c qung co
* Download to keep your version
* Edit, email or read offline
Choose a format:
.PDF.TXT
Ti v
khuyn ngh
<https://vi.scribd.com/doc/14721319/STL-String>
STL String <https://vi.scribd.com/doc/14721319/STL-String>
river11576
Ti liu ny l bn gc cha c ch k c nhn bin son bi
river11576@gmail...
<https://vi.scribd.com/doc/14626851/Stl-Vector>
Stl Vector <https://vi.scribd.com/doc/14626851/Stl-Vector>
river11576
Ti liu ny l bn gc cha c ch k c nhn bin son bi
river11576@gmail...
<https://vi.scribd.com/doc/39195156/Bai-tap-Cau-truc-du-lieu-va-giai-thuat-SGU20
09-2>
Bai+tap+Cau+truc+du+lieu+va+giai+thuat+-+SGU2009_2
<https://vi.scribd.com/doc/39195156/Bai-tap-Cau-truc-du-lieu-va-giai-thuat-SGU20
09-2>
Tn Li
<https://vi.scribd.com/doc/67265684/Lap-trinh-C>
Lap trinh C++ <https://vi.scribd.com/doc/67265684/Lap-trinh-C>
phanthithe
<https://vi.scribd.com/doc/73790116/Nen-Huffman>
Nen_Huffman <https://vi.scribd.com/doc/73790116/Nen-Huffman>
conghoang9x
<https://vi.scribd.com/doc/62332913/C%E1%BA%A5u-Truc2>
Cu Trc2 <https://vi.scribd.com/doc/62332913/C%E1%BA%A5u-Truc2>
Tien Duc Bui
<https://vi.scribd.com/doc/61845213/GT-KTLT2>
GT_KTLT2 <https://vi.scribd.com/doc/61845213/GT-KTLT2>
NGAYNGO
<https://vi.scribd.com/doc/26755/Thuat-Toan-va-Giai-Thuat>
Thuat Toan va Giai Thuat
<https://vi.scribd.com/doc/26755/Thuat-Toan-va-Giai-Thuat>
anon-706195
Trc <#>|Next <#>Page 1 of 2

You're Reading a Free Preview


Ti v
scribd. scribd. scribd. scribd. scribd. scribd. scribd. scribd. scribd.
scribd. scribd. scribd. scribd.
*About*
*
*
*
*
*
*
*

Browse books <https://vi.scribd.com/books>


Browse documents <https://vi.scribd.com/browse>
Gii thiu v Scribd <https://vi.scribd.com/about>
Meet the team <https://vi.scribd.com/about/team>
Our blog <http://blog.scribd.com/>
Tham gia nhm ca chng ti! </about/jobs>
Contact Us <https://vi.scribd.com/contact>

*H tr*
*
*
*
*

Tr gip <http://support.scribd.com/login>
FAQ </faq>
Bao chi </press>
Purchase help
<http://support.scribd.com/forums/189164-Purchases-subscriptions-and-refunds

>
* AdChoices </adchoices>
*i tc*
* Nh xut bn </publishers>
* Nh pht trin / API </developers>
*Php l*
o iu khon </terms>
o Bo mt </privacy>
o Bn quyn <https://vi.scribd.com/copyright>
*Memberships*
* Join today <https://vi.scribd.com/subscribe>
* Invite Friends <https://vi.scribd.com/referrals?source=footer>
* Gifts <https://vi.scribd.com/giftcards>
*Stay Connected*
<http://blog.scribd.com><http://blog.scribd.com><https://www.facebook.com/pa
ges/Scribd/6978454082><https://www.facebook.com/pages/Scribd/6978454082><https:/
/twitter.com/scribd><https://twitter.com/scribd><https://www.pinterest.com/scrib
d/><https://www.pinterest.com/scribd/>
Scribd on Appstore
<https://itunes.apple.com/us/app/scribd-worlds-largest-online/id542557212?mt
=8&uo=4&at=11lGEE>
Scribd on Google Play
<https://play.google.com/store/apps/details?id=com.scribd.app.reader0&hl=en>
Copyright 2015 Scribd Inc. .Terms of service
<https://vi.scribd.com/terms>.Privacy
<https://vi.scribd.com/privacy>.Mobile Site
<https://vi.scribd.com/mobile?remobile=1>.Site Language:Ting Vit

English
</language?destination=https%3A%2F%2Fwww.scribd.com%2Fdoc%2F52580991&id=en&ancho
r=>

</language?destination=https%3A%2F%2Fzh.scribd.com%2Fdoc%2F52580991&id=zh&anchor
=>
Espaol
</language?destination=https%3A%2F%2Fes.scribd.com%2Fdoc%2F52580991&id=es&anchor
=>
</language?destination=https%3A%2F%2Far.scribd.com%2Fdoc%2F52580991&id=ar&anchor
=>
Portugus
</language?destination=https%3A%2F%2Fpt.scribd.com%2Fdoc%2F52580991&id=pt-br&anc
hor=>

</language?destination=https%3A%2F%2Fja.scribd.com%2Fdoc%2F52580991&id=ja&anchor
=>
Deutsch
</language?destination=https%3A%2F%2Fde.scribd.com%2Fdoc%2F52580991&id=de&anchor
=>
Franais
</language?destination=https%3A%2F%2Ffr.scribd.com%2Fdoc%2F52580991&id=fr&anchor
=>
Turkce
</language?destination=https%3A%2F%2Ftr.scribd.com%2Fdoc%2F52580991&id=tr&anchor
=>

</language?destination=https%3A%2F%2Fru.scribd.com%2Fdoc%2F52580991&id=ru&anchor
=>
Ting vit
</language?destination=https%3A%2F%2Fvi.scribd.com%2Fdoc%2F52580991&id=vi&anchor
=>
Jzyk polski
</language?destination=https%3A%2F%2Fpl.scribd.com%2Fdoc%2F52580991&id=pl&anchor
=>
Bahasa indonesia
</language?destination=https%3A%2F%2Fid.scribd.com%2Fdoc%2F52580991&id=id&anchor
=>
Are you sure?
This action might not be possible to undo. Are you sure you want to
continue?
CANCEL
OK
You've been reading!

NO, THANKS
OK
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var
s_code=s.t();if(s_code)document.write(s_code)//-->

You might also like