In general, the essence of the question is as follows. The program creates a file with approximately the following content: 1) 1,2,3,4 2) 5,2,3,4 3) 1,2,8,4 4) 1,2,3,3 5) 4,3, 2.1 n) .......

I need to remove duplicate (containing the same elements, but let's say in a different order) lines for example 1 and 5. How to do this?

    2 answers 2

    I propose such an option (concept): read each line into the T class, for which operator == (const T &) and operator <(const T &) are defined. Objects of this class are added to the set std :: set <T> M. Then, if the comparison operator recognizes elements of lines 1 and 5 (see the example of the question author) as identical, then a new element will not be added to the set M. After reading the entire source file, only unique instances of class T will remain in the set, so you can simply output all of them into the source file, overwriting it.

    • theoretically does not sound bad, I will try to implement - saysmonic
    • Only, if you look at the given data, you should use not std::set , but std::multiset - alexlz
    • alexlz, so std :: multiset will make it possible to add identical objects, which is not necessary. - vladimir_ki
    • one
      This is what. Those. I was wrong to write that multiset is needed instead of set. If you do not care about efficiency, then the global structure will be set<multiset<int> > . And it is not necessary to deduce from this set if the old order of lines and elements in lines is significant. - alexlz
    • Now I understand your offer. In fact, you propose not to write your class T to store and compare elements of a single line, but to use std :: multiset <int> instead. - vladimir_ki

    @vladimir_ki and so?

     #include <iostream> #include <string> #include <sstream> #include <set> using namespace std; int main() { string line; set<multiset<int> > s; while(getline(cin,line)) { stringstream lineStream(line); string cell; multiset<int> ms; while(getline(lineStream,cell,',')) { int cellint; stringstream cellstream(cell); cellstream >> cellint; ms.insert(cellint); } if(s.find(ms) == s.end()) { cout << line << endl; s.insert(ms); } } return 0; } 

    UPD cleaned up the result.

    • Yes, this is a good solution, all in C ++ and STL. First of all, its visibility of what is happening. There was a question: why is the result variable needed? - vladimir_ki
    • sorry "like" I can not put, thanks for the option, drove under him. By the way, this is why I suffered: Given a positive integer n. Find out whether it can be represented as a sum of 4 prime numbers. Print all possible options without repetition. With the last part, "no repetition options" and problems. The task here is codepaste.ru/8957 . thanks again - saysmonic
    • @vladimir_ki result? - this is an important thing. The most lazy thing was to think about entering csv, took a sample from stackoverflow. And this is the tail that I forgot to clean up. - alexlz
    • @alexlz is understandable. Well, then you can remove it from your example so that it will not multiply across the expanses of the runet :) - vladimir_ki
    • @saymonic_ And if the next tests are made from numbers no less tried, how will the repetitions appear? - alexlz