Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //////main.cpp///////
- #include <vector>
- #include <iostream>
- #include <utility>
- #include <string>
- #include <algorithm>
- #include "string_edit.h"
- #include <fstream>
- #include <set>
- #include <algorithm>
- #include <string>
- using namespace std;
- ostream& operator<<(ostream& out, const vector<string>& vect) {
- for (const auto& x : vect) {
- out << x << " ";
- }
- return out;
- }
- vector<string> Intersection(const string& file_name) {
- vector<string> result;
- vector<vector<string>> file_sets;
- ifstream in(file_name);
- if (in) {
- string line;
- for(int i=0; getline(in, line);i++){
- StringModify str_mod(line);
- vector<string> temp = str_mod.modify();
- if (i!=0) {
- for (string& str : temp) {
- sort(str.begin(), str.end());
- }
- }
- file_sets.push_back(move(temp));
- }
- }
- for (const auto& element : *file_sets.begin()) {
- size_t counter = 0;
- string element_sort = element;
- sort(element_sort.begin(), element_sort.end());
- for (auto set_ = file_sets.begin() + 1; set_ != file_sets.end(); set_++) {
- for (const auto& x : *set_) {
- if (element_sort == x) {
- counter++;
- break;
- }
- }
- }
- if (counter == file_sets.size() - 1) {
- result.push_back(element);
- for (auto set_ = file_sets.begin() + 1; set_ != file_sets.end(); set_++) {
- auto it = find(set_->begin(), set_->end(), element_sort);
- if (it != set_->end()) {
- set_->erase(it);
- }
- }
- }
- counter = 0;
- }
- return result;
- }
- int main() {
- cout << Intersection("test.txt");
- }
- /////string_edit.h////////
- #pragma once
- #include <string>
- #include <vector>
- #include <set>
- #include <iostream>
- #include <sstream>
- #include <string_view>
- class StringModify {
- public:
- StringModify(const std::string& str):string_(str){}
- std::vector<std::string> modify() {
- clean(string_);
- return split(string_);
- }
- private:
- std::string string_;
- void clean(std::string& str) {
- str.erase(str.begin());
- str.erase(prev(str.end()));
- const auto end = str.end();
- auto it = str.begin();
- while (it != end) {
- it = std::find(it, end, ' ');
- if (it == end) {
- break;
- }
- str.erase(it);
- }
- }
- std::vector<char> open_symbols{ '<','{','[' };
- std::vector<char> close_symbols{ '>','}',']' };
- bool consits(const std::vector<char>& vector, char ch) {
- return std::find(vector.begin(), vector.end(), ch) == vector.end() ? false : true;
- }
- std::vector<std::string> split(const std::string& str) {
- std::vector<std::string> result;
- size_t counter = 0;
- auto pos = str.begin();
- if (str == "") {
- result.push_back(str);
- return result;
- }
- for (auto it = str.begin(); it != str.end(); it++) {
- if (*it == ',' && counter == 0) {
- result.push_back(std::string(pos, it));
- pos = next(it);
- }
- else if (consits(open_symbols, *it)) {
- counter++;
- }
- else if (consits(close_symbols, *it)) {
- counter--;
- if (counter == 0) {
- result.push_back(std::string(pos, next(it)));
- if (next(it) != str.end()) {
- pos = next(++it);
- }
- }
- }
- else if (it == prev(str.end())) {
- result.push_back(std::string(1, *it));
- }
- }
- return result;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement