STL Practice Guide (medium)

Source: Internet
Author: User

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

(Part 1)

Another container-set)

This is an explanation of the set in Microsoft's help document: "It describes an object that controls the sequence of variable-length elements (note: the keys and values in the set are of the key type, the key and value in map are two components in a pair structure. Each element contains a sort key and a value ). Any element in the sequence can be searched, inserted, or deleted. The time for completing these operations is proportional to the logarithm of the number of elements in the sequence, the delete operation is invalid when the cursor points to a deleted element."
A corrected and more practical definition should be: a set is a container, and the values of the elements contained in it are unique. This is useful when collecting a specific value of data. Elements in the set are arranged in a certain order and used as instances in the set. If you need a key/value pair (pair) to store data, map is a better choice. A collection is organized by a linked list. The insert and delete operations are faster than the vector operations, but the query or addition of elements at the end is somewhat slow.
The following is an example:

// Program: set demo
// Objective: To understand the set in STL)

# Include <string>
# Include <set>
# Include <iostream>
Using namespace std;

Int main (int argc, char * argv [])
{
Set <string> strset;
Set <string >:: iterator si;
Strset. insert ("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 set are arranged in alphabetical order, and each value is unique.

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

Copy (strset. Begin (), strset. End (), ostream_iterator <string> (cout ,""));

. Although set is more powerful, I personally think it is a bit unclear and error-prone. If you understand this, you will know that set is used) what can be done.

All STL containers

The concept of container emerged earlier than template. It was originally an important concept in the field of computer science, but here its concept and STL are mixed. The following are seven types of containers in STL:

Vector )--A standard and secure array in STL. You can only add data in front of the vector.
Deque (double-ended queue )--The function is similar to that of vector, but data can be added to the front and back ends.
List )--The cursor can only move one step at a time. If you are familiar with the linked list, the list in STL is a two-way linked list (each node has two pointers pointing to the front and back ).
Set )--Contains sorted data. The values of these data must be unique.
Map )--A sorted set of binary groups. Each element in map is composed of two values, the key value (the key value in a map must be unique) it is used for sorting or searching. Its value can be retrieved again in the container, while another value is the value associated with the element. For example, in addition to finding a data in Ar [43] = "overripe", map can also find a data in Ar ["banana"] = "overripe. If you want to obtain the element information, you can simply enter the full name of the element.
Multiset (Multi-replica set )--It is similar to a set. However, values in a set cannot be unique (that is, they can be duplicated ).
Multimap )--Similar to ing (MAP), however, key values do not need to be unique (that is, they can be repeated ).
Note:If you read Microsoft's help documentation, you will encounter a statement about the efficiency of each container. For example, the insert time of log (N * n. Unless you want to process a large amount of data, the impact of these times can be ignored. If you find that your program is obviously lagging behind or you need to handle time-related things, you can learn more about the efficiency of various containers.

How to use classes in a map?

Map is a template class that obtains values through keys.
Another problem is that you want to use your own class in map instead of the existing data type, such as the int that has been used now. Create a "template-ready" class. Make sure that the class contains some member functions and overload operators. The following members are required:

  • Default constructor (usually null)
  • Copy constructor
  • Heavy-duty "=" Operator
     

You should reload as many operators as possible to meet the needs of specific templates. For example, if you want to define a class as the key in map, you must reload the related operators. However, we will not discuss overload operators too much here.

// Program: Map custom classes.
// Purpose: to explain how to use a custom class in map.

# Include <string>
# Include <iostream>
# Include <vector>
# Include <map>
Using namespace std;

Class CStudent
{
Public:
Int nStudentID;
Int nAge;
Public:
// Default constructor -- usually null
CStudent (){}
// Complete Constructor
CStudent (int nSID, int nA) {nStudentID = nSID; nAge = nA ;}
// Copy the constructor
CStudent (const CStudent & ob)
{NStudentID = ob. nStudentID; nAge = ob. nAge ;}
// Reload "="
Void operator = (const CStudent & ob)
{NStudentID = ob. nStudentID; nAge = ob. nAge ;}
};

Int main (int argc, char * argv [])
{
Map <string, CStudent> 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 );

// Access members in the Cstudent class by name
Cout <"The Student number for Joe Lennon is" <
(MapStudent ["Joe Lennon"]. nStudentID) <endl;

Return 0;
}

TYPEDEF

If you like to use the typedef keyword, the following is an example:
Typedef set <int> SET_INT;
Typedef SET_INT: iterator SET_INT_ITER

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

ANSI/ISO string

ANSI/ISO strings are widely used in STL containers. This is a standard string class and has been widely promoted. However, in the absence of format declarations, problems may occur. You must use <and iostream code (such as dec and width) to concatenate strings.
You can use c_str () to obtain the character pointer again when necessary.

 

(To be continued)

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.