Use Array and Vector

Array is an array type, and vector is the vector class provided by the library that defines the container.

Containers can hold consecutive integer values and allow elements to be accessed by name or by location.

  1. The size of an array must be a constant expression.
  2. When we define a vector, we must include a vector header, as in
#include <vector>
vector<int> pell_seq( seq_size );
Copy the code
  • Angle brackets are element types whose case is in parentheses. This size is not a constant expression.

  • The first element of the container is 0.

  • To specify the first few elements of a sequence, write:

pell_seq[ 0 ] = 1;
pell_seq[ 1 ] = 2;
Copy the code

Initialize the

  • Array initialization
  1. int elem_seq[ seq_size ] = {
    1.2.3.3.4.7};Copy the code
  2. int elem_seq[] = {1.2.3.3.4.7};
    Copy the code
  • An initialization vector

Vector does not support initialization lists of the kind described above.

  1. vector<int> elem_seq( seq_size );
    elem_seq[ 0 ] = 1;
    elem_seq[ 1 ] = 2;
    / /...
    Copy the code
  2. Use an initialized array as the initial value of the vector
   int elem_vals[ seq_size ] = {
       1.2.3.3.4
    };
    vector<int> elem_seq( elem_vals, elem_vals+seq_size );
    // The two values in brackets are the locations of the actual memory
Copy the code

Elem_seq.size () returns the number of elements that the vector contains.

Why does running the code given in the book report an error

#include <vector>

int main(a)
{
    int seq_size = 8;
    vector<int> pell_seq(seq_size);

    return 0;
}
Copy the code

The following error message is displayed: