removing duplicates

closed account (42ApX9L8)
My name is James. I am still a beginner programmer but am taking intermediate programming classes. Right now the class I am taking is Intermediate C++ and I am having some issues with a very remedial issue.

Here is my issue. I have to use an ifstream to read in 2 files with a series of numbers in them. That isn't the issue. The issue is that I can't figure out how to use a sort algorithm or how to remove the duplicates.

Here is the code I have:

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
   ifstream numbers("num1.txt");
   ifstream numbers2("num2.txt");

   string line;

   if(numbers.fail() || numbers2.fail())
   {
        cout << "Could not open file." << endl;
	exit(1);
   }
   else
   {
        while(!numbers.eof() || !numbers2.eof())
        {
	    getline(numbers,line);
	    cout << line << endl;
	    getline(numbers2,line);
	    cout << line << endl;
        }
   }

   return 0;
}


What the files contain:
num1.txt:
-10.5
-1.8
3.5
6.3
7.2

num2.txt:
-1.8
3.1
6.3

Here is the output:
-10.5
-1.8
-1.8
3.1
3.5
6.3
6.3
6.3
7.2
7.2

Can anybody help me with a code to sort and remove duplicates?
Thanks in advance.
You should read all numbers from files and merge it into an array or a vector or STL list.Then use STL methods for removing duplicates and sorting.
STL list is better for this example.Because it has own sort and unique method

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 <fstream>
#include <list>
#include <cstdlib>

using namespace std ;

int main( )
{
    ifstream first( "num1.txt" ) , second( "num2.txt" );
    list< double >  dList ;
    
    double temp ;
    
    while( !first.eof())
    {
           first >> temp ;
           dList.push_back( temp );
    }
    while( !second.eof())
    {
           second >> temp ;
           dList.push_back( temp );
    }
    dList.sort(); // sort list
    dList.unique(); // remove duplicates
 
    for( list< double >::iterator it = dList.begin(); it != dList.end(); it++ )
         cout << *it << endl;
    system( "PAUSE" );
    return ( 0 ) ;
}
Last edited on
Don't test against ifstream::eof() -- if your file contains anything but a number it will cause an infinite loop.

Always test against ifstream::good() instead.

Also, make sure you actually got a number before appending it to the list.

Finally, it is worth your effort to make a little function to do this stuff:
1
2
3
4
5
6
7
8
9
10
bool load_numbers_from_file( const string& filename, list <double> & xs )
{
    double x;
    ifstream f( filename.c_str() );

    while (f >> x)  // while (f.good())
        xs.push_back( x );

    return f.eof();  // success = did we reach eof()?
}

Now, use it:
1
2
3
4
5
6
7
    list <double> dList;
    if (!load_numbers_from_file( "num1.txt", dList )
    ||  !load_numbers_from_file( "num2.txt", dList ))
    {
        cout << "Could not open file.\n";
        return 1;
    }

Hope this helps.
Last edited on
closed account (42ApX9L8)
Thanks to all. This helped tremendously. I greatly appreciate it.
Topic archived. No new replies allowed.