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.

 

 

 

 

相关推荐
程序员的必经之路! 【限时优惠】 现在下单,还享四重好礼: 1、教学课件免费下载 2、课程案例代码免费下载 3、专属VIP学员群免费答疑 4、下单还送800元编程大礼包 【超实用课程内容】  根据《2019-2020年中国开发者调查报告》显示,超83%的开发者都在使用MySQL数据库。使用量大同时,掌握MySQL早已是运维、DBA的必备技能,甚至部分IT开发岗位也要求对数据库使用和原理有深入的了解和掌握。 学习编程,你可能会犹豫选择 C++ 还是 Java;入门数据科学,你可能会纠结于选择 Python 还是 R;但无论如何, MySQL 都是 IT 从业人员不可或缺的技能!   套餐中一共包含2门MySQL数据库必学的核心课程(共98课时)   课程1:《MySQL数据库从入门到实战应用》   课程2:《高性能MySQL实战课》   【哪些人适合学习这门课程?】  1)平时只接触了语言基础,并未学习任何数据库知识的人;  2)对MySQL掌握程度薄弱的人,课程可以让你更好发挥MySQL最佳性能; 3)想修炼更好的MySQL内功,工作中遇到高并发场景可以游刃有余; 4)被面试官打破沙锅问到底的问题问到怀疑人生的应聘者。 【课程主要讲哪些内容?】 课程一:《MySQL数据库从入门到实战应用》 主要从基础篇,SQL语言篇、MySQL进阶篇三个角度展开讲解,帮助大家更加高效的管理MySQL数据库。 课程二:《高性能MySQL实战课》主要从高可用篇、MySQL8.0新特性篇,性能优化篇,面试篇四个角度展开讲解,帮助大家发挥MySQL的最佳性能的优化方法,掌握如何处理海量业务数据和高并发请求 【你能收获到什么?】  1.基础再提高,针对MySQL核心知识点学透,用对; 2.能力再提高,日常工作中的代码换新貌,不怕问题; 3.面试再加分,巴不得面试官打破沙锅问到底,竞争力MAX。 【课程如何观看?】  1、登录CSDN学院 APP 在我的课程中进行学习; 2、移动端:CSDN 学院APP(注意不是CSDN APP哦)  本课程为录播课,课程永久有效观看时长 【资料开放】 课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化。  下载方式:电脑登录课程观看页面,点击右侧课件,可进行课程资料的打包下载。
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页