remove duplicates from input.txt and write to result.txt c++

I'm trying to remove words from a string where the characters exist twice. .
i wrote this one but in this program iam finding just duplicate words so i need like that in example

exapmle

input.txt

stack
kcats
stacka
astack
kcats
overflow
ooverflow
overflowo

result.txt

stack
stacka
overflow
ooverflow


myprogram is removing duplicates.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 #include <iostream>
#include <fstream>
#include <string> 
#include <set> 

using namespace std;
int main(){ 
fstream in;
fstream out;
string strFile, strFile2,fName;
bool flag;
set<string> Set;
in.open("input.txt");
while (!in.eof()){ 
    in >> strFile;
    flag = true;
    for(size_t l = 0; l < fName.size(); l++){
        strFile = fName.at(l);
        for(size_t k = 1; k < fName.size(); k++){
            strFile2 = fName.at(k);
            if(strFile.compare(strFile2) == 0){
                fName.erase(fName.begin() + l);
                fName.erase(fName.begin() + k);
            //};
            flag = false;
            break;
            };
        }; 
    };
    if (flag)           // giving fail
        Set.insert(strFile);
};
in.close();
out.open("result.txt");
for(set<string>::iterator it = Set.begin();it != Set.end(); it++){ //cout << *it << endl;
    out << *it << endl;
};
out.close();
//system("pause"); 
return 0; }
You could do it by keeping a separate container of sorted strings. It's not too bad in terms of efficiency, since it's a pretty small exercise.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>

#define find_str( a, b ) std::find( a.begin(), a.end(), b ) == a.end() 

int main( int argc, const char* argv[] )
{
    std::ifstream in( "input.txt" );
    std::ofstream out( "output.txt" );
    std::vector<std::string> lines, sorted_lines;

    if( !in )
    {
        std::cerr << "Error: Couldn't open file input.txt";
        return -1;
    }
    
    std::string str, sorted_str;

    while( in )
    {
        std::getline( in, str );
        sorted_str = str;
        std::sort( sorted_str.begin(), sorted_str.end() );

        if( find_str( sorted_lines, sorted_str ) )
        {
            lines.push_back( str );
            sorted_lines.push_back( sorted_str );
        }
    }

    for( const auto &i : lines )
        out << i << std::endl;

    in.close();
    out.close();

    return 0;
}


output.txt
stack
stacka
overflow
ooverflow
Last edited on
for( const auto &i : lines )
out << i << std::endl;


in here gives an fail/

i am using dev c++
i didn't understand why.
aydo000 wrote:
i am using dev c++

That's why. You'll need a C++11 compliant compiler for that line to work. Otherwise, you can use iterators.
Last edited on
That's why. You'll need a C++11 compliant compiler for that line to work. Otherwise, you can use iterators.

if i want to combine this programs.for example

input.txt

stack
kcats
stacka
astack
kcats
overflow
ooverflow
overflowo
Abc
ABC

output.txt
stack
stacka
overflow
ooverflow
Abc

how can we write ?
You already have #include <set> in the code that you posted; now use std::set<>.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <set>
#include <cctype>
#include <algorithm>
#include <sstream>

std::string make_key( std::string str )
{
    for( char& c : str ) c = std::tolower(c) ; // 2011

    // for( std::size_t i = 0 ; i < str.size() ; ++i ) // 1998
    //     str[i] = std::tolower( str[i] ) ;

    std::sort( str.begin(), str.end() ) ;
    return str ;
}

bool inserted( std::set<std::string>& anagram_set, const std::string& str )
{ return anagram_set.insert( make_key(str) ).second ; }

int main()
{
    std::istringstream input_file( "stack\n kcats\n stacka\n astack\n kcats\n overflow\n"
                                    "ooverflow\n overflowo\n Abc\n ABC\n" ) ;

    std::set<std::string> anagram_set ;

    std::string word ;
    while( input_file >> word )
        if( inserted(anagram_set,word) ) std::cout << word << '\n' ;
}

http://coliru.stacked-crooked.com/a/1308465658964593
Topic archived. No new replies allowed.