-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.txt
More file actions
84 lines (63 loc) · 1.73 KB
/
Copy pathVector.txt
File metadata and controls
84 lines (63 loc) · 1.73 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
0- empty(): it returns whether the container is empty or not.
/*
if (num.empty() == false)
std::cout << "Vector is not empty";
else
std::cout << "Vector is empty";
*/
1- Fill vector
/*
int main()
{
std::vector<int> num; // Declaration of Vector in c++
// Initializing vector with values from 1 to 7;
for (int i = 1; i <= 7; i++)
num.push_back(i);
}
*/
2- Delete Elements from C++ Vectors
/*
std::vector<int> num;
// declare iterator to iterate through Vector using iterators
std::vector<int>::iterator iter;
//use iterator with for loop
for (iter = num.begin(); iter != num.end(); ++iter)
{
std::cout << *iter << " ";
}
return 0;
*/
3- Access Elements of a Vector
/*
// ==> In C++, we use the index number to access the vector elements.
// ==> Here, we use the "at()" function to access the element from the special index.
int main() {
std::vector<int> num;
std::cout << num.at(2) << std::endl;
std::cout << num.at(0) << std::endl;
return (0);
}
*/
4- Change Vector Element
/*
int main() {
std::vector<int> num{1, 2, 3, 4, 5};
std::vector::iterator iter;
for (iter = num.begin(); iter != num.end(); ++iter)
std::cout << *iter << " ";
std::cout << std::endl;
// change elements at indexes 1 and 4
num.at(1) = 0;
num.at(4) = 7;
for (iter = num.begin(); iter != num.end(); ++iter)
std::cout << *iter << " ";
std::cout << std::endl;
}
*/
5- erase() : remove elements from a specified element or a range in the vector
/*
// removing the first element
num.erase(num.begin());
*/
// https://www.freecodecamp.org/news/cpp-vector-how-to-initialize-a-vector-in-a-constructor/
// erase : https://www.bitdegree.org/learn/c-plus-plus-vector