[c++]记录使用vector遇到过的天坑问题

/ 4评论 / 782阅读 / 0点赞

vector

* vector是c++的一个模板容器,同时也是一个动态数组,是数组就意味着它支持随机访问。

* 它几乎与arraylist一样,但vector是线程安全的,因此vector的性能比arraylist弱。

* 使用时需要#include <vector>

注意

vector<int> arr;
for (int i = 10; i--;)		//向vector压入数据
	arr.push_back(i);

vector<int>::iterator it_p = arr.begin();//获取vector第一个的迭代器
for (; it_p < arr.end(); ++it_p)//依次顺序输出
	cout << *it_p << " ";

cout << arr.back() << endl;	//输出最后一个元素的值

it_p = arr.begin();
it_p += 4;			//把迭代器移动4个位置
cout << *(it_p - 3) << endl;	//不会报错
//cout << *(it_p - 6 + 3) << endl;//如果编译器有优化提前计算出了结果-3,则不会报错
int a = 6, b = 3;
//cout << *(it_p - a + b) << endl;//会报错

  1. Rastrear Celular说道:

    Localize por meio do software de sistema “Find My Mobile” que acompanha o telefone ou por meio de software de localização de número de celular de terceiros.

  2. YpKjqgSW说道:

    buy cialis online usa The system will be available Friday, October 30th

  3. Authelm说道:

    buy cialis daily online Inflammation can lead to increased pressure in the medullary bone, which can cause the infection to break through the cortex and periosteum

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注