Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On different ways of filling a vector

Tags:

c++

stdvector

I can think of three ways of filling a std::vector

Suppose we have

vector<int> v(100, 0);

Then I want it to hold (1, 1, 1). We can do:

v.clear();
v.resize(3, 1);

Or

v = vector<int>(3, 1);

And I learned another approach:

vector<int>(3, 1).swap(v); 

First question is: Is any of them the best approach?

Second question: suppose v was declared outside the main function. According to this answer, the memory will be allocated in the data segment. If I use the second or third approach, will the memory be allocated on the stack?

like image 958
kunigami Avatar asked Jan 11 '11 19:01

kunigami


2 Answers

How about you use the member of vector that is there for this task?

std::vector<int> v(100);
v.assign(3, 1); // this is what you should do.
like image 140
etarion Avatar answered Sep 24 '22 01:09

etarion


So, here's the differences and I'll let you decide what is best for your situation.

v.clear();
v.resize(3, 1);

In this case we have marked the vector as cleared. It still holds whatever it allocated in order to hold 100 elements (which can be more than the space necessary for 100 elements). Then we added 3 items with value of 1. All this did was increase the size counter and reset 3 values, the underlying memory is still the same size.

v = vector<int>(3, 1);

This does pretty much the same thing except that an extra vector temporary is created and instead of there being intermittent places where the counter is 0 and then 3 with some values, it simple copies the counter size and then does an operation similar to a memcpy to copy over the 3 elements. Size of underlying memory allocated for v is still enough to hold 100 integers.

vector<int>(3, 1).swap(v); 

This one is significantly different. In this case we create a temporary vector that holds 3 elements that are all initialized to 1. Theoretically it could still have enough memory reserved for 100 elements but the chances are it has much less. Then we swap this vector with our own and let the temporary get destroyed. This has the added benefit of clearing out any extra memory allocated by our old vector that wasn't in the temporary. The way this works is that the two vectors (our v and the temporary) swap more than just counters and values, they also swap buffer pointers.

This is the only way to shrink a vector.

like image 37
Edward Strange Avatar answered Sep 24 '22 01:09

Edward Strange