简体   繁体   中英

Copy another vector for a smaller size

I have 2 vectors. I want the second vector to copy the first vector for the size of n which is less than the length of the first vector. (the second vector length should be n too)

I tried doing this by a loop:

for (int i = 0; i < n; ++i)
{
    //secVector[i] will equal firstVector[i] and n is less than fristVector length
}

but the time complexity of this is O(n) and it takes a lot of time in large lengths, I wonder if there is any function could do this faster.

This cannot be done with std vector.

There are immutable vectors where this can be done in logarithmic time, such as https://sinusoid.es/immer/ - this uses wide B trees and copy on write to give near-vector performance with O(1) copy and O(lg n) slice.

Such structures are considered exotic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM