C ++ standard template library STL Learning

Source: Internet
Author: User

STL is a library supported by all c ++ compilers and all operating system platforms. It can be used for all c ++ language compilers and all platforms (Windows/Unix/Linux ..). Of course, you can use C/C ++ to implement all the algorithms in the standard query template library, but STL is already an encapsulated library, indicating that its implementation details have been optimized a lot, the efficiency of Self-writing is not necessarily higher than that in the database. Why is the machine question more inclined to implement algorithms by myself? We mainly examine our skills in using languages and our understanding of the principles of common algorithms.

In computer questions, the general focus is on the use of C language proficiency, the C language is also cross-platform, because the lower level, because the execution efficiency of C and the space occupied by the program is small, therefore, C language is often used in embedded programming. It is seldom seen that some people use the C ++ standard template library. In fact, the C ++ standard template library is not required in the question. Using C ++ STL can greatly save time, because many common algorithms have been encapsulated in STL, they can be used directly, such as sort sorting.

Previously, vector containers and sort sorting were used. These are all in STL. vector can easily implement dynamic arrays, instead of allocating space before implementing dynamic arrays in C language, finally, it is so troublesome to release space that the vector container has encapsulated these details and is transparent to users.

The following figure shows how to use the container-stack in the standard template library to implement string arithmetic operations. For example, input "1 + 4*5-8/3" and the output result is 19.

The most common method for this question is stack implementation. The following is the sample code:

# Include <iostream> # include <stack> using namespace STD; // arithmetic operation without parentheses, the operator priority is '=' // compare the priority of the two operators. Char optcompare (char a, char B) {if (a = '+' | A = '-') {If (B = '+' | B = '-') {return '>';} else {return '<';} else {// if A is */, then no matter what operator B is, all are> return '>';} int compute (int x, char optr, int y) {int ret; Switch (optr) {Case '+ ': ret = x + y; break; Case '-': ret = x-y; break; Case '*': ret = x * Y; break; Case '/': ret = x/y; break;} return re T ;}// test stack void main () {stack <int> dat; // operation stack <char> optr; // operator stack string STR = "1 + 4x5-8/3"; for (INT I = 0; I! = Str. size (); I ++) {If (STR [I]> = '0' & STR [I] <= '9') {dat. push (INT) STR [I]-48); // convert the character into a number into the stack} else {If (optr. empty () {optr. push (STR [I]);} else {If (optcompare (optr. top (), STR [I]) = '<') {optr. push (STR [I]);} else {// The top element of the stack has a high priority, and the operation result is returned to the stack int A = dat. top (); dat. pop (); int B = dat. top (); dat. pop (); dat. push (compute (B, optr. top (), A); optr. pop (); optr. push (STR [I]) ;}}} while (optr. empty () = false) {// calculate the final result int A = dat. top (); dat. pop (); int B = dat. top (); dat. pop (); dat. push (compute (B, optr. top (), A); optr. pop ();} cout <"The result is" <dat. top () <Endl ;}

Of course, you can also define the data structure of the stack. Obviously, it is much easier to directly use the stack container.

The following references describe the C ++ standard template library for future use.

1. http://www.sgi.com/tech/stl/ // STL official website, you can directly check the API Function
2. http://net.pku.edu.cn /~ YHF/usingstl.htm

3. http://morningspace.51.net/resource/stlintro/stlintro.html

4. http://blog.csdn.net/zlgrj1986/article/details/2252787

5. http://www.cnblogs.com/shiyangxt/archive/2008/09/11/1289493.html

6. http://www.vckbase.com/document/viewdoc? Id = 1461

7. http://www.linuxsir.org/bbs/showthread.php? Threadid = 86454 // introduction to the C ++ STL standard template library on Linux

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.