Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using std::cout; using std::endl; using std::cin;
- #include<fstream>
- using std::ifstream;
- #include<string>
- using std::string;
- #include<algorithm>
- using std::copy; using std::copy_if; using std::swap;
- #include<fstream>
- using std::ifstream;
- #include<utility>
- using std::pair; using std::make_pair;
- #include "lab11_arrays.h"
- /*
- Alex Ralya Lab 11
- */
- template<typename T>
- void insert(T* (&ary), size_t &sz, size_t indx, T val){
- if (indx >= sz){
- sz = indx+1;
- T* new_ary = new T[sz]{};
- copy(ary, ary+sz, new_ary);
- swap(ary,new_ary);
- delete [] new_ary;
- }
- ary[indx] = val;
- }
- size_t fill_from_file(long*& array, string fileName) {
- ifstream in_file(fileName);
- if (in_file){
- string line;
- int counter = 0;
- size_t sz = 0;
- while(std::getline(in_file,line)) {
- if(counter == 0) {
- array = new long[std::stol(line)]
- counter++
- } else {
- sz++;
- insert(array, sz, (counter-1), std::stol(line));
- }
- }
- return sz;
- } else {
- throw std::runtime_error("File could not be opened!");
- }
- }
- void print_array(long* array[], size_t sizeOfArray, ostream& oss) {
- for(int i=0; i < sizeOfArray; i++) {
- oss << array[i] << " ";
- }
- cout << oss.str();
- }
- size_t concatenate(long*& array1, size_t &sizeOfArray1, long* array2, size_t sizeOfArray2) {
- int *array1Front = array1;
- int *array1Back = array1 + sizeOfArray1;
- sizeOfArray1 = sizeOfArray1 + sizeOfArray2;
- int *newArray = new long[sizeOfArray1 + sizeOfArray2]{};
- copy(array1, array1+sizeOfArray1, newArray);
- copy(array2, array2 + sizeOfArray2, newArray + sizeOfArray1);
- swap(array1, newArray);
- delete [] newArray;
- return sizeOfArray1;
- }
- pair<long*, size_t> copy_evens(long array[], size_t sz) {
- size_t szForNewArray = 0;
- for(auto element: array) {
- if(element%2==0){
- szForNewArray++;
- }
- }
- int *newArray = new long[szForNewArray]{};
- copy_if(array, array + sz, newArray, [](long i) {
- return !(i%2);
- } );
- pair<long*, size_t> thePair[newArray, szForNewArray];
- return thePair;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement