Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <fstream>
- #include <list>
- using namespace std;
- void PrintList(ostream& file, const list<int>& list) {
- for (std::list<int>::const_iterator it = list.begin(); it != list.end(); it++)
- {
- file << *it;
- if (it != --list.end()) {
- file << endl;
- }
- }
- }
- list<int> ListWithRandomNumbers(int N, int M) {
- list<int> result(N);
- generate(result.begin(), result.end(), [M]() {return rand() % (2 * M) - M; });
- return result;
- }
- fstream FileWithRandomNumbersByStdGenerate(string fileName, int N, int M) {
- fstream file(fileName, ios::in | ios::out | ios::trunc);
- PrintList(file, ListWithRandomNumbers(N, M));
- file.flush();
- return file;
- }
- fstream FileWithRandomNumbersByCycle(string fileName, int N, int M) {
- fstream file(fileName, ios::in | ios::out | ios::trunc);
- for (int i = 0; i < N; i++) {
- file << rand() % (2 * M) - M;
- if (i != N - 1) {
- file << endl;
- }
- }
- file.flush();
- return file;
- }
- list<int> ListFromFile(fstream& file) {
- file.seekg(0, ios::beg);
- list<int> result;
- int temp;
- while (file.peek() != EOF)
- {
- file >> temp;
- result.push_back(temp);
- }
- file.close();
- return result;
- }
- int FindMax(list<int>::const_iterator begin, list<int>::const_iterator end) {
- list<int>::const_iterator it = begin;
- int result = *it;
- for (; it != end; it++) {
- if (*it > result) {
- result = *it;
- }
- }
- return result;
- }
- int Max(const list<int>& list) {
- return FindMax(list.begin(), list.end());
- }
- list<int> Modify(const list<int>& list) {
- std::list<int>::const_iterator begin = list.begin();
- std::list<int>::const_iterator end = list.end();
- std::list<int> res(begin, end);
- int max = Max(res);
- std::list<int>::iterator it = res.begin();
- for (; it != end; it++) {
- *it -= max;
- }
- return res;
- }
- list<int> Modify(list<int>::const_iterator begin, list<int>::const_iterator end)
- {
- std::list<int> res(begin, end);
- int max = Max(res);
- list<int>::iterator it = res.begin();
- for (; it != end; it++) {
- *it -= max;
- }
- return res;
- }
- list<int> ModifyWithTransform(const list<int>& list) {
- int max = Max(list);
- std::list<int> res(list.size());
- transform(list.begin(), list.end(), res.begin(), [max](int num) { return num - max; });
- return res;
- }
- list<int> ModifyWithForEach(const list<int>& list) {
- int max = Max(list);
- std::list<int> res(list.begin(), list.end());
- for_each(res.begin(), res.end(), [max](int &num) { num -= max; });
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement