STL Practice Guide (on)

zhaozj2021-02-16  52

STL Practice Guidelines Practical Guide to STL Author: Jeff Bogan Translation: Zhou Xiang

Translator Note This is an article guided how you learn STL and practice in 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 the practice guidelines for the readers using STL programming.

STL introduction

STL (Standard Template Library, Standard Template Library is a good technology that needs to master C programming today. 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:

It is convenient to easily implement a series of algorithms such as search data or sort of data; more secure and convenient when debugging procedures; even if people use STL, you can easily understand (because STL is cross-platform of).

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.

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 programs here: 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 Demonstration One // Purpose: Understand the vector in STL

// #include "stdafx.h" - If you use the precompiled header file, you contain the header file of this header file #include // STL vector. 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; //

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.

One of the troubles of STL - initialization

STL is annoying place to initialize it. The initialization of the containers in the STL is more troublesome than that of the C / C array. You can only be an element, or first initialize a normal array and then fill in the container by transformation. I think people can usually do this:

// Program: Initialization Demo // Objective: To illustrate how the vector in STL is initialized. #include // and The same #include using namespace std; int Ar [10] = {12, 45, 234, 64, 12, 35, 63, 23, 12 , 55}; char * str = "Hello World"; int Main (int Argc, char * argv []) {Vector VEC1 (Ar, Ar 10); Vector Vec2 (STR, STR STRLEN (STR)); RETURN 0;}

In programming, there are many ways to complete the same job. Another way to fill the filler is to use more familiar square brackets, such as the following procedures:

// Program: Vector Demo 2 // Purpose: Understand the STL vector with array subscript and square brackets

#include #include #include using namespace std;

Char * SZHW = "Hello World"; int Main (int Argc, char * argv []) {Vector vec (Strlen); / / To allocate memory space INT I, K = 0; char * CPTR = SZHW; while (* cptr! = '/ 0') {vec [k] = * cptr; CPTR ; k ;} for (i = 0; i

This example is clearer, but the operation of the cursor is less, and the additional shape is defined as the subscript, and you must clearly explain how much memory space is displayed in the program. Namespace (Namespace) is related to STL is Namespace. STL is defined in the STD namespace. There are three ways to declare the namespace used: 1. Use this namespace using this namespace, on the top of the file, but add: USING NAMESPACE STD; this method is the simplest method for a single project, this method can limit your code In the STD namespace. 2. Declaration of each object to be used before each template (just like originalization): use std :: cout; using std :: endl; using std :: flush; using std :: set; using std :: inserter Although there is a certain length of rotation, it can be more favorable for the function of memory usage, and you can easily declare and use members in other namespaces. 3. Using the STD domain identifier when using the template in the STD namespace. For example: typedef std :: Vector

VEC_STR;

This method is relatively long, but is the best way to mix the use of multiple namespaces. Some STL's fanatics have been using this method and regard those who do not use this method as a heterogeneous. Some people will build some macro simplified issues through this approach.

In addition, you can add Using Namespace STD to any domain, such as the head or a control cycler of the function. Some suggestions To avoid annoying warnings in debug mode, using the following compiler command: #pragma Warning (Disable: 4786) Another thing to note is that you must ensure that between two angle brackets or sharp brackets And the name is spaced between space because it is to avoid confusion with the ">>" shift operator. Such as vector

> VECLIS;

This will write an error, but write this: Vector

> VECLIS;

Avoid errors. (to be continued)

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

New Post(0)