Standard template libraries derived from C + + templates-----> Initial Involvement

Source: Internet
Author: User

C + + template, is a very important part of the previous mention of some foundation, about the template needs to be aware of the issues, will be in the recent collation, today is to say, is a template out of the standard Template Library.

The original is often recommended to see the "STL Source Analysis" This book, heard very strong, is a C + + master need to go through the road, can never know what STL is, also has forgotten to check, today some of the things sorted out, at least can let not understand this aspect of children's shoes to understand.

C + + Standard Template Library, that is Stl:standard template Lib,stl generation, is inevitable. In the long-term coding process, some programmers found that some of the code is often used, and the requirements are particularly stable, so the major companies are selling their own IDE environment, while the code is packaged together to sell, and then the big companies agree that should say this part of the code to further unify the specification, so, Form a set of standard templates, and gradually evolved into the current STL.

C + + Standard Template Library is a very large family, today, refers to some of the commonly used parts, come out and share with you. Other ways of using this are very similar to those mentioned today, and many programmers are using it when developing projects. Now get to the point of today.

vector Vectors : header file: #include <vector>

The essence of a vector is an array of components , which can be thought of as a arrays, but more powerful relative arrays, depending on the number of elements stored, automatic length or shorten , features: Reading can be completed in constant time

In the VS2013 environment, you can simply verify it.

650) this.width=650; "title="]izvwg_xdv7po{0 ' Txq70{f.png "src=" http://s3.51cto.com/wyfs02/M00/7F/1B/ Wkiom1ctkwxhsay5aabouw3d-7o368.png "alt=" Wkiom1ctkwxhsay5aabouw3d-7o368.png "/>

where size is the number of existing elements in the vector, capacity is the vector that can hold the most array elements

How to initialize a vector object:

1. VECTOR<T>V1-----> Save object of type T, default v1 is empty vector

2, Vector<t>v2 (v1)-----> Use one vector to initialize another vector

3, Vector<t> v3 (n,i)----->v3 contains n values of I elements, that is, v3 stored n i

4, Vector<t> v4 (n)----->v4 contains n copies of the value initialization element

Vector Common functions:

Empty ()-----> determine if the vector is empty < NULL returns TRUE, non-null returns fause>

begin ()-----> return vector iterator first element

End ()-----> returns the next element of the last element of the vector iterator

Clear ()-----> empty Vector

Front ()-----> First Data

Back ()-----> Last Data

Size ()-----> get the size of the data in the vector

push_back (elem)-----> inserting data into the tail of a vector

pop_back ()-----> Delete vector trailing data

Since the vector is the encapsulation of the array, it is unavoidable to take into account its traversal problem.

There are two ways in which vector traversal works:

One, traversing in array form

for (int i = 0;i<vec.size;i++)

{

cout<<vec[k]<<endl;

}

Second, using iterators

Vector iterators are defined in the following way:

vector<t>::iterator + Variable name = Vec.begin (); It can be understood that the type of the iterator variable is vector<t>::iterator, and the next reason to assign a value vec.begin (), VEC is the vector name I define, because we want to complete the traversal. The code is as follows:

int main (void) {vector<int>vec; vec.push_back (1); Vec.push_back (2); Vec.push_back (3); Vec.push_back (4); Vec.pop _back (); Array method for (int i = 0; i < vec.size (); i++) {cout << vec[i] << Endl;}//iterator method vector <int>::iterat or Pvec = Vec.begin (); for (; Pvec! = Vec.end (); pvec++) {cout << *pvec << Endl;} system ("Pause"); return 0;}

Linked list templates: List header file: #include<list>

Features: Fast data insertion speed

Usage: Same as vector use method, with push, insert, begin, end and other functions, can also be accessed through iterators

It is important to note that thelist cannot be accessed by means of an array

Also give the list traversal code, as follows:

int main (void) {list <int>list1; list1.push_back (1); List1.push_back (2); List1.push_back (3); List1.push_back (4) ; List<int>::iterator plist = List1.begin (); for (; plist!= list1.end (); ++plist) {cout << *plist << Endl;} system ("Pause"); return 0;}

map Template: Map header files: #include <map>

The map key (key) corresponds to a value of one by one, which is the pair, which defines several pairs of key and value by the keyword pair , and then inserts the INSERT function into the map.

when iterating through a map with an iterator, you cannot add direct access to the iterator

Give the code for the map traversal as follows:

Int main (void) {&NBSP;MAP&LT;INT,&NBSP;STRING&GT;M;&NBSP;PAIR&NBSP;&LT;INT,&NBSP;STRING&GT;P1 (1, "Hello"); &NBSP;PAIR&NBSP;&LT;INT,&NBSP;STRING&GT;P2 (2,  "World");  pair <int, string>p3 (3,   "Zhou");  m.insert (p1);//Push_back m.insert (P2) is not available here;  m.insert (p3); cout < < m.size ()  << endl; ////array output  //for  (int i = 0; i  < m.size ();  i++)// {//  cout << m[i] << endl ;// } //iterator output  map<int, string>::iterator pmap = m.begin ();  cout  << m.size ()  << endl; for  ( pmap != m.end ();  pmap++)  {  cout << pmap->first << endl;//first = key    cout << pmap->second << endl;//second = value   cout < < endl;   }  system ("pause");  return 0;}

In addition, the array access of the MA element, and the simple array is also different, such as the following code.

Map<string, string> m; Pair<string, string> p1 ("A", "Shanghai"); Pair<string, string> p2 ("B", "Beijing"); M.insert (p1); M.insert (p2); cout << m["A"] << Endl; cout << m["B"] << Endl;

This article is from the "11321490" blog, please be sure to keep this source http://11331490.blog.51cto.com/11321490/1764867

Standard template libraries derived from C + + templates-----> Initial Involvement

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.