Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template <typename tp>
- struct t_Array {
- tp* array;
- int length;
- void clean() {
- for (int i = 0; i < length; i++) {
- array[i] = 0;
- }
- }
- void setSize(int n) {
- length = n;
- array = new tp[length];
- clean();
- }
- void reSize(int n) {
- tp* test = new tp[length];
- int lenOld = length;
- for (int i = 0; i < length; i++) {
- test[i] = array[i];
- }
- length = n;
- array = new tp[length];
- clean();
- if (lenOld > length) {
- lenOld = length;
- }
- for (int i = 0; i < lenOld; i++) {
- array[i] = test[i];
- }
- }
- void printArr() {
- for (int i = 0; i < length; i++) {
- cout << array[i] << "\t";
- }
- cout << endl;
- }
- void add(int index, tp num) {
- tp* test = new tp[length];
- for (int i = 0; i < length; i++) {
- test[i] = array[i];
- }
- length++;
- array = new tp[length];
- for (int i = 0; i < index; i++) {
- array[i] = test[i];
- }
- array[index] = num;
- for (int i = index + 1; i < length; i++) {
- array[i] = test[i - 1];
- }
- }
- void remove(int index) {
- tp* test = new tp[length];
- for (int i = 0; i < length; i++) {
- test[i] = array[i];
- }
- length--;
- array = new tp[length];
- for (int i = 0; i < index; i++) {
- array[i] = test[i];
- }
- for (int i = index; i < length; i++) {
- array[i] = test[i + 1];
- }
- }
- };
- template <typename t>
- struct Linq
- {
- private:
- typedef bool(*Predecate)(t);
- public:
- t_Array<t> where(t_Array<t> arr, Predecate func) {
- int index = 0;
- t_Array<t> r;
- r.setSize(0);
- for (int i = 0; i < arr.length; i++) {
- if (func(arr.array[i])) {
- r.add(index, arr.array[i]);
- index++;
- }
- }
- r.length = index;
- return r;
- }
- };
- int main() {
- t_Array<int> arr;
- Linq<int> linq;
- arr.setSize(5);
- arr.printArr();
- for (int i = 0; i < 5; i++) {
- arr.array[i] = i + 1;
- }
- arr.printArr();
- arr.add(3, -25);
- arr.printArr();
- cout << "\n\n";
- t_Array<int> r = linq.where(arr, [](int a) {
- if (a > 2) {
- return true;
- }
- else {
- return false;
- }
- });
- r.printArr();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement