Bharadwaj Gali
Bharadwaj Gali

Reputation: 103

Does inserting an element in vector by re-sizing the vector every time takes more time?

I got a decision making problem here. In my application, I need to merge two vectors. I can't use stl algorithms since data order is important (It should not be sorted.).

Kindly help me to choose the proper one. And if there is any better approach or simpler techniques (like stl algorithms), or easier container than vector, please post here. Thank you.

Upvotes: 0

Views: 507

Answers (3)

Matt Timmermans
Matt Timmermans

Reputation: 59174

You shouldn't be focusing on the resizes. In approach 1, you should use use vector.insert() so you don't actually need to resize the vector yourself. This may cause reallocations of the underlying buffer to happen automatically, but std::vector is carefully implemented so that the total cost of these operations will be small.

The real problem with your algorithm is the insert, and maybe the search (which you didn't detail). When you into a vector anywhere except at the end, all the elements after the insertion point must be moved up in memory, and this can be quite expensive.

If you want this to be fast, you should build a new vector from your two input vectors, by appending one element at a time, with no inserting in the middle.

Upvotes: 2

Bryan Koch
Bryan Koch

Reputation: 13

Depending on your actual setup (like if you're adding object pointers to a vector instead of copying values into one), you might get significantly faster results using a std::list. std::list allows for constant time insertion which is going to be a huge performance overhead.

Doing insertion might be a little awkward but is completely do-able by only changing a few pointers (inexpensive) vs insertion via a vector which moves every element out of the way to put the new one down.

If they need to end up as vectors, you can then convert the list to a vector with something like (untested)

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

Upvotes: 0

user3453575
user3453575

Reputation: 71

Doesn't look like you can do this in better time complexity than O(n.log(n)) because removing duplicates from a normal vector takes n.log(n) time. So using set to remove duplicates might be the best thing you can do. n here is number of elements in both vectors.

Upvotes: 0

Related Questions