通过每次重新调整向量大小来在向量中插入元素需要更多时间吗?

时间:2016-04-28 03:29:16

标签: c++ algorithm vector stl

我在这里遇到了决策问题。在我的应用程序中,我需要合并两个向量。我不能使用stl算法,因为数据顺序很重要(不应该对它进行排序)。

  • 两个向量都包含有时相同或最差情况下75%不同的数据。

  • 目前我很困惑b / w两种方法,

    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算法),或容器比矢量更容易,请在此处发布。谢谢。

3 个答案:

答案 0 :(得分:2)

你不应该专注于调整大小。在方法1中,您应该使用vector.insert(),这样您实际上不需要自己调整向量的大小。这可能导致底层缓冲区的重新分配自动发生,但是std :: vector经过精心实施,因此这些操作的总成本很小。

您的算法的真正问题是插入,也许是搜索(您没有详细说明)。当你在除了最后的任何地方进入向量时,插入点之后的所有元素必须在内存中向上移动,这可能非常昂贵。

如果你想要快速,你应该从你的两个输入向量构建一个新的向量,通过一次附加一个元素,而不是插入中间。

答案 1 :(得分:0)

看起来你不能以比O(n.log(n))更好的时间复杂度做到这一点,因为从法向量中删除重复项需要n.log(n)时间。因此,使用set删除重复项可能是您可以做的最好的事情。 这里是两个向量中元素的数量。

答案 2 :(得分:0)

根据您的实际设置(如果您将对象指针添加到向量而不是将值复制到一个),使用std :: list可能会获得明显更快的结果。 std :: list允许恒定时间插入,这将是一个巨大的性能开销。

进行插入可能有点尴尬,但完全可以做到只需更改一些指针(便宜)与通过向量插入的向量,该向量将每个元素移开以放下新元素。

如果他们需要以矢量形式结束,那么您可以将列表转换为类似(未经测试)的矢量

std::list<thing> things;

//efficiently combine the vectors into a list
//since list is MUCH better for inserts
//but we still need it as a vector anyway

std::vector<thing> things_vec;
things_vec.reserve(things.size()); //allocate memory

//now move them into the vector
things_vec.insert(
    things_vec.begin(), 
    std::make_move_iterator(things.begin()), 
    std::make_move_iterator(things.end())
);

//things_vec now has the same content and order as the list with very little overhead