answersLogoWhite

0


Best Answer

A vector should always be considered the default container object unless you have good reason to use a more specialised container (such as a map, set or list).

A vector is essentially a variable-length array which automatically grows to accommodate new elements. If you require a fixed-length array, consider using std::array rather than std::vector or built-in array (C-style array).

As well as encapsulating the array and its current length, a vector also encapsulates a reserve to allow moderate expansion without reallocating the array upon every insertion. When the reserve is depleted, reallocation may occur however the optimum growth rate upon reallocation is around 160% which gives a reasonable balance between performance and memory consumption. You may set your own reserve if you can predict how many insertions you need to cater for, but in many cases there is very little to be gained by doing so.

Like all other standard-library containers, a vector is also a resource handle. That is, when a vector falls from scope, the elements within it are destroyed automatically. However, when the elements are pointers, the objects they refer to are not destroyed because pointers are not objects and therefore do not have destructors. The majority of vectors we use contain pointers, particularly when working with polymorphic objects (where we refer to a common base of the objects rather than the objects themselves). However, if a vector of pointers falls from scope before we destroy the objects it referred to, we create a resource leak.

To overcome this, we can easily populate vectors with smart pointers which are themselves resource handles with their own destructors. When the vector of smart pointers falls from scope, the smart pointers are destroyed automatically, which in turn automatically destroys the objects they referred to -- unless the smart pointer is a shared pointer in which case the object is only destroyed when the final reference falls from scope. Either way, we eliminate the risk of creating a resource leak at little to no cost (shared pointers incur some cost due to reference counting, but that cost is negligible compared to alternative solutions).

Being a resource handle, vectors implement the move semantic thus making it possible to pass and return vectors by value efficiently and at minimal cost. Built-in arrays, on the other hand, cannot be passed by value at all because arrays decay to pointers (hence we cannot implicitly copy a built-in array); we can only pass them by reference. However, because of the implicit decay to a pointer, passing a built-in array by reference is actually slightly less efficient than passing a vector by reference because the pointer alone does not tell us how many elements it refers to, so we also need to pass the array size. A vector carries all information with it, so passing the reference alone suffices.

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What makes a vector so popular and useful in C plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the angle between vector a and vector b when mode of vector a plus vector b is equal to mode of vector a minus vector b?

90 degrees


If vector c equals vector a plus vector b under what circumstances does c equal a minus b?

When b is zero.


If vector a and b are unit vectorprove that Vector a a 2 plus vector b b 2 2 vector a plus vector b ab 2?

Unfortunately, limitations of the browser used by Answers.com means that we cannot see most symbols. It is therefore impossible to give a proper answer to your question. Please resubmit your question spelling out the symbols as "plus", "minus", "times", "divided by", "equals".


If vector B is added to vector A under what circumstances does the resultant vector A plus B have magnitude A plus B. under what circumstances does the resultant vector equals to zero?

if b + a , since a+b equals b + a due to it being commutative . it shud have the same magnitude and direction


Represent a stack in c plus plus?

Use a vector with a base class type. Any objects derived from the base class can be pushed and popped from the vector just as you would from a stack.


Is it possible for vector A plus vector B to equal vector A - vector B?

It's impossible as the addition of two vectors is commutative i.e. A+B = B+A.For subtraction of two vectors, you have to subtract a vector B from vector A.The subtraction of the vector B from A is equivalent to the addition of (-B) with A, i.e. A-B = A+(-B).


What is United Mileage Plus and what makes it useful?

United Mileage Plus is the United Airlines' frequent flyer program. What makes it useful is that the miles have no expiration date, but flyers must remain frequent; otherwise, the reward defeats its purpose. Frequent flyers also get unlimited upgrades on domestic flights, along with many other added perks.


Why speed is scalar while velocity is vector?

Speed is scalar (that is, without direction) and velocity is a vector (speed plus direction) by definition in physics.


What does push back() do to a vector in c plus plus?

The vector's push_back() member inserts one or more new elements at the end -- the back -- of the vector. If there are insufficient unused elements available, the underlying array is reallocated with a larger reserve and the existing elements moved to the new allocation, after which the new elements are appended. All iterators into the vector are rendered invalid after a reallocation.


What is the unit vector x plus yj?

It is (x + yj) / |x + yj|


Wap to print table of any number in c plus plus?

#include<iostream> #include<vector> template<typename T> void print_table(std::vector<T> table) { for (auto i : table) std::cout << i << std::endl; } int main() { std::vector<int> table = {1,5,9,3,5,2,9}; print_table (table); }


What does vectoralphabet(26) do in C plus plus?

It doesn't do anything other than to create a compiler error. A vector is a class template thus you must specify the element type in the type declaration. For example, a std::vector<T> is a vector of type T elements. It is assumed you really meant the following: std::vector<char> alphabet (26); This declaration constructs a vector of type char with a length of 26 elements. The elements are default initialised, thus the vector will contain 26 NULL characters (ASCII character code 0).