1, using the array to initialize the vector method
Of course there are many ways to talk about it here. The reason is simple, other ways see this blog post: http://www.cplusplus.me/1112.html
int a[] = {-1111,-1};vector<int> N (A , a +sizeof(a)/sizeof(int));
This involves the knowledge of pointers and the use of sizeof.
The pointer offset actually moves a value of a type size.
The 3:sizeof attribute is able to calculate the length of an array of statically allocated memory!
Int a[10];int n = sizeof (a);
if sizeof (int) equals 4, then n= 10*4=40;
Special note: charch[]= "abc"; sizeof (CH);
The result is 4, note that the string array ends with '! '! In general, we can use sizeof to calculate the number of elements contained in an array, with the following method: int n = sizeof (a)/sizeof (a[0]);
It is very important to note that the use of sizeof in the form parameter group of a function. For example, assume the following function:
void fun (int array[10])
{
int n = sizeof (array);
}
What do you think is the value of N in fun? If you answer 40, then I regret to tell you that you are wrong again. Here n equals 4, in fact, regardless of whether the formal parameter is an array of type int, or a float array, or any other array of user-defined types, and no matter how many elements the array contains, n is 4! Why is it? The reason is that when the function parameter is passed, the array is converted to a pointer, perhaps you want to ask why to translate into pointers, for reasons can be found in many books, I simply say: If the whole array is passed directly, then it is necessary to involve the copy of the array element (the actual parameter to the copy of the formal parameter), This can result in very inefficient function execution! Instead of just passing the address of the array (that is, the pointer), you only need to copy 4byte.
Some things about the vector