|
| 1 | +/* |
| 2 | +Vector in c++ |
| 3 | +Vector is a type of container |
| 4 | +It is used when we don't know how much data we enter we |
| 5 | +don't know it support dynamic array(means have some capacity) |
| 6 | +Vecotor increase it size according to it requirement |
| 7 | +it increase the size according to multiply with 2 |
| 8 | +means if two value inserted and inserting third will increment according to 4 |
| 9 | +you need to include a vector |
| 10 | +to create a blank vecotr use this command |
| 11 | +vector <int>v1; |
| 12 | +*/ |
| 13 | +#include<iostream> |
| 14 | +#include<vector> |
| 15 | +using namespace std; |
| 16 | +int main() |
| 17 | +{ |
| 18 | + // vector <int>v1; |
| 19 | + // to initile it to the point use this command |
| 20 | + vector <int>v1 { 10,20,30}; |
| 21 | + // while declaring initilization capacity will be 3 |
| 22 | + // it will not follow by default value 1 |
| 23 | + // now it will increment 3 than 6 so on |
| 24 | + // to initilize it size |
| 25 | + |
| 26 | + vector <char> v2(5); |
| 27 | + |
| 28 | + // to initilize the value and also size |
| 29 | + // here 5 is the size and 10 is the value |
| 30 | + |
| 31 | + vector <int> v3(5,10); |
| 32 | + vector <string> v4(3,"hello"); |
| 33 | + vector <int> v5; |
| 34 | + |
| 35 | + // square bracket |
| 36 | + |
| 37 | + cout<<v4[0]<<" "<<v4[1]<<" "<<v4[2]<<"\n"; |
| 38 | + |
| 39 | + // loop |
| 40 | + |
| 41 | + for(int i=0;i<4;i++) |
| 42 | + { |
| 43 | + cout<<v1[i]<<" "; |
| 44 | + } |
| 45 | + cout<<endl; |
| 46 | + |
| 47 | + // push_back to insert a value |
| 48 | + |
| 49 | +// cout<<"Current Capacity : "<<v5.capacity()<<"\n"; |
| 50 | +// v5.push_back(10); |
| 51 | +// cout<<"Current Capacity : "<<v5.capacity()<<"\n"; |
| 52 | +// v5.push_back(20); |
| 53 | +// cout<<"Current Capacity : "<<v5.capacity()<<"\n"; |
| 54 | +// v5.push_back(30); |
| 55 | + |
| 56 | +// // you can see here capacity increase by multiple of 2 |
| 57 | + |
| 58 | +// cout<<"Current Capacity : "<<v5.capacity()<<"\n"; |
| 59 | +// v5.push_back(30); |
| 60 | +// cout<<"Current Capacity : "<<v5.capacity()<<"\n"; |
| 61 | + |
| 62 | + // by loop |
| 63 | + |
| 64 | + cout<<"Current Capacity : "<<v5.capacity()<<"\n"; |
| 65 | + for(int i=0;i<=9;i++) |
| 66 | + { |
| 67 | + v5.push_back(10*(i+1)); |
| 68 | + } |
| 69 | +// cout<<"Current Capacity : "<<v5.capacity()<<"\n"; |
| 70 | +// cout<<"Size : "<<v5.size()<<"\n"; |
| 71 | + |
| 72 | +// // pop_back to delete last value |
| 73 | + |
| 74 | +// v1.pop_back(); |
| 75 | +// cout<<"After pop : "<<endl; |
| 76 | +// cout<<"Current Capacity : "<<v5.capacity()<<"\n"; |
| 77 | +// cout<<"Size : "<<v5.size()<<"\n"; |
| 78 | + |
| 79 | +// // size return number of element |
| 80 | + |
| 81 | +// v1.pop_back(); |
| 82 | +// cout<<"Current Capacity : "<<v5.capacity()<<"\n"; |
| 83 | +// v1.pop_back(); |
| 84 | +// cout<<"Current Capacity : "<<v5.capacity()<<"\n"; |
| 85 | +// v1.pop_back(); |
| 86 | +// cout<<"Current Capacity : "<<v5.capacity()<<"\n"; |
| 87 | +// v1.pop_back(); |
| 88 | +// cout<<"Current Capacity : "<<v5.capacity()<<"\n"; |
| 89 | +// cout<<"Size : "<<v5.size()<<"\n"; |
| 90 | + |
| 91 | +// to print |
| 92 | + |
| 93 | +for(int i=0;i<v5.size();i++) |
| 94 | +{ |
| 95 | + cout<<v5[i]<<" "; |
| 96 | +} |
| 97 | +cout<<endl; |
| 98 | + |
| 99 | +// to clear |
| 100 | + |
| 101 | +//v5.clear(); |
| 102 | +//cout<<"Current Capacity : "<<v5.capacity()<<"\n"; |
| 103 | +//cout<<"Total element : "<<v5.size()<<endl; |
| 104 | + |
| 105 | +// at to return index |
| 106 | +cout<<"Value at index 4 is : "<<v5.at(4)<<endl; |
| 107 | + |
| 108 | +// front and back |
| 109 | + |
| 110 | +cout<<"First value is : "<<v5.front()<<"\n"; |
| 111 | +cout<<"Last value is : "<<v5.back()<<"\n"; |
| 112 | + |
| 113 | +// to insert a value at mid use iterator |
| 114 | + |
| 115 | +vector <int>:: iterator it=v5.begin(); |
| 116 | +v5.insert(it+3,35); // here it is beggining position +2 show 3 rd position ,35 is value we insert |
| 117 | +for(int i=0;i<v5.size();i++) |
| 118 | +{ |
| 119 | + cout<<v5[i]<<" "; |
| 120 | +} |
| 121 | +cout<<endl; |
| 122 | +} |
0 commit comments