Explanation of the usage of C ++ vector and the usage of vector

Source: Internet
Author: User

Explanation of the usage of C ++ vector and the usage of vector

The vector container type vector container is a template class that can store any type of objects (but must be the same class object ). The vector object can efficiently add elements at runtime, and the elements in the vector are continuously stored. Vector constructor prototype: template <typename T> explicit vector (); // default constructor. The vector object is empty. explicit vector (size_type n, const T & v = T (); // create a vector object vector (const vector & x) with n elements; vector (const_iterator first, const_iterator last); note: all objects in the vector container memory are initialized. If the initial value of the stored object is not specified, the built-in type will be initialized with 0, and the default constructor will be called for class type initialization (if there are other constructor and no default constructor, in this case, the initial element values must be provided before they can be placed in the container ). Example: vector <string> v1; // creates an empty container. Its object type is string-type vector <string> v2 (10 ); // create a vector <string> v3 (5, "hello") Container with 10 string objects with an initial value (that is, an empty string "); // create a vector <string> v4 (v3.begin (), v3.end () container for five string objects with the value "hello ()); // v4 is the same as v3 container (completely copied) vector operation (the following functions are all member functions) bool empty () const; // if the container is empty, returns true; otherwise, returns falsesize_type max_size () const; // returns the maximum number of elements that the container can accommodate size_type size () const; // returns the number of elements in the container size_type capacity () const; // Number of elements that the container can store, including: capacity ()> = size () void reserve (size_type n); // ensure capacity ()> = nvoid resize (size_type n, T x = T (); // make sure that the returned result contains: size () = n; if the previous size () <n, then fill in the value of element x. Reference front (); // return the reference of the first element in the container (the container must be not empty) const_reference front () const; reference back (); // return the reference of the last element in the container (the container must be not empty) const_reference back () const; reference operator [] (size_type pos ); // return the reference of an element whose subscript is pos (the subscript starts from 0. If the subscript is incorrect, it is undefined. Const_reference operator [] (size_type pos) const; reference at (size_type pos); // return the reference of the element whose subscript is pos. If the subscript is incorrect, throw an exception out_of_rangeconst_reference at (size_type pos) const; void push_back (const T & x); // Add an element void pop_back () to the end of the container (); // The last element in the popup container (the container must be not empty) http://dig.chouti.com/link/6301183http://dig.chouti.com/link/6301185http://dig.chouti.com/link/6301188http://dig.chouti.com/link/6301189http://dig.chouti.com /Link/6301190 // Note: The following insert and delete operations will move elements (to maintain continuous storage ), therefore, the previous iterator may fail iterator insert (iterator it, const T & x = T (); // insert an element before the insert point element (or insert an element at the insert point) void insert (iterator it, size_type n, const T & x); // note that the iterator may no longer be valid (space may be reassigned) void insert (iterator it, const_iterator first, const_iterator last); iterator erase (iterator it); // delete a specified Element and return the position of the last element after the deletion (if there is no element, return end ()) iterator erase (iterator first, iterator last );/ /Note: After an element is deleted, the iterator corresponding to the element after the deleted vertex is no longer valid. Void clear () const; // clear the container, which is equivalent to calling erase (begin (), end () void assign (size_type n, const T & x = T ()); // assign values. Replace void assign (const_iterator first, const_iterator last); const_iterator begin () const; // Iteration Sequence iterator begin (); const_iterator end () const; iterator end (); const_reverse_iterator rbegin () const; reverse_iterator rbegin (); rend () const; reverse_iterator rend (); Comparison of vector objects (non-member functions) Comparison of vector objects has six comparison operators: operator =, operator! =, Operator <, operator <=, operator>, operator> =. For operator = and operator! =. If a vector object has the same number of elements and all the elements at the corresponding position are equal, the two vector objects are equal; otherwise, they are not equal. Operator <, operator <=, operator>, and operator> = are compared using dictionary sorting policies. Note: you only need to implement operator = and operator! =. The others can be implemented based on these two implementations. Because operator! = (Lhs, rhs) is! (Lhs = rhs), operator <= (lhs, rhs) is! (Rhs <lhs), operator> (lhs, rhs) is (rhs <lhs), operator >=( lhs, rhs) is! (Lhs, rhs ). In addition to common prefix auto-increment operators, the vector iterator also supports arithmetic operations such as it + n, it-n, and it2-it1. Note that the return value of it2-it1 is difference_type (signed type ). Note that any operation that changes the container size may invalidate the previous iterator. Application Example # include <iostream> # include <cassert> # include <vector> using namespace std; int main () {vector <string> v (5, "hello "); vector <string> v2 (v. begin (), v. end (); assert (v = v2); cout <"> Before operation" <endl; for (vector <string>: const_iterator it = v. begin (); it <v. end (); ++ it) cout <* it <endl; v. insert (v. begin () + 3, 4, "hello, world"); cout <"> After insert" <endl; for (vector <string>: size_ty Pe I = 0; I <v. size (); ++ I) cout <v [I] <endl; vector <string >:: iterator it = v. erase (v. begin () + 3, v. begin () + 6); assert (* it = "hello, world"); cout <"> After erase" <endl; for (vector <string> :: size_type I = 0; I! = V. size (); ++ I) cout <v [I] <endl; assert (v. begin () + v. size () = v. end (); assert (v. end ()-v. size () = v. begin (); assert (v. begin ()-v. end () =-vector <string>: difference_type (v. size (); return 0;} program description: The above program uses three elements in the loop output container. The traversal mode of each loop is different. In particular, the second loop uses the size () function in condition judgment, instead of retaining the variable before the loop. There are two reasons for doing this: first, if the number of container elements is modified in the loop when the program is modified in the future, this loop can still work well, however, it is incorrect to save the size () function value first. Second, because these small functions (which require only one Return Statement) are basically declared as inline, therefore, you do not need to consider efficiency issues. I am looking for more information on the Internet. I suggest you buy an stl.
http://dig.chouti.com/link/6301183http://dig.chouti.com/link/6301185http://dig.chouti.com/link/6301188http://dig.chouti.com/link/6301189http://dig.chouti.com/link/6301190

Detailed usage of C ++ vector,

Vector container type
A vector container is a template class that can store any type of objects (but must be the same class object ). The vector object can efficiently add elements at runtime, and the elements in the vector are continuously stored.
Vector Construction

Function prototype:
Template <typename T>
Explicit vector (); // default constructor. The vector object is empty.
Explicit vector (size_type n, const T & v = T (); // create a vector object with n elements
Vector (const vector & x );
Vector (const_iterator first, const_iterator last );

Note: all objects in the vector container memory are initialized. If the initial value of the stored object is not specified, the built-in type will be initialized with 0, and the default constructor will be called for class type initialization (if there are other constructor and no default constructor, in this case, the initial element values must be provided before they can be placed in the container ).

Example:
Vector <string> v1; // create an empty container whose object type is string
Vector <string> v2 (10); // create a container with 10 string class objects with initial values (that is, empty strings)
Vector <string> v3 (5, "hello"); // create a container of five string class objects with the value "hello"
Vector <string> v4 (v3.begin (), v3.end (); // v4 is the same container as v3 (completely copied)

Vector operations (the following functions are all member functions)

Bool empty () const; // if the container is empty, true is returned; otherwise, false is returned.
Size_type max_size () const; // returns the maximum number of elements that the container can accommodate.
Size_type size () const; // return the number of elements in the container
Size_type capacity () const; // number of elements that the container can store, including: capacity ()> = size ()
Void reserve (size_type n); // ensure capacity ()> = n
Void resize (size_type n, T x = T (); // make sure that: size () = n is returned; if the previous size () <n, then fill in the value of element x.

Reference front (); // return the reference of the first element in the container (the container must be non-empty)
Const_reference front () const;
Reference back (); // return the reference of the last element in the container (the container must be not empty)
Co ...... remaining full text>

Usage of c ++ vector

Not a parameter. You can think of "snakmap" as a two-dimensional array.
The snail kemap is another vector in the vector. You can think of a vector as a one-dimensional array.
Then, you can easily understand the snakmap.
However, writing code in this way poses many risks. This is because the value of c. iSign may be a negative number or greater than the value of snail map. size.

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.