STL Practice Guide

xiaoxiao2021-03-06  42

This is a guide you how you are

Learn STL and practical articles under Microsoft Visual Studio. This article spoken from STL, step by step, gradually, involved in the method of STL writing code, STL code compilation, Namespace, ANSI / ISO string in STL, Various types In container, Template, Iterator, Algorithms, Allocator, container nested, etc., the author puts some suggestions in this article, and Point out the problem that should be noted when using STL. This article covers wide, and the perspective is comprehensive. It is not only suitable for beginners to learn STL, but also readers using STL.

Programming Practice Guide.

STL introduction

STL (Standard Template Library, Standard Template Library) is the current C today

Programmed people need a good technology. I think everyone who first school STL should spend some time to familiarize it. For example, there will be a lot of learning curves in a sharp rise, and some name is not easy to remember, maybe I know. The name has been used in light), however, if you have a STL, you will not feel headache. STL is more complex and powerful than MFC.

STL has some advantages:

You can easily achieve a series of algorithms such as search data or sort data sorting;

More secure and convenient when debugging procedures;

Even people who use STL in the UNIX platform you can easily understand (because STL is cross-platform).

background knowledge

Write this part is to let some beginner's readers have a good start in the challenging computer science field, not to understand the endless japanese terms and dull rules, just regarding those jars and rules here. STLER is used for self-entertainment creation.

Use code

The code used in this article is mainly guided in STL practice.

Some definitions of foundation concepts

Template - macro (Macro) of a class (and various data types and functions such as structures). Sometimes called a cookie cutter, a regular name should be called a generic - a class template called the generic class, and a function of a function is naturally called a modest function ( Generic function.

STL - Standard Template Library, some of the intelligent people written some templates now have become part of the standard C language used by each person.

Container - template class that can accommodate some data. There are versors such as VECTOR, SET, MAP, MULTIMAP and DEQUE in STL.

Vector - Basic Array Template, which is a container.

Cursor (Iterator) - This is a strange thing, it is a pointer to point to the elements in the STL container, or to other elements.

Hello World Program I am willing to write my program in my gold: a Hello World program. This program transmits a string to a character vector and then displays one of the characters in the vector. The vector is like a garden that is growing, and half of all STL containers is based on the vector. If you have the program, you will almost master half the entire STL. // Program: Vector Demo One // Objective: To understand the vector // #include "stdafx.h" in the STL - If you use the precompiled header file, you contain the header of the header file #include // STL vector file. There is no ".h" here. #include // contains the header file of the COUT object. Using namespace std; // guarantees that members in the STD namespace can be used in the program. Char * szhw = "Hello World"; // This is an array of characters and ends with "/ 0". INT main (int Argc, char * argv []) {Vector vec; // Declare a character vector VECTOR (Array in STL) / / Define a cursor Iterator for a character array. Vector :: item vector vi; // initialize the character, loop the entire string, // is used to fill the data into the character vector until the end of "/ 0". Char * cptr = szhw; // points a pointer to "Hello World" string while (* cptr! = '/ 0') {vec.push_back (* cptr); CPTR ;} // push_back function put data in vector The tail. // Display the characters in the vector in the console for (vi = vec.begin (); vi! = Vec.end (); vi ) // This is the standardized start of the STL loop - usually " ! = ", Not" <"// because" <"is not defined in some containers. // begin () Returns the cursor of the vector starting element (Ite () returns the cursor (Iterator) of the vectors of the vector. {cout << * vi;} // Use operator "*" to extract data from the cursor pointer. Cout << Endl; // Remove Return 0;

Push_back is a standard function that puts data into a Vector (vector) or Deque (two-end queue). INSERT is a similar function, however it can be used in all containers, but usage is more complicated. End () is actually taking the end of the end (the previous elements at the end of the container) so that the loop is correctly run - it returns the pointer to the most close-to-array boundary. Just like an array in a normal cycle, such as for (i = 0; i <6; i ) {ar ​​[i] = i;} --ar [6] does not exist, this element does not reach this element in the loop. Therefore, there will be no problem in the cycle.

转载请注明原文地址:https://www.9cbs.com/read-67479.html

New Post(0)