A brief introduction to some standard template containers for C + + (1)

Source: Internet
Author: User

    1. Vector type:



  1. Vector is a member of the C + + Standard Template Library, and it needs to include: #include <vector> header files


  2. Vectors are collections of the same data type, each of which corresponds to an index value.


  3. The standard library manages the associated memory.


  4. A vector is not a data type, but a class template.


  5. Vector type Each instance specifies the type of data he saves. Thus,vector<int>,vector<string> is a custom data type.


  6. How vectors are initialized:

    (1) using the default initialization of Vector<t> v1; Use the default constructor of type T (if the class does not define a default constructor, the standard library still produces an object with an initialized value.) It is a good idea to define a default constructor in your own class. If it is a built-in type, the initialization is created with a value of 0

    (2) Copy initialization of Vector<t> v1 (v2); Using this approach, V1 and V2 need to be the same data type.

    (3) Value initialization:vector<t> v1 (n,t) or vector<t> v2 (T);



  7. An important attribute of vector objects and other standard library container objects: runtime, adding elements efficiently


  8. Vector-supported operation:vector<t> VEC1;

    (1) Vec1.empty () returns True if the Vec1 is empty. otherwise returns false

    (2) Vec1.size () returns the number of elements in the VEC1

    (3) Vec1.push_back (v1) Add V1 at the end of VEC1

    (4) Vec1[n] Returns the value of the element (n-1) in VEC1. n must be less than vec1.size ()

    (5) vec1 = VEC2; Replace the elements in the VEC1 with a copy of the VEC2

    (6) Vec1 = = VEC2; If VEC1 equals vec2 then return True

    (7) >, >=, <, <=,! = are all the same as built-in type meanings.



  9. For the size () function, it is generally saved with size_type, similar to the size_type of string, and you need to indicate what type of size_type you want to use.

    Vector<int> Size_type; Ok

    Vector Size_type; Error

  10. add element to vector: push_back () function.

    Vector <int> VEC1;

    Vec1.push_back (10); Ok


  11. The following table operation of the vector does not provide an action to add elements. The following table operation can only fetch elements that already exist. This means that if there is no element in this vector for a vector<int> vec1, it is not allowed to add elements to this container by following the table operation. Conversely, if this element is now determined, two elements in this container 0 and 1, if the value of the first element is changed to 2 by the following table, vec1[0] = 2;//ok.


  12. In particular, it is important to emphasize that for vectors, the following table operation can only be used to make sure that an existing element


  13. In addition to using subscript operators to access elements in the vector, the standard library provides another way: iterators:

    1. Definition: An iterator is a type of data that examines the elements in a container and traverses the elements. (This definition applies to containers defined by the standard library.)

    2. The container for each standard library defines the corresponding iterator data type. Vector<class T>::iterator Myiter;

    3. Each standard library container type defines a member named iterator, where the iterator has the same meaning as the actual iterator type.


  14. The Begin and end operations of the vector container.

    1. The BEGIN function returns an iterator in the container that points to the first element.

      Vector<int> Myvec (n,10);

      Vector<int>::iterator Myiter;

      Myiter = Myvec.begin ();


The End function returns an iterator in the container that points to the next position in the last element. Usually the result of an end is called an "out-of-terminal iterator", and this function returns an iterator that points to an element that does not exist. If the container is empty, then myvec.begin () = = Myvec.end (); The iterator type can use the dereference operator (*) to access the element that the iterator points to. Because the end () function of the container returns the position beyond the end element, it is not possible to dereference the iterator returned by end (). If two iterators execute the same element, then the two iterators are equal. But for a vector container, two elements of equal value, the values of their corresponding iterators are not the same. (You can do an experiment with reference to the code below) vector<int> Vec1;vec1.push_back (1); Vec1.push_back (3); Vec1.push_back (3); vector<int>:: Iterator it1, it2;it1 = Vec1.begin (), it2 = Vec1.begin () + 2;if (it1 = = it2) cout << "yes" << endl;elsecout < < "no" << Endl;


M. using the iterator to reset the value of the container element and the following table operation is similar in the container we can use iterator to access or change the value of the element, but in some cases we only want the iterator to be able to only reach the value of the element, but not change the value of the element, this time we need to use Const_ Iterator to achieve.  The only difference with ordinary iterator is that we use const_iterator to not modify the value of the element in the container, but only to access the value of the container. In specific use, we need to choose which iterator to use, depending on the situation.



This article is from "Home and Everything" blog, please make sure to keep this source http://louis1126.blog.51cto.com/2971430/1676113

A brief introduction to some standard template containers for C + + (1)

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.