C + + vector multidimensional array initialization and Qing 0 __c++

Source: Internet
Author: User

Vector Common Methods

assign () assigning values to elements in vectors

void assign (Input_iterator start, Input_iterator end); //
void assign (Size_type num, const type &VAL);

Reserve () sets the vector smallest element to hold the quantity function to reserve a space for at least a size element for the current vector. (The actual space may be greater than size)

Resize () Change the size function of vector elements change the size of the current vector and assign a value to the newly created element Val

Swap () swap two vectors

Capacity () returns the number of elements that the vector can hold (without reassigning memory)

Max_size () returns the maximum number of elements that a vector can hold (including the ability to reallocate memory).

Size () returns the number of vector elements

Get_allocator () returns the vector's memory allocator

http://blog.csdn.net/qingqinglanghua/article/details/5035763

initialization and clearance of 0

Define an empty two-dimensional vector, and then assign a value
Vector<vector <int> > Ivec (M,vector<int> (n)); M*n's two-dimensional vector, note that there are spaces between the two ">".

void assign (Const_iterator, Const_iterator last);

void Assign (Size_type _count, const type& _val); assignment, replacing all elements within the container with the specified sequence of elements

Defining and initializing two-dimensional arrays

Vector<vector <int> > Ivec (M,vector<int> (n,0)); M*n's two-dimensional vector, all elements initialized to 0

Vector<string> v1;         Creates an empty container whose object type is a String class
Vector<string> v2 (a);     To create a container with 10 string class objects with an initial value (that is, an empty string)
Vector<string> v3 (5, "Hello"); Create a container with 5 string class objects with a value of "Hello"
Vector<string> v4 (V3.begin (), V3.end ());  V4 is the same container as V3 (full copy)

Several empty container (delete) methods using STL vectors

Iterator Erase (iterator it); Deletes the specified element and returns the position of the element after the deletion (if there are no elements, return end ())
Iterator Erase (iterator, iterator last); Note: After deleting an element, the corresponding iterator for the element after the deletion point is no longer valid.

void clear () const; function deletes all elements in the current vector, equivalent to calling erase (begin (), End ())

Http://hi.baidu.com/ljjyxz123/blog/item/c3bab7f50aabbc05bd31096e.html

Assign and resize

Template <class inputiterator>
void assign (Inputiterator, Inputiterator last);
void Assign (size_type n, const t& u);

The Assign () function either assigns the elements of the interval [first or last] to the current vector, or the element with n values of u to the vector. This function clears the previous contents of the vector assignment.

Note: The Assign operation first deletes all elements within the vector container, and then inserts the new element specified by the parameter into the container.

Parameters

The last

Marks a span of a pair of iterators to copy all elements within the [First,last) markup range into the current vector. Contains the element that the primary points to, and does not contain the element that was pointed to last.

N,u

Represents an element that is reset to the current vector to store n values as T

void Resize (Size_type sz, t C = t ());

Change Length

Resets the length size of the current vector container to sz
If SZ is less than the size of the current vector container, the extra element is deleted, otherwise the newly added element is initialized with a value of T

Parameters

Sz

To set the value of the size of the current vector
The member type Size_type is a unsigned integral type.

C

The value of the newly added element that is initialized.

Nullable, NULL to initialize the newly added element with a value initialization

The Resize function has 2 overloaded versions, one with only one size_type parameter, and one with _ty _val in addition to the Size_type parameter, which is the "optional" new element value.

First version:

void Resize (Size_type _newsize)
{//Determine new length, padding with _ty () elements as needed
Resize (_newsize, _ty ());
}

It can be seen that it makes the 2nd argument with _ty () and invokes its 2nd version. The 2nd version is defined as follows:

void Resize (Size_type _newsize, _ty _val)
{//Determine new length, padding with _val elements as needed
if (Size () < _newsize)
_insert_n (End (), _newsize-size (), _val);
else if (_newsize < size ())
Erase (Begin () + _newsize, End ());
}

As the definition indicates, for the first version:

If the _newsize is less than oldsize, the remaining element values remain unchanged.

If the _newsize is greater than oldsize, the newly added element value is initialized with the default constructor parameter of the element (in particular, the int will be initialized to 0).

For the 2nd version:

If the _newsize is less than oldsize, the remaining element values remain unchanged. (All calls erase () (Begin () + _newsize, End ()) erase extra elements)

If the _newsize is greater than oldsize, the newly added element value is initialized with the supplied 2nd parameter.

Regardless of which version is used, the values in the range of [0,min (_newsize,oldsize) remain unchanged.

capcity and Size

Capcity is the maximum number of elements that can currently be accommodated in this container, that is, the number that can be accommodated without reallocating memory
Size is the number of elements that already exist in the container now
So the volume >= length

Vector <int> A (10);
A.push_back (1);
At this point a.size () =1,a.capacity () =10

Reverse and resize

The vector's reverse only increased the vector's capacity, but size did not change.
Resize also changed the vector's capacity and size.

The reserve is a container reservation, but does not really create an element object and cannot refer to elements within the container until the object is created, so when you add a new element, you need to use the push_back ()/insert () function.

Resize is to change the size of the container and create the object, so after calling the function, you can refer to the object within the container, so when you add the new element, use the operator[operator, or use an iterator to refer to the element object.

Furthermore, two functions in the form of a difference, the reserve function after a parameter, that is required to reserve the space of the container; The resize function can have two parameters, the first parameter is the new size of the container, and the second parameter is the new element to be added to the container, if the argument is omitted, Then the default constructor for the element object is invoked.

Vector<int> Myvec; 
Myvec.reserve (m);     The new element is not yet constructed, 
                          You cannot access an element with [] at this time 
for (int i = 0; i < i++) 
{ 
     Myvec.push_back (i); The new element is then constructed 
} 
Myvec.resize (102);      Two new elements are constructed with the default constructor of the element 
MYVEC[100] = 1;           Directly manipulate new elements 
MYVEC[101] = 2;  

To implement the semantics of resize, the resize interface makes two guarantees:
One is to ensure that the interval [0, new_size) range of data is valid, if subscript index within this interval, Vector[indext] is legal.
Second, the guarantee interval [0, new_size) outside the scope of the data is invalid, if subscript index in the interval, Vector[indext] is illegal.


Reserve simply guarantees that the vector's space size (capacity) reaches at least the size n specified by its parameters. Within the range [0, n], if the subscript is index,vector[index] Such access may be lawful and may be unlawful, depending on the circumstances.


What the resize and the reserve interfaces have in common is that they ensure that the vector's space size (capacity) reaches at least the size specified by its parameters.

Http://blog.sina.com.cn/s/blog_749f55cd0100p5qw.html

Http://bbs.bccn.net/thread-91130-1-1.html

Vector list Assignment speed comparison

V2 = v1;//with assignment operator vector () (3rd)

L2 = l1;//with assignment operator Value List

V2.assign (V1.begin (), V1.end ());///Assign vector (1st)

L2.assign (L1.begin (), L1.end ());//Use Assign to assign value to list

Copy (V1.begin (), V1.end (), Inserter (v2, V2.begin ());//Assign value to vector using the copy algorithm (insert iterator mode) (5ND)

Copy (L1.begin (), L1.end (), Inserter (L2, L2.begin ());//Assign value to list using the copy algorithm (insert iterator mode)

V2.resize (V1.size ());

Copy (V1.begin (), V1.end (), V2.begin ());//vector Assignment (resize) (2nd) with the copy algorithm

L2.resize (L1.size ());

Copy (L1.begin (), L1.end (), L2.begin ());//Assign value to list using the copy algorithm (resize)

V2.assign (L1.begin (), L1.end ());///Assign vector Assignment (from list) (4st)

L2.assign (V1.begin (), V1.end ());///Use Assign to assign value to list (from vector)

Summary:
vector: for vector assignment, the speed of assign is the fastest, followed by resize the copy algorithm to assign value, and the first to think of the assignment operator, the speed is not fast, Can only be ranked third, currently do not know why this is used to insert the iterator and then copy the way is the slowest one.
List: For the list assignment, the assignment operator is the fastest, followed by the assign, followed by the resize copy, and the last one in the same way as the insert Iteration child copy.

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.