C++ vector 和push_back 详解

C++编程语言中有一种叫做Vector的应用方法。

(1)vector< 类型 > 标识符 ;

(2)vector< 类型 > 标识符(最大容量)

(3)vector< 类型 > 标识符(最大容量,初始所有值);

(4) int i[4] = {12,3,4,5};

vector< 类型 > vi(i , i+2); //得到i索引值为3以后的值 ;  

(5)vector< vector<int> > //vi定义2维的容器;记得一定要有空格,不然会报错

vector< int > line   // 在使用的时候一定要首先将vi个行进行初始化;

  for(int i = 0 ; i < 10 ; i ++)

 { 

vector.push_back(line); 

 }  /// 个人认为使用vector定义二维数组很好,

因为是长度可以不预先确定。很好。 

(6)C++ Vector排序

vector< int > vi ;   

vi.push_back(1); 

vi.push_back(3);  

vi.push_back(0);

sort(vi.begin() , vi.end()); /// /小到大

 reverse(vi.begin(),vi.end()) /// 从大道小 

(7)顺序访问

vector < int > vi ;  

 for( int i = 0 ; i < 10 ; i ++) 

{  vector.push_back(i);  }  

 for(int i = 0 ; i < 10 ; i ++) /// 第一种调用方法 

 { cout <<vector[i] <<" " ;  } 

 for(vector<int>::iterator it = vi.begin() ; it !=vi.end() ; it++) ///第二种调用方法

  { cout << *it << " " ; } 

(8)寻找

vector < int > vi ; 

  for( int i = 0 ; i < 10 ; i ++)

 {  vector.push_back(i);  }   

vector < int >::interator it = find(vi.begin() , vi.end,3) ;

 cout << *it << endl ; ///返回容器内找到值的位置。 

(9)使用数组对C++ Vector进行初始化

int i[10] ={1,2,3,4,5,6,7,78,8} ; ///第一种  

 vector<int> vi(i+1,i+3); ///从第2个元素到第三个元素

  for(vector <int>::interator it = vi.begin() ; it != vi.end() ; it++)

 {  cout << *it <<" " ;  } 

(10) 结构体类型

struct temp 

 { 

 public : string str ;  

 public :  int id ; 

};tmp  

int main()

  { 

 vector <temp> t ; 

 temp w1 ;  

 w1.str = "Hellowor" ;

 w1.id = 1 ;  

t.push_back(t1); 

cout << w1.str << "," <<w1.id <<endl ; 

 return 0 ;  

 } 

vector::push_back


public member function

void push_back ( const T& x );

Add element at the end

Adds a new element at the end of the vector,after its current last element. The content of this new element is initializedto a copy of x.

Thiseffectively increases the vector size by one, which causes a reallocation of the internalallocated storage if the vector size was equal to the vector capacitybefore the call. Reallocations invalidate allpreviously obtained iterators, references and pointers.

Parameters

x

Value to be copied to the new element.
T is the first template parameter (the type of theelements stored in the vector).

 

Return value

none

If areallocation happens, it is performed using Allocator::allocate(), which may throw exceptions (for thedefault allocatorbad_alloc is thrown if the allocation request does notsucceed).

Example

//vector::push_back

#include<iostream>

#include<vector>

 

intmain ()

{

  std::vector<int>myvector;

  int myint;

 

  std::cout << "Pleaseenter some integers (enter 0 to end):\n";

 

  do {

   std::cin >> myint;

   myvector.push_back (myint);

  } while (myint);

 

  std::cout << "myvector stores" <<int(myvector.size()) << "numbers.\n";

 

  return 0;

}

The example uses push_back to add a new element to the vector each time a new integeris read.

Complexity

Constant (amortized time, reallocation mayhappen).

See also

vector::pop_back

Delete last element (public member function)

vector::insert

Insert elements (public member function)

 



  • 10
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C++ 中的 vector 是一个动态数组,可以在运行时动态增长或缩小。它是一个模板类,可以存储任何类型的数据。 使用 vector 前需要包含头文件 vector。 ### 创建 vector 创建一个 vector 对象的语法如下: ```c++ vector<数据类型> 变量名; ``` 例如,创建一个存储整数的 vector 对象: ```c++ vector<int> v1; ``` 创建一个存储字符串的 vector 对象: ```c++ vector<string> v2; ``` ### 添加元素 使用 push_back() 方法可以向 vector 中添加元素,语法如下: ```c++ v.push_back(元素); ``` 例如,向 v1 中添加元素: ```c++ v1.push_back(10); v1.push_back(20); v1.push_back(30); ``` ### 访问元素 可以使用下标运算符 [] 或 at() 方法来访问 vector 中的元素。 例如,访问 v1 中的元素: ```c++ cout << v1[0] << endl; // 输出 10 cout << v1.at(1) << endl; // 输出 20 ``` ### 获取 vector 的大小 使用 size() 方法可以获取 vector 中存储元素的数量。 例如,获取 v1 的大小: ```c++ cout << v1.size() << endl; // 输出 3 ``` ### 遍历 vector 可以使用 for 循环或迭代器来遍历 vector 中的元素。 例如,使用 for 循环遍历 v1: ```c++ for (int i = 0; i < v1.size(); i++) { cout << v1[i] << " "; } ``` 使用迭代器遍历 v1: ```c++ vector<int>::iterator it; for (it = v1.begin(); it != v1.end(); it++) { cout << *it << " "; } ``` ### 删除元素 使用 erase() 方法可以删除 vector 中的元素,语法如下: ```c++ v.erase(迭代器); ``` 例如,删除 v1 中的第二个元素: ```c++ v1.erase(v1.begin() + 1); ``` ### 插入元素 使用 insert() 方法可以向 vector 中插入元素,语法如下: ```c++ v.insert(迭代器, 元素); ``` 例如,向 v1 中插入元素 15: ```c++ v1.insert(v1.begin() + 1, 15); ``` ### 清空 vector 使用 clear() 方法可以清空 vector 中的所有元素。 例如,清空 v1: ```c++ v1.clear(); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值