Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include<iostream>
- #include<fstream>
- #include<string>
- #include<vector>
- using std::cout;
- using std::string;
- using std::fstream;
- using std::vector;
- string solveEx1(string filepath_input, string filepath_output) {
- vector<std::pair<int, string>> contents;
- fstream file;
- file.open(filepath_input.c_str(), std::ios::in);
- string nameTmp;
- int ageTmp;
- while (!file.eof()) {
- file >> nameTmp;
- file >> ageTmp;
- contents.push_back(std::make_pair(ageTmp, nameTmp));
- }
- // Search for max and min age
- int min_ = contents[0].first;
- int max_ = contents[0].first;
- for (unsigned int i = 0; i < contents.size(); i++) {
- if (min_ > contents[i].first)
- min_ = contents[i].first;
- if (max_ < contents[i].first)
- max_ = contents[i].first;
- }
- string ret_max_names = "";
- string ret_min_names = "";
- for (unsigned int i = 0; i < contents.size(); i++) {
- if (contents[i].first == min_) {
- if(ret_min_names.find(contents[i].second) == string::npos)
- ret_min_names = ret_min_names + contents[i].second;
- if (i != contents.size() - 1)
- ret_min_names += " ";
- }
- if (contents[i].first == max_) {
- if (ret_max_names.find(contents[i].second) == string::npos)
- ret_max_names = ret_max_names + contents[i].second;
- if (i != contents.size() - 1)
- ret_max_names += " ";
- }
- }
- /*
- cout << "\n";
- cout << min_ << " " << ret_min_names << "\n";
- cout << max_ << " " << ret_max_names << "\n";
- */
- string ret = std::to_string(min_) + " " + ret_min_names + "\n";
- ret += std::to_string(max_) + " " + ret_max_names + "\n";
- fstream file_output;
- file_output.open(filepath_output.c_str(), std::ios::out);
- file_output << ret;
- cout << "[+] Saved results to: " << filepath_output << "\n";
- return nameTmp;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement