I am trying to populate a vector of strings and then remove any duplicates within that vector. Here is what I have so far. I am getting an error saying: Error 2 error C2064: term does not evaluate to a function taking 2 arguments
.

void removeDup() {

    vector <string> list;
    ifstream inputFile;
    inputFile.open("database.txt");
    string myString;
    int count;

    if(inputFile.is_open()) {
        while(!inputFile.eof()) {
            getline(inputFile, myString);
            list.push_back(myString);
        }
        std::sort(list.begin(), list.end());
        list.erase(std::unique(list.begin(), list.end(), list.end()));

    inputFile.close();

}

Edited 3 Years Ago by tensity

Hello,

I believe what you're doing here is wrong:

list.erase(std::unique(list.begin(), list.end(), list.end()));

It expects a function (boolean) rather than the end of the list (vector). In this case, you could try this:

bool compare (string i, string j) {
  return (i==j);
}



list.erase(std::unique(list.begin(), list.end(), compare);

Source: Click Here

You just have a misplaced bracket. You have this:

list.erase(std::unique(list.begin(), list.end(), list.end()));

It should be this

list.erase(std::unique(list.begin(), list.end()), list.end());

Did you spot the difference :)

I think you get this compilation error because, in your version, the compiler is trying to use list.end() as a comparator (and it expects the comparator to take two arguments).

while(!inputFile.eof()) 
{
    getline(inputFile, myString); // what happens if this fails?
    list.push_back(myString); // push_back the previous line one more time?
}

Instead, write:

while( getline( inputFile, myString ) ) list.push_back(myString) ; 

Alright. So I have changed a few things around, and finally I have it working. the only question that I have at this point is if there is a more efficient way of doing what I am doing. I am pulling in a bunch of lines from a text file and storing it in a vector, sorting the vector, etc., and then outputing to a new text file.

Edited 3 Years Ago by tensity

if there is a more efficient way of doing what I am doing.

O(N) time, O(N) space; preserves the order.

But the O(N) space for std::unordered_set<> is larger than the O(N) space for std::vector<> if the percentage of duplicate lines is not large.

#include <fstream>
#include <string>
#include <unordered_set>

void copy_unique_lines( const char* srce_file, const char* dest_file )
{
    std::ifstream fin(srce_file) ;
    std::ofstream fout(dest_file) ;

    // http://en.cppreference.com/w/cpp/container/unordered_set
    std::unordered_set<std::string> unique_lines ;  

    std::string line ;
    while( std::getline( fin, line ) )
        if( unique_lines.insert(line).second ) fout << line << '\n' ;
}
This article has been dead for over six months. Start a new discussion instead.