Vector STL in C++
CodeHelp Supreme Batch
Introduction
1. The Standard Template Library (STL) provides a collection of template classes and functions that
offer common data structures and algorithms to make programming more efficient and
convenient.
2. A vector in C++ is a dynamic array that can grow or shrink in size, making it a versatile and
efficient data structure for storing and manipulating sequences of elements.
int arr [5] ; - dynamic -
stetic
I
⑤
I
I
W
~
4 T
Features
1. Contiguous Memory: Elements in a vector are stored in contiguous memory locations, which
makes it efficient for random access and iteration.
2. Dynamic Sizing: Unlike built-in arrays in C++, which have a fixed size, vector can dynamically
resize itself as elements are added or removed. This dynamic sizing is managed internally, so you
don't need to worry about memory management.
3. Automatic Reallocation: When a vector reaches its capacity and you try to add more elements, it
automatically reallocates memory to accommodate the new elements. This allows you to work with
dynamic-sized collections without worrying about memory management.
4. Size and Capacity: vector maintains two important properties: the size, which is the number of
elements currently stored in the vector, and the capacity, which is the number of elements the
vector can hold without reallocation.
5. Array-Like Access: You can access elements in a vector using array-like syntax, using square
brackets ([]) or the at() member function.
[5] ; Mem
int arr
Static .
-> W
~
Static org ->
allocation
- dynamic away
-
-
int n ; i/p 0 ->
50
cinn ; 0-
int[n];
=> war iP int * aur
=
new
o
↓
10/30/40/50
2 > 2
Dymanic memory
↓01 50 size
-
allocator
[5]=80/1 ↓
-
-> aur
2 sith push to kene
to puchna has
Na mer se
f -
-
capacite
data
keep inserti
the
↓
.
2) just size
-
Cap . -
↓ -
Vector <ints v ;
I
I M A
V .
push-back() ; 0
2 2
1 I (2) !
T ⑧
/I 3) ; #31 Y 3
01 2 3
-
(4) ; -34 4 4
0 ,
23
17 (5) ; 41511 I 8 5
0123456 7
its
2
2
01
Front- v[o]
~ [vsize() -1)
End -
-
I
v[3 -
I
I
v[2] ->