C ++ vector usage experience

Source: Internet
Author: User

 

Standard Library vector type
Use the required header file:
# Include <vector>
Vector: vector is a class template. It is not a data type. Vector <int> is a data type.

I. Definition and initialization
Vector <t> V1; // The default constructor V1 is empty.
Vector <t> V2 (V1); // V2 is a copy of V1.
Vector <t> V3 (n, I); // V3 contains n elements whose values are I.
Vector <t> V4 (n); // V4 contains n elements whose values are 0.
Ii. Value Initialization
1> if no element initialization formula is specified, the standard library provides an initialization value for value initialization.
2> If the saved expression contains elements of the class type of the constructor, the standard library uses this type of constructor for initialization.
3> If the saved expression does not have the class elements of the constructor, the Standard Library generates an object with an initial value and uses this object for value initialization.
Iii. Most important operations of vector objects
1. V. push_back (t) add t data at the end of the array
2. V. Size () current data size
3. V. Empty () determines whether the vector is empty.
4. V [N] returns the element whose position is N in v.
5. V1 = V2: Replace the V1 element with a copy of the V2 element.
6. V1 = v2 determine whether V1 and V2 are equal
7 .! =, <, <=,>,> = Maintain the meanings of these operators

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, then

You must provide the initial element values to put them 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)
Const_reference back () const;

Reference operator [] (size_type POS); // returns a reference to an element whose subscript is pos (subscript starts from 0. If the subscript is incorrect, it is undefined.
Const_reference operator [] (size_type POS) const;
Reference at (size_type POS); // returns the reference of an element whose subscript is pos. If the subscript is incorrect, an exception out_of_range is thrown.
Const_reference at (size_type POS) const;

Void push_back (const T & X); // Add an element to the end of the container
Void pop_back (); // The last element in the pop-up container (the container must be not empty)

// Note: The following insert and delete operations will move elements (to maintain continuous storage), so the previous iterator may become invalid.
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 the 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 vertex is deleted 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 (); // value assignment, which replaces all elements in the container with the specified element sequence
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 ();
Const_reverse_iterator rend () const;
Reverse_iterator rend ();

Comparison of vector objects (non-member functions)

There are six comparison operators for comparing vector objects: Operator = and 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 ).

Vector iterator
In addition to common prefix auto-increment operators, the vector iterator also supports arithmetic operations: 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_type 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

Cause: 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, and if the size () is saved first () the function value is incorrect. Second, because these small functions (which require only one Return Statement) are basically declared as inline

You do not need to consider efficiency issues.
---------------------------------
In C ++ programming language, there is an application method called vector. Its role is very important in actual programming. Here we will give you a detailed introduction to the application skills and basic content of C ++ vector, hoping to help you.
(1) vector <type> identifier;
(2) vector <type> identifier (maximum capacity );
(3) vector <type> identifier (maximum capacity, initial values );
(4) int I [4] = {12, 3, 4, 5 };
Vector <type> VI (I, I + 2); // obtain the value after I index value is 3;
(5) vector <int> // VI defines a 2-dimensional container. Remember to have spaces. Otherwise, an error will be reported.
Vector <int> line
// When using the tool, you must first initialize the VI rows;
For (INT I = 0; I <10; I ++)
{
Vector. push_back (line );
}
/// I personally think it is good to define a two-dimensional array using vector,
Because the length is not predetermined. Good.
(6) C ++ vector sorting
Vector <int> VI;
Vi. push_back (1 );
Vi. push_back (3 );
Vi. push_back (0 );
Sort (VI. Begin (), VI. End (); // small to large
Reverse (VI. Begin (), VI. End () // small from Avenue
(7) sequential access
Vector <int> VI;
For (INT I = 0; I <10; I ++)
{
Vector. push_back (I );
}
For (INT I = 0; I <10; I ++) // The first call method.
{
Cout <vector [I] <"";
}
For (vector <int >:: iterator it = VI. Begin ();
It! = VI. End (); It ++) // The second call Method
{
Cout <* It <"";
}
(8) Search
Vector <int> VI;
For (INT I = 0; I <10; I ++)
{
Vector. push_back (I );
}
Vector <int>: interator it = find (VI. Begin (), VI. End, 3 );
Cout <* It <Endl; // return the location of the value in the container.
(9) Use arrays to initialize the C ++ vector.
Int I [10] = {1, 2, 4, 5, 6, 7, 78, 8 };
/// First
Vector <int> VI (I + 1, I + 3); // from the first element to the third element
For (vector <int >:: interator it = VI. Begin ();
It! = VI. End (); It ++)
{
Cout <* It <"";
}
(10) struct type
Struct temp
{
Public:
String STR;
Public:
Int ID;
} TMP
Int main ()
{
Vector <temp> T;
Temp W1;
W1.str = "hellowor ";
W1.id = 1;
T. push_back (T1 );
Cout <w1.str <"," <w1.id <Endl;
Return 0;
}

Related Article

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.