STL Practice Guide (middle)

zhaozj2021-02-16  50

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

(Connected)

Another container - set (SET)

This is the interpretation of the Collection (SET) in Microsoft Help document: "Describes an object that controls the sequence of germplasses (Note: Key and Value in the set are key types, while Key and Value in Map are a PAIR structure Two components of the medium), each element contains a sort key and a value (Value). You can find, insert, and delete any of the elements in any element in the sequence, and complete these operations The time is a proportional relationship with the number of elements in this sequence, and when the binder points to a deleted element, the delete operation is invalid. "And a corrected and more actual definition should be: a collection (SET) Is a container, which is unique in which the elements contained therein are unique. This is useful when collecting a specific value of a data. The elements in the collection are arranged in a certain order and are used as an example in the collection. If you need a key / value pair (PAIR) to store data, MAP is a better choice. A collection is organized through a linked list, which is slower than the vector (Vector), but finds or adds the end of the elements. Below is an example:

// Program: SET Demo // Purpose: Understand the collection of STL (SET)

#include #include #include using namespace std;

Int main (int Argc, char * argv []) {set strset; set :: item ("cantaloupes"); strset.insert ("apple"); strset.insert "Orange"); strset.insert ("banana"); strset.insert ("grapes"); strSet.insert ("grapes"); for (Si = strSet.Begin (); Si! = strset.end () ; Si ) {cout << * Si << "";} cout << endl; return 0;}

// Output: Apple Banana Cantaloupes grapes Orange // Note: The elements in the output of the output are arranged in the order of alphabetically, and each value is not repeated.

If you are interested, you can replace the output loop with the following code:

Copy (strSet.egin (), strset.end (), ostream_iterator (cout, "));

Although the collection (SET) is more powerful, I personally think that it is unclear, and it is easier to make mistakes. If you understand this, you will know what to do with a collection (SET).

All STL containers

The concept of container is earlier than template, which is originally an important concept in a computer science field, but here, its concept is mixed with STL. Below is the 7 containers that appear in STL: Vector - STL is secure array. Can only add data in the "front" of the Vector. DEQUE (Double-end queue double-ended queue) - is similar to the vector, but data can be added to the front and rear ends. List (list) - The cursor can only be moved at a time. If you are already familiar with the list, then the LIST in the STL is a two-way linked list (each node has two pointers that are forwarded and pointing to the successive subsequent). Set (Collection) - contains sorted data, which must be unique. Map (mapping) -Rolded binary groups, each element in MAP consists of two values, where the key (key value, key value in a map must be unique) Use when sorting or searching, it can be re-acquired in the container; and the other value is the value associated with the element. For example, in addition to the AR [43] = "Overripe" finds a data, the MAP can also find a data via Ar [Banana "] =" Overripe ". If you want to get the element information, you can easily implement the full name of the input element. MultiSet - and Collection (SET) are similar, but the values ​​are not required to be unique (ie, repetition). MultiMap - and Mapp (MAP) are similar, however, the key value is not required (that is, you can have repetition). Note: If you read Microsoft's help documentation, you will encounter a statement on the efficiency of each container. For example: LOG (N * n) insertion time. The impact of these times can be ignored unless you have to handle a lot of data. If you find that your program has obvious hysteresis or requires time critical things, you can learn more about various container operational efficiency.

How to use classes in a map?

MAP is a template class that gets value (value) through the Key. Another problem is that you want to use your own classes in Map, not existing data types, such as INT that has been used now. Create a "Template-Ready" class, you must ensure that some member functions and overload operators are included in this class. Some members of the following are must:

Default constructor (usually empty) copy constructor overloaded "=" operator

You should overload as many operators as possible to meet the needs of a particular template, for example, if you want to define a key (key) in the MAP, you must overload the relevant operator. But there is no discussion of overload operators here.

// Program: Map the custom class. // Objective: Describe how to use custom classes in MAP.

#include #include #include #include usespace std;

Class cstudent {public: int nStudentId; int nage; public: // Default constructor - usually empty cstudent () {} // complete constructor cstudent (int nsid, int NA) {nStudentId = nsid; NAGE = Na;} // copy constructor cstudent (const cstract & ob) {nStudentId = ob.nstudentID; Nage = ob.nage;} // Reserved "=" void operator = (const cstudent & ob) {nstudentId = ob.nstudentId; Nage = ob.nage;}}; int main (int Argc, char * argv []) {Map mapstudent;

MapStudent ["Joe Lennon"] = CSTUDENT (103547, 22); MapStudent ["Phil McCartney"] = CStudent (100723, 22); MapStudent ["Raoul Starr"] = CStudent (107350, 24); Mapstudent ["Gordon Hamilton "] = CStudent (102330, 22);

// Visit the member COUT << "The Student Number for Joe Lennon IS" in the CStudent class by name "(MapStudent [Joe Lennon"]. NstudentId << Endl

Return 0;}

Typedef

If you like to use the typedef keyword, the following is an example: typedef set set_int; typedef set_int :: item set_int_iter

A habit of writing code is to use uppercase letters and underscores to name data types.

ANSI / ISO string

The ANSI / ISO string is used very common in the STL container. This is a standard string class and has been widely advocated, but problems will be issued in the absence of format declaration. You must use the "<<" and input and output streams (such as DEC, Width, etc.) to link the string. The character pointer can be re-obtained using c_str () when necessary.

(to be continued)

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

New Post(0)