Dynamic array class: vector


In the programming process, if we know the length of the array, you can define a static array. In fact, we often encounter the situation at the beginning and the length of the array can not be determined, so this time we can use dynamic array class vector to define an array, so not only saves storage memory, but also makes the program more flexible and reliable.

1.vector features

A vector is a template class, a dynamic array of common functions, the following features:
(1) adding (deleting) the time required elements are fixed at the end of the array.
(2) adding (deleting) the time required for the intermediate elements and the array element number of the elements behind the proportional
element (3) is a dynamic memory, and the vector class is responsible for managing memory.

2. The conventional vector operation

We are given below five initialized (instantiated) method:

#include<iostream>
#include<vector>
using namespace std;

int main()
{
	//1.初始化一个动态数组dyarr1
	vector <int> dyarr1;
	//2.初始化一个动态数组dyarr2,数组的初始长度是10
	vector <int> dyarr2(10);
	//3.初始化一个动态数组dyarr3,包含10个元素,每个元素都是1
	vector <int> dyarr3(10,1);
	//4.将动态数组dyarr3复制到dyarr4
	vector <int> dyarr4(dyarr3);
	//5.使用迭代器,将dyarr4中的前5个元素复制到dyarr5
	vector <int> dyarr5(dyarr4.begin(),dyarr4.begin()+5);
	for(int i=0;i<dyarr5.size();i++)
		cout<<dyarr3[i]<<endl;
	return 0;
}

Note: vector <type> dyarr // here is not only the type of int, may be a float, type, vector, etc.

3. The common operation function

3.1 push_back elements inserted at the end ()

//在数组dyarr1后添加一个元素50
dyarr1.push_back(50)

3.2 insert element inserted in the specified location ()

push_back elements can be inserted at the end. To insert the element in the middle, you can insert ()
The following example method of inserting three kinds:

#include<iostream>
#include<vector>
using namespace std;

int main()
{
	vector <int> dyarr1(5,10);
	//1.在数组dyarr1前插入20
	dyarr1.insert(dyarr1.begin(),20);
	//2.在数组dyarr1末尾插入两个30
	dyarr1.insert(dyarr1.end(),2,30);
	//3.将数组dyarr2插入到dyarr1第一个元素的后面(后面的元素依次后移)
	vector <int> dyarr2(2,50);
	dyarr1.insert(dyarr1.begin()+1,dyarr2.begin(),dyarr2.end());
	//显示dyarr1的每一个元素
	for(int i=0;i<dyarr1.size();i++)
		cout<<dyarr1[i]<<" ";
}

3.3 Delete the last element pop_back ()

pop_back time required to remove elements from the end of the vector is fixed, not with the number of storage elements becomes.

//删除dyarr1最后一个元素
dyarr1.pop_back();

3.4 three ways to access vector elements

#include<iostream>
#include<vector>
using namespace std;

int main()
{
	vector <int> dyarr1(5,10); //初始化数组5个元素都是10 
	//1.用下标运算符[]以数组方式访问
	for(int i=0;i<dyarr1.size();i++)
		cout<<dyarr1[i]<<" "; 
	cout<<endl;
	//2.用成员函数at()访问
	for(int i=0;i<dyarr1.size();i++)
		cout<<dyarr1.at(i)<<" ";
	cout<<endl;
	//3.使用迭代器意义访问打印出来(类似于指针)
	vector <int>::iterator Locator=dyarr1.begin();
	while(Locator!=dyarr1.end())
	{
		cout<<*Locator<<" ";
		++Locator;
	} 
	return 0;
}

4. The dynamic array deque class

deque class and vector class is very similar, but support in the array of the beginning and end of the insert or delete elements.
Before use need to include the header file:

#include <deque>

(1) using push_back and insert pop_back at the end and removing elements

deque <int> dyarr1(5,10); //初始化数组5个元素都是10 
dyarr1.push_back(100);
dyarr1.pop_back();

(2) use push_front and pop_front insert and delete elements at the beginning

dyarr1.push_front(100);//在数组开头插入100
dyarr1.pop_front();//删除数组开头的元素

5. Summary

(1) When not know how many elements need to store, be sure to use a dynamic array vector or deque
(2) vector can only be inserted or deleted at the end, deque can insert and delete operations at the beginning and the end.

Published 51 original articles · won praise 135 · Views 5060

Guess you like

Origin blog.csdn.net/wjinjie/article/details/105101009