0

Hey could someone please point me to a website which shows you how to remove duplicates of a list container of a class that have the same name, but change the int to be an average of both values in the class?

class product{
           string name;
           float rating;
}

so if the data read was "iPod 5 iPod 9" ipod being the name and the number after it the rating, how would i search for and remove the second iPod entry and make the first rating change to rating = 5+9/2

4
Contributors
9
Replies
10
Views
8 Years
Discussion Span
Last Post by siddhant3s
0

Sort by name order, do a total of the ratings.

Remove all occurences of the name and replace with name and rating - for all names.

0

cool thanx, could you please show me a link to some code? ive pretty new to containers and cant find any examples relating to lists of classes

0

cool thanx, could you please show me a link to some code? ive pretty new to containers and cant find any examples relating to lists of classes

[humor]
Yeah sure, why not. imthwee is quite good at it. ;)
[/humor]

0

cool thought i might give maps ago :P just one little problem

is it possible to have 3 types of data in a map container? having the name as the key, the rank as the second, and the number of times it has been ranked as the third?

pair<string, int> readin;
	pair<mapit, bool> ret;
	getline(ins, readin.first);
	ins >> readin.second;
	ret = product.insert(readin);
	ins.ignore();
	if (!ret.second)
	{
	//cerr<<"Duplicate entry."<<endl;
                // add readin.second to previous instance of name		
	}

is it possible if the name is a duplicate, the add the second to the previous instance of that name... then devide it by number of times its been added to find the average?

0

maps only take two values. For your purposes one value can be a structure that contains the two numbers you want.

map<vector<int>, std::string> theList;
0

Consider a map of pair std::map<keyType, std::pair<int,int> > theMap; The solution is more or less like post#8 but pair would be more efficient.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.