removing duplicate strings in a vector

Hi, I'm trying to create a database/search engine program and I'm having remove duplicate strings from a vector. I'm mostly just trying to make it so that if 2 or more movie have the same title in the database, it will remove the duplicates and just print out one copy of the movie console. I tried using the unique() command, but it doesn't seem to work. Does anyone have any advice on what I should do?

code:
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
#include <iterator>
#include "Movie.h"

using namespace std;


//removes duplicate movie titles

template <typename T>
void remove_duplicates(vector<T>& vec)
{
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
}

//-------------------------------------
//Main program ------------------------
//-------------------------------------

int main () {
	vector<Movie> movies;
	ifstream fin("CS172_Spring2014_Movie_inputs.txt");
	ofstream fout("movie.txt");

	//read in file until end of file

	while ( !fin.eof() ) {
	string data;
	Movie movie;

	getline(fin, data, '\n'); //reads data from file
	movie.setTitle(data);     //sets next data to movie title		 
	
	getline(fin, data, '\n');  
	movie.setDirector(data);	//sets next data to Director
	
	getline(fin, data,'\n');
	int year = atoi(data.c_str());  //converts data string to int
	if(year==0)	{
		Movie_Rating rating = movie.rate_from_string(data); //converts and assigns string data to Movie_Rating type object
		movie.setRating(rating);							//sets next data to movie rating
	}
	else {
		movie.setYear(year);            //sets next data to year
	
	}
	
	getline(fin, data, '\n');
	year = atoi(data.c_str());
	if(year==0)	{
		Movie_Rating rating = movie.rate_from_string(data); //converts and assigns string data to Movie_Rating type object
		movie.setRating(rating);			
	}else {
		movie.setYear(year);
	}
	//sets next data to movie rating
	
	getline(fin, data, '\n');
	movie.setURL(data);      //sets next data to imdb URL
	
	getline(fin,data,'\n');
		//read actors in file until $$$$
		while( data != "$$$$"){
			movie.addActor(data); // sets next data to actors until $$$$ is encountered
			getline(fin,data,'\n');
		}
		movies.push_back(movie);
	}

        remove_duplicates(movies);   //removes duplicate movie titles        
	
	for(int i=0; i< movies.size(); i++)	{
	movies[i].output(cout);     //print to screen
	movies[i].output(fout);     //print to movie.txt file
	}
	
	return 0; //program done
}
Last edited on
The UUID for movies is the EIDR, not the title. But that's mostly just me being a pedantic jerk, my apologies. You aren't using the "std::unique()" function correctly. What you need for the third parameter is a homebrew function that evaluates the range given and returns a Boolean value (true || false). Our sites reference has a pretty clear example of how this is used: http://www.cplusplus.com/reference/algorithm/unique/?kw=unique

Ok, I completely removed the removed_duplicates() subprogram and replaced with
1
2
3
4
	unique(movies.begin(), movies.end(), [](Movie const &a, Movie const &b) -> bool
	{
		return a.getTitle() == b.getTitle();
	});


It seems to only remove some duplicates, but not for all of the movie titles.

EDIT: nvm the semantic error is caused by inconsistencies in the database.
Last edited on
Topic archived. No new replies allowed.