-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.cpp
More file actions
59 lines (53 loc) · 1.03 KB
/
vector.cpp
File metadata and controls
59 lines (53 loc) · 1.03 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
#include <iostream>
#include <vector>
int main()
{
int arr[10];
int *ptr = new int[10];
for (int i = 0; i < 10; ++i)
{
ptr[i] = i * 10;
}
std::vector<int> data{1, 2, 3};
for (int i = 0; i < 5; ++i)
{
data.push_back(i * 10);
}
// Access
data[0] = 100;
std::cout << data[0] << "<-- data[0]\n";
//}
for (auto x : data)
{
std::cout << x << " ";
}
auto it = data.begin();
std::cout << "\n data.begin() --> " << *it << std::endl;
++it;
--it;
it = it + 5;
// Delete
it = data.begin();
data.erase(it); //<-- erase the first element 100
std::cout << std::endl;
for (auto x : data)
{
std::cout << x << " ";
}
// Insert
it = data.begin() + 5;
data.insert(it, 500);
std::cout << std::endl;
for (auto x : data)
{
std::cout << x << " ";
}
/*
100<-- data[0]
100 2 3 0 10 20 30 40
data.begin() --> 100
2 3 0 10 20 30 40
2 3 0 10 20 500 30 40
*/
return 0;
}