Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- template<class Type>
- int dsize(Type* a) {
- if (a)
- return _msize(a) / sizeof(Type);
- else
- return 0;
- }
- template<class Type>
- void show_arr(int sz, Type* arr, char ch = ' ') {
- for (int k = 0; k < sz; ++k)
- std::cout << arr[k] << ch;
- std::cout << std::endl;
- }
- template <class Type>
- bool append(Type*& arr, Type val) {
- if (arr) {
- auto buff = new Type[dsize(arr) + 1];
- for (int k = 0; k < dsize(arr); ++k)
- buff[k] = arr[k];
- buff[dsize(arr)] = val;
- delete[] arr;
- arr = buff;
- return true;
- }
- else {
- return false;
- }
- }
- template<class Type>
- bool insert(Type*& arr, int id, Type val) {
- if (arr && dsize(arr) > id) {
- auto buff = new Type[dsize(arr) + 1];
- for (int k = 0; k < id; ++k)
- buff[k] = arr[k];
- buff[id] = val;
- for (int k = id; k < dsize(arr); k++)
- buff[k + 1] = arr[k];
- //show_arr(dsize(buff), buff);
- delete[] arr;
- arr = buff;
- return true;
- }
- else {
- return false;
- }
- }
- template<class Type>
- void myswap(Type& a, Type& b) {
- Type t = a;
- a = b;
- b = t;
- }
- template<class Type>
- void shuffle(Type* arr) {
- int sz = dsize(arr);
- for (int k = 0; k < 100; k++)
- myswap(arr[rand() % sz],
- arr[rand() % sz]
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment