I have to create a generic function that removes all duplicates in a vectorInt a vectorBook which is a class i created that has getYear and getName functions. I'm not sure how to make the the function because the vectorInt is compared because the Book gets compared with getName and getYear. Int is compared on one level while Book is compared on two levels.
template<class T> vector<T> removeDuplicates(vector<T> n){
for(unsigned int i = 0; i < n.size();i++){
T current = n.at(i);
for(unsigned int j = i + 1; j < n.size(); j++){
T compare = n.at(j);
if(current == compare)
n.erase(n.begin() + j);
}
}
return n;
}
Thanks for the help
EDIT:
Tried using this
template <class T> std::vector<T> removeDuplicates(std::vector<T> vec)
{
std::sort( vec.begin(), vec.end() );
vec.erase( std::unique( vec.begin(), vec.end() ), vec.end() );
return vec;
}
but for books i keep getting an error
class Book {
public:
Book();
Book(std::string, int);
int getYear() const {
return year;
}
std::string getName() const {
return name;
}
bool operator==(Book const &);
private:
std::string name;
int year;
};