Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
here goes my code.. i have explained it in the comments. please do refer the codes and the comments inside it. i have also showed you the output and the desired output of the program in the comments.please do refer it.
(thanks in advance).


C++


C#
vector<int>a = {1,1,11,11,12,13,14,15,16,16,3};
	vector<int>b(9, 0);
	auto st = a.begin();
	auto ptr=unique(a.begin(), a.end(),less<int>());	//it should remove the elements whose prev. element is less then itself. but its not working the desired way.
	while (st != ptr)
	{
		cout << *st << " ";
		st++;
	}
	return 0;
	 /*
		output shown is 
		1 1 
		desired output is :
		1 11 16 3
	 */


What I have tried:

i have tried the above code and also reading some of the answers from the stack-overflow forums .. it states that there is a unexpected behaviour of unique and remove algorithms of the stl library.
Posted
Updated 5-Jun-16 11:52am

1 solution

The apparent problem is the third parameter, binary predicate. It should return true if the elements are to be considered equal. Two array elements are passed to this predicate function, and you should not make any assumption on what are they. The purpose of this predicate is obvious though. You return true when one element is less than another, and it means that one of these elements is removed, because the interpretation of the predicate is "the elements are equal, so one of the two elements is redundant, so it should be removed".

Please see: unique — C++ Reference.

Infer the consequences by yourself, or use the debugger to see how the algorithm works; for this purpose, create your own predicate function with two elements, pass it the the algorithm function and set a break point on your predicate function. Possible implementations are shown here: std::unique — cppreference.com.

Infer the consequences by yourself.

What to do instead? First of all, you need to formulate what you want to achieve precisely. Neither your description nor the sample of data can explain it. It should be mathematically strict formulation. What you want to achieve, by your example, cannot be considered as anything matching the concept of uniqueness. For example, 14 and 16 are already unique, but you want to remove them. What is it? Anyway, no matter what it is, the algorithm should be different. By the way, what's wrong with writing your own algorithm?

By the way, maybe you can also consider different approach. How do you obtain your input data? You could considering filtering data as you add elements to the vector. If your goals really was uniqueness, the solution would be very obvious. In your case, it depends on what you want to achieve.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900