C++Vector

 

C++ Vectors

Constructors

Syntax:

  vector();
       
       
  vector( size_type num, const TYPE &val );
       
       
  vector( const vector &from );
       
       
  vector( input_iterator start, input_iterator end );
       
       

C++ Vectors can be constructed with either:

·                         Nothing - which creates an empty vector,

·                         num and val - puts num copies of val in the vector,

·                         from - creates a vector with the same contents as from, or

·                         start and end - which creates a vector containing elements from start to end.

For example, the following example constructs a vector consisting of five copies of the integer 42.

vector<int> v1( 5, 42 );
   
   

at

Syntax: 
   
   
  TYPE at( size_type loc );
       
       

The at() function returns a reference to the element in the current vector at loc. The at() function is safer than the [] operator, because it won't let you reference items outside the bounds of the vector. For example, consider the following code:

vector<int> v( 5, 1 );
   
   

  
  
   
    
  
  
for( int i = 0; i < 10; i++ ) {
   
   
  cout << "Element " << i << " is " << v[i] << endl;
   
   
}
   
   
  
   
   

This code overrunns the end of the vector, producing potentially dangerous results. The following code would be much safer:

vector<int> v( 5, 1 );
   
   

  
  
   
    
  
  
for( int i = 0; i < 10; i++ ) {
   
   
  cout << "Element " << i << " is " << v.at(i) << endl;
   
   
}
   
   

Instead of attempting to read garbage values from memory, the at() function will realize that it is about to overrun the vector and will throw an exception.

erase

Syntax:

  iterator erase( iterator loc );
       
       
  iterator erase( iterator start, iterator end );
       
       

The erase() function either deletes the element at location loc, or deletes the elements between start and end. The return value is the element after the last element erased. For example:

// Create a vector, load it with the first ten characters of the alphabet
   
   
vector<char> alphaVector;
   
   
for( int i=0; i < 10; i++ )
   
   
  alphaVector.push_back( i + 65 );
   
   

  
  
   
    
  
  
int size = alphaVector.size();
   
   
vector<char>::iterator startIterator;
   
   
vector<char>::iterator tempIterator;
   
   

  
  
   
    
  
  
for( int i=0; i < size; i++ ) {
   
   
  startIterator = alphaVector.begin();
   
   
  alphaVector.erase( startIterator );
   
   

  
  
   
    
  
  
  // Display the vector
   
   
  for( tempIterator = alphaVector.begin(); tempIterator != alphaVector.end(); tempIterator++ )
   
   
    cout << *tempIterator;
   
   
  cout << endl;
   
   
}
   
   

That code would display the following output:

BCDEFGHIJ
   
   
CDEFGHIJ
   
   
DEFGHIJ
   
   
EFGHIJ
   
   
FGHIJ
   
   
GHIJ
   
   
HIJ
   
   
IJ
   
   
J
   
   

pop_back

Syntax: 
   
   
  void pop_back();
       
       

The function pop_back() deletes the last element in the current vector. For example:

vector<char> alphaVector;
   
   
for( int i=0; i < 10; i++ )
   
   
  alphaVector.push_back( i + 65 );
   
   

  
  
   
    
  
  
int size = alphaVector.size();
   
   
vector<char>::iterator theIterator;
   
   
for( int i=0; i < size; i++ ) {
   
   
  alphaVector.pop_back();
   
   
  for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ )
   
   
    cout << *theIterator;
   
   
  cout << endl;
   
   
}
   
   

This code displays the following output:

ABCDEFGHI
   
   
ABCDEFGH
   
   
ABCDEFG
   
   
ABCDEF
   
   
ABCDE
   
   
ABCD
   
   
ABC
   
   
AB
   
   
A
   
   
Related topics:
erase().



   
   

push_back

Syntax: 
   
   
  void push_back( const TYPE &val );
       
       

The push_back() function appends val to the end of the current vector.

insert

Syntax: 
   
   
  iterator insert( iterator loc, const TYPE &val );
       
       
  void insert( iterator loc, size_type num, const TYPE &val );
       
       
  void insert( iterator loc, input_iterator start, input_iterator end );
       
       

The function insert() either:

·                                inserts val before loc, returning an iterator to that element, 
   
   
·                                inserts num copies of val before loc, or 
   
   
·                                inserts the elements from start to end before loc. 
   
   
For example: 
   
   
// Create a vector, load it with the first 10 characters of the alphabet
   
   
vector<char> alphaVector;
   
   
for( int i=0; i < 10; i++ )
   
   
  alphaVector.push_back( i + 65 );
   
   

  
  
   
    
  
  
// Insert four C's into the vector
   
   
vector<char>::iterator theIterator = alphaVector.begin();
   
   
alphaVector.insert( theIterator, 4, 'C' );
   
   

  
  
   
    
  
  
// Display the vector
   
   
for( theIterator = alphaVector.begin(); theIterator != alphaVector.end(); theIterator++ )
   
   
  cout << *theIterator;
   
   

This code would display:

CCCCABCDEFGHIJ
   
   

size

Syntax: 
   
   
  size_type size();
       
       

The size() function returns the number of elements in the current vector.

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值