-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path41_STL.cpp
96 lines (86 loc) · 2.69 KB
/
41_STL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/* STL : Standard Template Library
It is used because its a good idea not to reinvent the wheel
-In C++, the STL Standard Template Library has many algorithms for some
operations like searching, counting, and manipulation of ranges and their
elements.
Component : -Container
-Use Template classes
-Stores data
-Algorithm (Procedure to process data)
-sorting
-Searching
-Use template functions
-Iterator
-Objects points to an element in the container
-Handled just like pointer
-Connects ALgo. with Container
STL : Container + Algo. + Interator
RA = Roughness Average
-Container:
-Sequence : Linear fashion
-Vector = -RA fast
-Insertion/Del Middle = slow
-Insertion/Del in end = fast
-list = -RA slow
-Insertion/Del in middle = fast
-Insertion/Del in end = slow
, Diqueue
-Associative : Direct access(Tree)
=All operation fast
except RA ( Roughness average )
-Set/multiset
-Map/Mutlimap
-Derived : Real world modeling
= Depend on data structure used
-Stack
-Queue
-Priority - Queue
*/
#include <iostream>
#include <vector>
using namespace std;
void display(vector<int> &v)
{
// for(int i=0; i< v.size() = we can also use this
cout << " - ";
// int i=0;
for (int n : v)
{
cout << n << " ";
// cout<<v.at(i)<<" ";
// i++;
}
cout << endl;
}
int main()
{
vector<int> vec1; // Zero length vector declared
// It manages its size by own
int element;
int size;
cout << "Enter size of the vector : ";
cin >> size;
for (int i = 0; i < size; i++)
{
cout << "Enter element : ";
cin >> element;
vec1.push_back(element); // Length is increased , element added in the end
}
cout << "Given vector is : " << endl;
display(vec1);
vec1.pop_back();
cout << "After pop back : " << endl;
display(vec1);
// We can also overwrite any element
vec1[1] = 44;
cout << "AFter overwriting 2nd element : " << endl;
display(vec1);
vector<int>::iterator itr = vec1.begin();
vec1.insert(itr, 88); // Inserting at first position
cout << "AFter insertion 1st element : " << endl;
display(vec1);
cout << "Inserting copies from 3rd element in vector : " << endl;
vec1.insert(itr + 2, 10, 69);
display(vec1);
return 0;
}