-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector.cpp
86 lines (76 loc) · 1.73 KB
/
Vector.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
// Vector:array's with dynamic size
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
void printVec(vector<int> &v){
// cout<<"size:"<<v.size()<<endl;
for(int &i : v){
cout<<i<<" ";
}
// for(int i=0;i<v.size();i++){
// cout<<v[i]<<" ";
// }
// v.push_back(2);
cout<<endl;
}
int main()
{
vector<int> v;
v.push_back(1);
cout<<"Capacity->"<<v.capacity()<<endl;
v.push_back(2);
cout<<"Capacity->"<<v.capacity()<<endl;
v.push_back(3);
cout<<"Capacity->"<<v.capacity()<<endl;
cout<<"Element at index 2->"<<v.at(2)<<endl;
cout<<"Front Element->"<<v.front()<<endl;
cout<<"Back Element->"<<v.back()<<endl;
v.pop_back();
v.pop_back();
v.pop_back();
cout<<"Empty or not : "<<v.empty()<<endl;
int n;
cin>>n;
for(int i=0;i<n;i++){
int ele;
cin>>ele;
v.push_back(ele);
}
printVec(v);
// vector<int> v;
// int n ;
// cin>>n;
// for(int i=0;i<n;i++){
// int e;
// cin>>e;
// // printVec(v);
// v.push_back(e); //O(1)
// }
// printVec(v);
// vector<int> v(10); //size->10,by_default initialise with 0
// vector<int> v(10,3); //prefill with 3
// vector<int> v(3); //prefill with 3
// vector<int> v;
// v.push_back(7);
// v.push_back(8); //O(1)
// printVec(v);
// v.pop_back(); //O(1)
// printVec(v);
// vector<int> v2 = v; //O(n) : copy each elemnt one by one
// vector<int> v2(v); //O(n) : copy each elemnt one by one
// vector<int> &v2=v; //v2 and v is same
// v2.push_back(5);
// printVec(v);
// printVec(v);
// printVec(v2);
// vector<string> v;
// int n;
// cin>>n;
// for(int i=0;i<n;i++){
// string s;
// cin>>s;
// v.push_back(s);
// }
// printVec(v);
return 0;
}