C ++ vector (vector)

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

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 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.

---------------------------------

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;

}

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.