Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Array {
- private:
- int capacity;
- int at;
- int *arr;
- public:
- Array(int _capacity) {
- capacity = _capacity;
- at = 0;
- arr = new int[capacity];
- }
- ~Array() {
- }
- void add(int x) {
- if(at == capacity) {
- int *tmp_arr = new int[capacity];
- for(int i = 0; i < at; i++) {
- tmp_arr[i] = arr[i];
- }
- capacity *= 2;
- arr = new int[capacity];
- for(int i = 0; i < at; i++) {
- arr[i] = tmp_arr[i];
- }
- }
- arr[at] = x;
- at++;
- }
- void change(int a, int b) {
- for(int i = 0; i < at; i++) {
- if(arr[i] == a) {
- arr[i] = b;
- }
- }
- }
- void deleteAll(int x) {
- int *tmp_arr = new int[capacity];
- int tmp_at = 0;
- for(int i = 0; i < at; i++) {
- if(arr[i] != x) {
- tmp_arr[tmp_at] = arr[i];
- tmp_at++;
- }
- }
- arr = new int [capacity];
- for(int i = 0; i < tmp_at; i++) {
- arr[i] = tmp_arr[i];
- }
- at = tmp_at;
- }
- void print() {
- for(int i = 0; i < capacity; i++) {
- if(i < at) {
- cout << arr[i] << " ";
- }
- else {
- cout << " _ ";
- }
- }
- cout << endl;
- }
- };
- int main() {
- Array a(5);
- a.add(2);
- a.add(3);
- a.add(4);
- a.print();
- a.add(5);
- a.add(6);
- a.print();
- a.add(7);
- a.print();
- a.change(2, 10);
- a.print();
- a.deleteAll(5);
- a.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement