mixed with arrays and vectors in C + + review of university textbooks

Source: Internet
Author: User

This article is based on Deng Junhui authoring The data structure (c + + language version) (3rd edition), "C + + Primer (5th edition)" and the relevant blog posts on the Internet written, Bo Master level is limited, if there is inappropriate, welcome to point out.

One, array

An array in C + + is a built-in data type.

An array is a container for objects of the same type, and the size of the array is constant, and you cannot add elements to the array arbitrarily.

1. Defining and initializing built-in arrays

(1) The size of the array is constant, (a[d],d the dimensions of the array), and the dimensions of the arrays must be a constant expression. When you define an array, you must specify the type and size of the array.

(2) When initializing, allows the dimension of the array is not specified, the dimension is not specified, and the compiler guesses the dimension based on the size of the initial value of the array; If the dimension is specified, the number of initial values is less than or equal to the dimension, and when less than, the insufficient portion is 0 (actually equal to the dimension).

1 int a[]={1,2,3};    The size of array A is 3;2 int a[5]={1,2,3};    Equivalent to {1,2,3,0,0}, size is a[5]={1,2,3,4,5,6};    Error, too many initial values

There is also a special case: an array of characters. When initializing an array with string literals, be aware that the string literal is followed by a null character. In other words, the size of the array is equal to the size of the literal and 1.

Special Note: Copying and assignment is not allowed------You cannot copy the contents of an array to another array as an initial value, or use an array to assign a value to another array.

1 int a[]={1,2,3};    2 int a2[]=a;            Error 3 a2=a;                Error

2. Accessing array elements

The index of an array starts at 0, such as: An array of 10 elements with an index from 0 to 9 instead of 1 to 10, and if A[10] the subscript is out-of-bounds.

In addition, when an array subscript is used, its type is size_t, in the header file Cstddef.

Second, vector

Vector in C + + is a class template.

Vectors are containers of the same type of object, vector sizes can vary, and elements can be added to the array.

1. Defining and initializing vector objects

There are several ways to initialize, such as the following:

1 vector<t> v1;                    V1 is empty, performs default initialization of 2 vector<t> v2 (v1);                V2 contains a copy of all elements of V1 3 vector<t> v2=v1;                Equivalent to V2 (v1) 4 vector<t> v3 (n,val);            V3 contains n repeating elements, and each element's value is VAL5 vector<t> v4 (n);                V4 contains n repeated values initialized by the object 6 vector<t> v5{a,b,c ...};         Contains the number of initialization elements, each element is assigned a corresponding value of 7 vector<t> v5={a,b,c ...};        Equivalent v5{a,b,c ...}

Precautions:

(1) vector<t> v1, the element cannot be added by subscript only when initialized by default. That is, when you initialize the V1 to empty, if you want to add 10 elements to the V1, not through the v1[2]=3, and other forms added, because, others are empty, do not know what v1[2] is what.

(2) Note the difference between vector<t> V4 (n) and Vector<t> v4{n}. The former says that there are n identical elements in the V4, as to how much the value depends on the initialization value of the corresponding object, whereas the latter is said to have only one element in the V4 and its value is n.

(3) You cannot initialize a vector object with parentheses that contain multiple values. Note the difference between the and or parentheses.

1 vector<int> Intv (a);        

2. Adding objects to the Vector object

Use the vector's member function push_back to add an object to it:

1 vector<int> v;2 for (int i=0;i!=100;++i) 3 {4     v.push_back (i); 5}

Attention:

If the loop body contains statements that want to add elements to the vector object, you cannot use the range for loop. Because the range for statement should not change the amount of the sequence it traverses. The reasons are as follows:

1 vector<int> v={1,2,3,4,5,6,7,8,9};  2 for (auto &r:v) 3 {4     r*=2; 5} 6  7 equivalent to 8 for (auto Beg=v.begin (), End=v.end (), beg!=end;++beg) 9 {Ten     Auto &r=*beg;11     r*=2;12}

That is, in the scope for statement, the value of end () is stored, and once the element is added (removed) in the sequence, the value of the End function may become invalid.

3. Expansion, insertion and deletion of vectors

(1) expansion

The underlying data structure of the vector when the array.

When free space in the vector is exhausted, the capacity of the internal array is expanded dynamically. Add space directly on the basis of the original physical space? This is unrealistic. The array-specific address method requires that the physical space must be contiguous, and we cannot guarantee that the tail is always reserved enough to be extended. One way is to request an array with a larger capacity, and move the members of the original array to the new space before inserting it on the subsequent side. The address of the new array is assigned by the OS and is not directly related to the original data area. the capacity of the new array is always twice times the original array .

(2) Insert and delete

The procedure for inserting a given value is to find the position to insert, and then move the element in that position (including the position) one bit backwards and then copy the element of that position to the given value. The removal process moves all elements that are later in the location forward one bit at a time.

(2) vector size and capacity

Size is the number of elements currently owned by the vector container, capacity refers to the total number of elements that the container can store before it must allocate new storage space, capacity is always greater than or equal to size.

Three, the comparison of the array and the vector

1, in-memory location

The array in C + + is the built-in data type, which is stored in the stack , and its memory allocation and release is done automatically by the system; vector, stored in the heap , by the STL Library program responsible for the allocation and release of memory, easy to use.

2, size can change

The size of the array is fixed after initialization, and vectors can be changed by operations such as push_back or pop.

3. Initialization

An array cannot copy the contents of an array to another array as an initial value, nor can it use an array to assign values to other arrays;

4. Execution efficiency

An array of >vector vectors. The main reason is that the expansion process of vectors consumes a lot of time.

REF:

Http://www.cnblogs.com/chhuach2005/p/3627011.html

mixed with arrays and vectors in C + + review of university textbooks

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.