独特的不按预期工作(C ++ stl) [英] Unique not working as per desired(C++ stl)

查看:92
本文介绍了独特的不按预期工作(C ++ stl)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的代码..我在评论中解释了它。请参考其中的代码和注释。我还在评论中向您展示了程序的输出和所需的输出。请参考它。

(提前感谢)。





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).






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
	 */





我尝试过:



i已经尝试了上面的代码,并从堆栈溢出论坛中读取了一些答案..它表明存在一个独特的意外行为并删除stl库的算法。



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.

推荐答案

明显的问题是第三个参数,二元谓词。如果元素被认为是相等的,它应该返回 true 。两个数组元素被传递给这个谓词函数,你不应该对它们是什么做出任何假设。这个谓词的目的很明显。当一个元素小于另一个元素时返回true,这意味着删除了其中一个元素,因为谓词的解释是元素相等,所以两个元素中的一个是多余的,所以应该删除它 。



请参阅: unique - C ++ Reference



自己推断结果,或使用调试器查看算法的工作原理;为此,使用两个元素创建自己的谓词函数,将算法函数传递给它,并在谓词函数上设置断点。可能的实现如下所示: std :: unique - cppreference.com



自己推断后果。



该怎么做?首先,你需要制定你想要达到的目标。您的描述和数据样本都无法解释。它应该是数学上严格的配方。通过您的示例,您想要实现的目标不能被视为与唯一性的概念相匹配的任何内容。例如,14和16已经是唯一的,但您想要删除它们。它是什么?无论如何,算法应该是不同的。顺便说一下,编写自己的算法有什么问题?



顺便说一下,也许你也可以考虑不同的方法。你如何获得输入数据?在向向量添加元素时,可以考虑过滤数据。如果您的目标确实是唯一性,那么解决方案将非常明显。在你的情况下,这取决于你想要达到的目标。



-SA
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


这篇关于独特的不按预期工作(C ++ stl)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆