C + + STL learning--vector__c++

Source: Internet
Author: User

People who have learned C + + will be very familiar with STL Standard Template Library, STL is actually encapsulates a series of interfaces for us to call. The implementation of many functions or algorithms does not require us to write from scratch, greatly improving our programming efficiency. This blog will introduce the use of vectors in detail in the context of a brief introduction to STL.

There are six major components in the STL:

One. Container (Container): is a data structure, such as list,vector,deque,queue, provided as a template class, in order to access the data in the container, you can use the iterator provided by the container class.

Two. Iterator (iterator): Provides a way to access objects in a container.

Three. Algorithm (algorithm): a template function used to manipulate data in a container.

Four. An imitation function (function object).

Five. Iteration Adapter (Adapter).

Six. Control adapter (allocator).


Our main discussion here is the container, the iterator, and the algorithm. For this blog, let's talk about vectors first. The relevant sample code is uploaded to Https://github.com/chenyufeng1991/STL_vector. The vector corresponds to a data structure that is an array, and is dynamic, which means that we do not have to worry about how much capacity the array has defined beforehand, and its size will grow dynamically. Similar to arrays, we can add and delete elements at the end, as well as random access and modification of element values. First, you introduce the header file #include <vector>. The specific use is as follows:

(1) The creation of vectors

    Vector<int> VEC1; Create an empty vector
    vector<int> vec2 (VEC1);//Create a vector vec2 and vec1 to initialize VEC2 vector<int> vec3
    (10); /Create a vector
    vector<int> vec4 (10,0) with n data;//create a vector containing 10 data and initialize all to 0

Constructors are overloaded, you can create vectors in a variety of ways.


(2) push_back (), Pop_back () Add deletion element

    adding element
    Vec1.push_back (4) to the vector tail;
    Vec1.push_back (6);
    Vec1.push_back (8);
    Vec1.push_back (1);
    Vec1.push_back (2);
    Printvector (VEC1);

    Delete element
    vec1.pop_back () at vector tail;
    Printvector (VEC1);

    Adding elements to the vector head cannot be completed because the vector's data structure is an array of elements that cannot be inserted into the head, otherwise the entire array needs to be moved forward;

    //delete element in Vector head, cannot be completed, reason above

You can use Push_back () to continuously add elements to the vector tail and use pop_back to delete the trailing elements. The operation is very convenient, more convenient than we use the array structure directly.


(3) [],at (), take the element value of a position

Takes the element value of a position in the vector
    cout << "The element value at 1 position is:" << vec1.at (1) << Endl;
    cout << "The element value in 1 position is:" << vec1[1] << Endl;

Because the vector's data structure is an array, it can be accessed randomly.


(4) Begin (), end (), pointer to head element, tail element

the data in the vector of void Printvector (Vector<int> ve) {
    cout << "is:";
    Vector<int>::iterator Veiterator;
    for (Veiterator = Ve.begin (); Veiterator < Ve.end (); veiterator++)
    {
        cout << *veiterator << ""; 
  }
    cout << Endl;
}
This is to understand the begin (). End () by printing all the elements in the vector. You need to use iterators to traverse, or you can interpret the iterator as a pointer, and the Begin () and end () as the head and tail pointers, respectively. This gives you access to each element of the vector.

(5) Back (), front (), access to the head elements and tail elements

    Returns the reference to the trailing data
    cout << "The value of the trailing data is:" << vec1.back () << Endl;
    Returns the reference to the header data
    cout << "The value of the header data is:" << vec1.front () << Endl;


(6) Max_size (), the maximum capacity of the vector; size (): The number of current elements of a vector.

    cout << "The maximum capacity in the vector is:" << vec1.max_size () << Endl;
    cout << "The number of elements in the vector is:" << vec1.size () << Endl;

(7) empty () to determine whether the vector is empty

cout << "Vector is empty:" << vec1.empty () << Endl;

If NULL, returns 1. Otherwise, return 0.


(8) Swap (): Swap the values in two vectors.


(9) Sort (): Sort vectors in ascending order; reverse (): Sort vectors in descending order.

Sort the vector in ascending order (
    Vec1.begin (), Vec1.end ());
    Printvector (VEC1);

    The vector is sorted in descending order
    reverse (Vec1.begin (), Vec1.end ());
    Printvector (VEC1);
The parameters passed are the ranges that need to be sorted, because the entire vector is sorted here, so the arguments point to the head and tail respectively.


(a) [],at (): Modifying elements

    Modify a value in a vector
    vec1[2] =;
    Printvector (VEC1);

    vec1.at (3) = A;
    Printvector (VEC1);


(one) erase (): Deleting an element

    Delete an element of an array
    //Why use iterator to navigate, because if an array deletes an element or inserts an element, it causes other elements to move, so it cannot be deleted directly
    Vector<int>: Iterator Vitera = Vec1.begin ();
    Vitera = Vitera + 2;
    Vec1.erase (Vitera);
    Printvector (VEC1);

When you delete an element, you use an iterator.


() Insert (): Inserts an Element

    Vector inserts an element, to use iterator to locate a position
    vector<int>::iterator Vinsert = Vec1.begin ();
    Vinsert = Vinsert + 2;
    Vec1.insert (Vinsert, 777);
    Printvector (VEC1);

You also need to use iterators.


() Clear (): Clears all elements

Clears all data
    vec1.clear ();
    Printvector (VEC1);
    cout << "Vector is empty:" << vec1.empty () << Endl;
After execution is an empty vector that contains no elements.


For a more detailed description of vectors, refer to the documentation: Http://en.cppreference.com/w/cpp/container/vector

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.