STL Vector

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瓦力冫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值