【发布时间】:2016-04-28 03:29:16
【问题描述】:
我在这里遇到了一个决策问题。在我的应用程序中,我需要合并两个向量。我不能使用 stl 算法,因为数据顺序很重要(不应该排序。)。
两个向量都包含有时相同或在最坏情况下相差 75% 的数据。
-
目前我对两种方法感到困惑,
Approach 1: a. take an element in the smaller vector. b. compare it with the elements in bigger one. c. If element matches then skip it (I don't want duplicates). d. If element is not found in bigger one, calculate proper position to insert. e. re-size the bigger one to insert the element (multiple time re-size may happen). Approach 2: a. Iterate through vectors to find matched element positions. b. Resize the bigger one at a go by calculating total size required. c. Take smaller vector and go to elements which are not-matched. d. Insert the element in appropriate position.
请帮我选择合适的。如果有任何更好的方法或更简单的技术(如 stl 算法),或者比向量更简单的容器,请在此处发布。谢谢。
【问题讨论】:
-
使用
set怎么样?您可以非常轻松地完成方法 1 中的所有步骤。 -
任何改进建议都需要关于您的数据的真实信息。例如,什么决定了订单 - 即您如何计算要插入的位置?数组中值的类型和范围是什么?
-
嗨@paddy。数据是格式化的字符串。数据来自两个不同的 xml 文件。由于存在分组,因此顺序很重要,并且位置计算基于最后插入的位置和当前元素位置。但是,元素范围并不太大。最大可以达到 5000 个元素。