Introduction


We speak of a Vector as a container because it contains other objects. All objects in a container must have the same type.

To use a Vector, we must include the appropriate header.

#include <vector>

Using std::vector;

In the case of  Vector, we must say what type of objects the Vector will contain. We specify the type by putting it between a pair of angle brackets following the template’s name:

vector<int> ivec;     //ivec holds objects of type int

vector<Sales_item>   //holds Sales_item


Defining and Initializing vector


Table 1.1 Ways to Initialize a vector

  vector<T> v1;

  Vector that holds objects of type T; Default constructor v1 is empty

  vector<T>v2(v1);

  V2 is a copy of v1

  vector<T>v3(n,i);

  V3 has n elements with value i

  vector<T>v4(n);

  V4 has n copies of a value-initialized object

 

Example:

vector<int> ivec1;           //ivec1holds objects of type

vector<int>ivec2(ivec1)      //copy elements of ivec1 into ivec2

vector<string> svec(ivec1);   //error: svec holds strings, not ints

 

We can initialize a vector from a count and an element value. The constructor uses the count to determine how many elementsthe vector should have and uses the value to specify the value each of those elements will have:

 

Example:

vector<int> ivec4(10,-1)     //10 elements, each initialized to -1

vector<string> svec(10,”hi!”); //10 strings, each initialized to “hi!”

 


vector(s) grow dynamically


A central property of vector(s) is that they are required to be implemented so that it is efficient to add elements to them at run time. Because vector(s) grow efficiently, it is usually best to let the vector grow by adding elements to it dynamically as the element values areknown.

 

Reason: To support fast random access,vector elements are stored contiguously each element is adjacent to the previous element. Given that element are contiguous, let’s think about what happens when we add an element to a vector: If there is no room in the vector for the new element, it cannot just add an element somewhere else in the memory because the elements must be contiguous for indexing to work. Instead, the vector must allocate new memory to hold the existing elements plus the new one,copy the old elements from the old location into the new space, add the new element,and deallocate the old memory. If vector did this memory allocation and deallocation each time we added an element, then performance would be unacceptably slow.

The way vector(s) achieve fast allocationis by allocating capacity beyond what is immediately needed. The vector holds this storage in reserve and uses it to allocate new elements as they are added.Thus, there is no need to reallocate the container for each new element.


Operations on vector(s)


  v.empty()

  Returns true if v is empty; otherwise returns false

  v.size()

  Returns number of elements in v

  v.push_back(t)

  Adds element with value t to the end of v

  v[n]

  Returns element at position n in v

  v1 = v2

  Replaces elements in v1 by a copy of elements in v2

  v1 == v2

  Returns true if v1 and v2 are equal

  !=,<,<=,>,and >=

  Have their normal meanings

 


The size of a vector


To use size_type, we must name the type in which it is defined. A vector type always includes the element type of thevector:

 

Example:

vector<int> ivec(10);

for(vector<int>::size_type ix = 0; ix!= ivec.size(); ++ix)

{

   ivec[ix]= 0;

}

 


Adding Elements to a vector


Use push_back

Example:

 

vector<string> text;

text.push_back(“hello world!”);


 ​​ http://www.waitingfy.com/?p=411​