Thread: Remove duplicates and save to txt

  1. #1
    Registered User
    Join Date
    Sep 2015

    Remove duplicates and save to txt

    Hello everybody, i've been trying to write a code that must open a .txt file with a huge vector (int only), then it will remove duplicates and save it again to another .txt file. Problem is when i run it, i only get an empty .txt file. Can you guys help me figure out what am i missing here?

    int i=0;
    int main()
        using namespace std;
        ifstream in_file("Ifile.txt");
        ofstream out_file("Ofile.txt");
            std::vector<int> vec;
            for(i; in_file >> i;i++)
                std::unique(vec.begin(), vec.end()),
                std::sort(vec.begin(), vec.end());
                cout << i;
                auto v = vec;
                std::copy(begin(v), end(v), std::ostream_iterator<int>(std::cout, "Ofile.txt"));

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Since you intend to use standard generic algorithms and iterators, perhaps you should do so all the way, e.g.,
    auto vec = std::vector<int>(std::istream_iterator<int>(in_file), std::istream_iterator<int>());
    sort(begin(vec), end(vec));
    vec.erase(unique(begin(vec), end(vec)), end(vec));
    copy(begin(vec), end(vec), std::ostream_iterator<int>(std::cout, "\n"));
    I switched the sort and erase+unique calls because you probably want to sort so that unique will remove duplicates that were not originally adjacent in the file. The second argument to the ostream_iterator is a delimiter, not a filename.

    If you do want to use i and the loop, then you probably should declare i just before the loop, then use a while loop whose sole purpose is to push_back into the vector. You should not be doing the sorting, erasing, and printing within the loop.
    Last edited by laserlight; 04-18-2016 at 09:42 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2015
    Oh, thanks a lot laserlight, it was really helpful!

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    If you do not care about the order of the output, consider using std::set.
    Just reading in the elements and then printing them will discard duplicates.
    The catch is that the result will be sorted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Remove duplicates items in list using GLib
    By Buda in forum C Programming
    Replies: 10
    Last Post: 08-27-2014, 06:19 AM
  2. Two int Arrays, remove duplicates from second
    By Sorin in forum C Programming
    Replies: 5
    Last Post: 04-03-2013, 03:51 AM
  3. Strings and Structs (remove duplicates)
    By christianB in forum C Programming
    Replies: 44
    Last Post: 07-25-2011, 09:01 PM
  4. recursive remove duplicates from a string
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-20-2009, 02:02 AM
  5. Really basic remove duplicates from array
    By Chris Fowler in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2002, 10:35 PM