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

Member Avatar for iamthwee

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.

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

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]

Member Avatar for iamthwee

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

I think you need to get out more kiddo!

___________________
I think you could put something useful together from:

http://www.daniweb.com/code/snippet900.html

<map> doesn't allow duplicates. And with map you don't even need that structure, unless it contains other things that you didn't post here.

Example code here.


std::set container also don't allow duplicates and are sorted.

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?

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;
Member Avatar for iamthwee

No a map has two entries.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.