0

I have try to develop an algorithm and in some times i should add multiple elements of a vector to another vector's end. Is there a way to proceed this in a constant time?

Also, is there a way for another data structures(such as arrays and iterators) to do that. I am not be restricted to use the vector.

//O(n) complexity

void add_to_vec( const std::vector<int>& src, std::vector<int & dest, int start, int n )
{
for( int i = 0; i < n; i++ ) {
dest.push_back( src[start + i] );
}
}
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 1
    No, you cannot add (in c++ terminology, `push_back()`) O(n) elements in O(1) time on a non-parallel machine. – lorro Nov 05 '22 at 16:34
  • There is no way to do an operation involving a variable number of elements in constant time. – john Nov 05 '22 at 16:34
  • But you can add multiple elements to a vector in a single line of code, but it remains an O(n) operation. – john Nov 05 '22 at 16:36
  • You can use std::vector's insert. But if you add n elements that would still be an O(n) operation. – Pepijn Kramer Nov 05 '22 at 16:38
  • I think you are perhaps confused about what O(n) and O(1) (or constant time) mean. If so then please rephrase your question without these terms. – john Nov 05 '22 at 16:38
  • What if you always add 5 elements? – user4581301 Nov 05 '22 at 16:39
  • @user4581301 Then that would be constant time. It's the variable part that makes it not constant time. – john Nov 05 '22 at 16:39
  • Okay there is variable part n but, think as linked list if we change the next pointer of the last element of a linked list to another linked list begin node, we append the variable number elements in constant time. Is there a similair mechanism in vectors or another type other than linked list? – green blanket Nov 05 '22 at 16:45
  • So then maybe you simply want a `std::list` instead. [`std::list::splice`](https://en.cppreference.com/w/cpp/container/list/splice) is O(1). (Though of course, it required O(n) time to create the other list in the first place.) Now, if you need O(1) splicing *and* O(1) random access, things get harder. – Nate Eldredge Nov 05 '22 at 16:57
  • @NateEldredge Note that ```list::splice``` is still O(n) when you use it to splice N elements (also via the iterator range version). While the splicing itself is O(1), the method has to loop through the elements to count them and update the list's size – Homer512 Nov 05 '22 at 17:29
  • 1
    @Homer512: True. But it's O(1) when you splice in an entire list (whose size would already be known), which would serve OP's purpose if the N elements to be appended were their own list. – Nate Eldredge Nov 05 '22 at 17:32
  • @john Depends on operation and what *involving* means. E.g. for vector-like container with trivially destructible element type erase *n* elements from the end (shrink) without re/de-allocating can be O(1) if optimizer avoids destructor calls (which are no-op). Similarly for resizing to bigger size (increase by *n*) if there's trivial default constructor. So, these operations involve (destroy/construct) *n* objects but can have O(1) complexity. – YurkoFlisk Nov 05 '22 at 17:49
  • Why would you think that's possible? A vector needs to allocate storage for the new elements. Then it needs to construct/copy/move elements. How could that possibly be done in O(1) time for an arbitrary number of incoming elements? – Jesper Juhl Aug 07 '23 at 18:43

0 Answers0