-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path61Vectors_STL.cpp
More file actions
88 lines (70 loc) · 2.39 KB
/
Copy path61Vectors_STL.cpp
File metadata and controls
88 lines (70 loc) · 2.39 KB
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
#include <iostream>
#include<vector>
//cplusplus.com vector methods
using namespace std;
template<class T>
void display(vector<T> &v){
cout<<"displaying this vector"<<endl;
vector<T>::iterator it;
for (it = v.begin(); it != v.end(); it++) //v.size() is default fucn of vector
{
cout<<(*it)<<" ";
// cout<<v.at(i)<<" "; //both syntax are same
}
cout<<endl;
}
int main(){
//****************************HOW TO CREATE A VECTOR****************************
vector<int> vec1; //zero lenght vector
int element,size;
cout<<"Enter the size of the vector "<<endl;
cin>>size;
for (int i = 0; i < size; i++)
{
cout<<"entre an element to add to this vector ";
cin>>element;
vec1.push_back(element); // push_back element is a default func of vector
}
// vec1.pop_back(); // removes the last element.
display(vec1);
vector<int> :: iterator iter = vec1.begin();
//***********************FUNCTIONS IN VECTOR************************************
// vec1.insert(iter, 566); //using iterator //this will display before the first vector
// display(vec1);
// vec1.insert(iter+1, 566); // this will display before the second vector
// display(vec1);
// vec1.insert(iter+1,5, 566); // inserting multiple copies
// display(vec1);
// vec1.erase(vec1.begin()+i);
//****************************TYPE OF VECTORS*************************************
vector<char> vec2(4); // 4-element char vector
// if we made a int vector, then a vector of size 4
// with 0,0,0,0 as its elements will be created
cout<<vec2.size()<<endl;
vec2.push_back('5');
vec2.push_back('p');
vec2.push_back('6');
vec2.push_back('6');
vec2.push_back('6');
display(vec2);
cout<<vec2.size()<<endl;
vector<char> vec3(vec2); // 4-element character vector from vec2
vector<int> vec4(6,3); //6-element vector of 3s
display(vec4);
cout<<vec4.size();
//**************************CREATING 2D VECTOR***************************************
int n;
cin >> n;
vector<vector<int>> matrix;
for (int i = 0; i < n; i++)
{
matrix.push_back(vector<int>());
for (int j = 0; j < n; j++)
{
int x;
cin >> x;
matrix[i].push_back(x);
}
}
return 0;
}