-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert&erase예제.cpp
50 lines (44 loc) · 1.06 KB
/
insert&erase예제.cpp
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
#include <bits/stdc++.h>
using namespace std;
void insert(int idx, int num, int arr[], int &len) {
for (int i = len; i >= idx + 1; i--)
arr[i] = arr[i - 1];
arr[idx] = num;
len++;
}
void erase(int idx, int arr[], int &len) {
for (int i = idx; i <= len; i++)
arr[i] = arr[i + 1];
len--;
}
void printArr(int arr[], int &len) {
for (int i = 0; i < len; i++)
cout << arr[i] << ' ';
cout << "\n\n";
}
void insert_test() {
cout << "***** insert_test *****\n";
int arr[10] = {10, 20, 30};
int len = 3;
insert(3, 40, arr, len); // 10 20 30 40
printArr(arr, len);
insert(1, 50, arr, len); // 10 50 20 30 40
printArr(arr, len);
insert(0, 15, arr, len); // 15 10 50 20 30 40
printArr(arr, len);
}
void erase_test() {
cout << "***** erase_test *****\n";
int arr[10] = {10, 50, 40, 30, 70, 20};
int len = 6;
erase(4, arr, len); // 10 50 40 30 20
printArr(arr, len);
erase(1, arr, len); // 10 40 30 20
printArr(arr, len);
erase(3, arr, len); // 10 40 30
printArr(arr, len);
}
int main(void) {
// insert_test();
erase_test();
}