How to Use Arrays and Vectors

array it needs a constant value

const int seq_size = 18; 
int pell_seql[seql_size];
//defined a constant value: seq_size = 18 

As vector,First, we need to include Head file of vector: #include<vector>

vector is a template,So we need to add curly braces <Type> Size don't need to be a Constant value here

#include<vector>
vector<int> pell_seql(seq_size);//Attention! Size don't need to be a constant value here 

whether array or vector,we can appoint Location,by []

pell_seql[0] = 1;//The first element is 1
pell_seql[1] = 2;//The second element is 2

initialization

array

int elem_seq[seql_size] = {,,,};//cant over size

vector

First Merhod(Troublesome)

vector<int>elem_seq(seql_size);
elem_seq[0] = 1;
elem_seq[1] = 2;
............etc

The second Method

int elem_vals[seq_size] = {1,2,3,4,5,6,7,8,9};

vector<int>elem_seq(elem_vals,elem_vals + seq_size)//the two values here refer to location,They identify Initialized element range

There is a difference between Array and Vector: Vector know its size

posted @ 2019-11-25 15:38  折学Lex  阅读(123)  评论(0编辑  收藏  举报