How to use array and vector

Source: Internet
Author: User

One: Define

The definition of array is the same as that defined in the C language. To specify a type, name, and size, the size should be a constant value. For example:

const int seq_size=18;

int pell_seq[seq_size];

The initialization of a vector must contain a header file, which indicates the element type within the angle brackets after the class name, and the size in parentheses, where the size is not necessarily a constant. For example:

#include <vector>

Vector<int>pell_seq (seq_size);

Two: Initialize

The initialization of an array can be done using the method of the specified list. The specified value becomes part or all of the elements of the array. For example:

int elem_seq[seq_size]={1,2,3,4,5};

The number of elements in the list should not exceed the size of the array, and if less, the remaining elements will be initialized to 0

Vector initialization is currently available in 2 ways:

The first of these is:

Vector<int>elem_seq (seq_size);

Elem_seq[0]=1;

elem_seq[1]=2;

Write out all the element values in turn

The second type is:

Use a known quantity group to initialize the vector

int elem_vals[seq_size]={1,2,3,4};

Initialize the elem_seq with the value of elem_vals;

Vector <int>elem_seq (elem_vals,elem_vals+seq_size);

Three: Differences

The vector knows what size it is,

In any program, the number of elements contained in the vector is returned as long as the elem_seq.size () statement is used. No additional definition is required.

How to use array and vector

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.