What makes a vector so popular and useful in C plus plus?

1 Answer

Answer :

A vector should always be considered the default containerobject unless you have good reason to use a more specialisedcontainer (such as a map, set or list).A vector is essentially a variable-length array whichautomatically grows to accommodate new elements. If you require afixed-length array, consider using std::array rather thanstd::vector or built-in array (C-style array).As well as encapsulating the array and its current length, avector also encapsulates a reserve to allow moderate expansionwithout reallocating the array upon every insertion. When thereserve is depleted, reallocation may occur however the optimumgrowth rate upon reallocation is around 160% which gives areasonable balance between performance and memory consumption. Youmay set your own reserve if you can predict how many insertions youneed to cater for, but in many cases there is very little to begained by doing so.Like all other standard-library containers, a vector is also aresource handle. That is, when a vector falls from scope, theelements within it are destroyed automatically. However, when theelements are pointers, the objects they refer to are not destroyedbecause pointers are not objects and therefore do not havedestructors. The majority of vectors we use contain pointers,particularly when working with polymorphic objects (where we referto a common base of the objects rather than the objectsthemselves). However, if a vector of pointers falls from scopebefore we destroy the objects it referred to, we create a resourceleak.To overcome this, we can easily populate vectors with smartpointers which are themselves resource handles with their owndestructors. When the vector of smart pointers falls from scope,the smart pointers are destroyed automatically, which in turnautomatically destroys the objects they referred to -- unless thesmart pointer is a shared pointer in which case the object is onlydestroyed when the final reference falls from scope. Either way, weeliminate the risk of creating a resource leak at little to no cost(shared pointers incur some cost due to reference counting, butthat cost is negligible compared to alternative solutions).Being a resource handle, vectors implement the move semanticthus making it possible to pass and return vectors by valueefficiently and at minimal cost. Built-in arrays, on the otherhand, cannot be passed by value at all because arrays decay topointers (hence we cannot implicitly copy a built-in array); we canonly pass them by reference. However, because of the implicit decayto a pointer, passing a built-in array by reference is actuallyslightly less efficient than passing a vector by reference becausethe pointer alone does not tell us how many elements it refers to,so we also need to pass the array size. A vector carries allinformation with it, so passing the reference alone suffices.

Related questions

Description : Plant quarantine method is useful only when a. The pathogen is seed transmitted b. The pathogen is wind transmitted c. The pathogen is soil inhabitants d. The pathogen is vector tramissible

Last Answer : a. The pathogen is seed transmitted

Description : Plant quarantine method is useful only when pathogen is — a. Wind borne b. Seed transmitted c. Soil inhabitant d. Vector transmissible

Last Answer : b. Seed transmitted

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

Last Answer : The vector's push_back() member inserts one or more new elementsat the end -- the back -- of the vector. If there are insufficientunused elements available, the underlying array is ... after which the new elements are appended. Alliterators into the vector are rendered invalid after areallocation.

Description : What are the most useful tools and/or fun games for the TI-83 Plus?

Last Answer : For games, I liked tetris and the built-in Puzzle Pack. There are apps to solve specific problems (e.g you use quadriat to solve problems with quadratic) . TiCalc has an amazing collection of ... memory on the TI-83 (because then the calculator becomes really slow and it takes forever to graph)

Description : Which of the following therapies would be most useful in the management of severe hypercalcemia? (a) Amiloride plus saline infusion (b) Furosemide plus saline infusion (c) Hydrochlorothiazide plus saline infusion (d) Mannitol plus saline infusion (e) Spironolactone plus saline infusion

Last Answer : Ans: B

Description : Define the useful load: a. traffic load plus dry operating mass b. traffic load plus usable fuel mass c. dry operating mass plus usable fuel load d. that part of the traffic load which generates revenue

Last Answer : a. traffic load plus dry operating mass

Description : Define the useful load: a. traffic load plus dry operating mass b. traffic load plus usable fuel mass c. dry operating mass plus usable fuel load d. that part of the traffic load which generates revenue

Last Answer : a. traffic load plus dry operating mass

Description : What makes Palin so darn attractive to us, and why is she so popular?

Last Answer : Whom are you calling “us”? I find her entirely repellent.

Description : What makes Anime Roleplaying so popular?

Last Answer : I bet masturbation is that communities #2 activity. Edit: My only guess would be the numbers are somehow skewed to a specific demographic.

Description : Top Gear is almost certainly the BBC's most successful export, why do you think it is so much more popular than other motoring shows, what makes it stand out from the crowd?

Last Answer : It's become more of an entertainment show now, rather than a motoring one. Crazy stunts, journeys,tasks and record attempts or whatever, those are the things that make it successful. On saying that ... that are only affordable to the multi-millionares. I still love it Top Gear is great great fun.

Description : Facebook users - did this most recent move on Facebook's part (Trending) make any sense? To me, it makes it far less useful.

Last Answer : answer:When you hover on the trending topic, it displays a balloon with a fuller explanation. I hadn't really noticed the change and have never found the Trending stuff particularly useful. There was that ... news. The change may be related to that but the vagaries of FB are always a mystery.

Description : Software is not only the basic requirement of a computer system, it makes a computer more powerful and useful. -Technology

Last Answer : 1. Software is a set of programs that tells the computer to perform a task.2. Computer system divides software into two major categories:(a) System software(b) Application software

Description : The addition of high proportions of maganese makes steel useful in making rails or railroads because manganese useful in making rails or railroads bec

Last Answer : The addition of high proportions of maganese makes steel useful in making rails or railroads because manganese useful in ... ,iii C. ii,iv D. I,iii,iv

Description : The addition of high proportions of maganese makes steel useful in making rails or railroads because manganese useful in making rails or railroads bec

Last Answer : The addition of high proportions of maganese makes steel useful in making rails or railroads because ... . can show highest oxidation state of +7

Description : What propertys of the materials used in computer chips makes them useful as switched that turn electricity on and off?

Last Answer : The silicon is a semiconductor and can be doped to control itsconductivity and create the junctions needed to make diodes andtransistors. These diodes and transistors can act as switches andlogic gates ... beused to attach wires or metal beads so that the chip can beconnected to external circuits.

Description : What property of liquid crystals makes them useful for displays?

Last Answer : Liquid crystals can rotate the angle of polarization of light bydifferent amounts depending on the strength of an electric fieldacross them. This allows them to be used in high speed opticalswitches controlling the amount of light visible in pixels of adisplay.

Description : __________ Nature of hypo (sodium thiosulphate) makes it useful in photography. (A) Oxidising (B) Reducing (C) Complex forming (D) Photochemical

Last Answer : (C) Complex forming

Description : What characteristics of Read Only memory ROM, makes it useful? 1) ROM information can be easily updated. 2) ROM provides very large amounts of inexpensive Data storage. 3) Data in ... remains there even without electrical power 4) ROM chips are easily Swapped between different brands of computers

Last Answer : 3) Data in ROM is Nonvolatile, that is , it remains there even without electrical power

Description : What do you think makes the Military such a popular career choice for the men of the British Monarchy?

Last Answer : Well, it is the only course open to second sons other than the clergy, right?

Description : Can anyone tell me what makes a blog popular?

Last Answer : 1. Write interesting, useful, and original posts. 2. Write a lot of them. 3. Get other people to link to the posts they find to be the most interesting and useful. 4. Write some more interesting and ... back later to read more, and link their friends to you. 5. Repeat from #1 as often as possible.

Description : Value education makes a student: (A) Good citizen (B) Successful businessman (C) Popular teacher (D) Efficient manager

Last Answer : Answer: A  Value education is the process by which people give values to others. It can be an activity that can take place in any organisation during which people are assisted by others, who may ... dialogue where pe ople are gradually brought to their own realisation of what is good be haviours.