-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTL-Vectors.cpp
More file actions
68 lines (56 loc) · 1.6 KB
/
STL-Vectors.cpp
File metadata and controls
68 lines (56 loc) · 1.6 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
#include <iostream>
#include <vector>
using namespace std;
// Array is not reziable.
// You have to create new array to change size of an array.
// Vector is resizable
template <class T>
void display(vector<T> &v)
{
for (int i = 0; i < v.size(); i++)
{
// cout << v[i] << " ";
cout << v.at(i) << " ";
}
cout << endl;
}
int main()
{
// ways to create a vector
vector<int> vec1; // zero length vector
// display(vec1);
// vector<char> vec2(4); // four element character vector
// vec2.push_back('5');
// display(vec2);
// vector<char> vec3(vec2); // four element character vector from vec2
// display(vec3);
int element;
int size;
cin >> size;
// push_back : Add element at the end (takes 1 arguement)
// size : returns size of vector (takes no arguement)
// pop_back : pop the last element (takes no arguement)
// insert: insert the element at the specified position (iter, how much time you want to insert, element to be inserted)
// at: returns element at the position (takes 1 arguement)
// clear: it clears the element of the vector
for (int i = 0; i < size; i++)
{
cout << "Enter element";
cin >> element;
vec1.push_back(element);
}
// vec1.pop_back();
// display(vec1);
// vector<int>::iterator iter = vec1.begin();
// vec1.insert(iter+3, 3, 566);
// display(vec1);
// cout << vec1.size();
// vec1.clear();
vector<int> vec2;
vec2.push_back(373);
vec2.push_back(32);
vec2.push_back(3392);
vec1.swap(vec2);
display(vec1);
return 0;
}