Successfully learned STL :: MAP

zhaozj2021-02-17  45

Objective: To organize a group of data through the name of the name

Analysis: According to the experience, you know that this function C has a ready-made implementation (Stl :: Map / Don't plan to use MS CMAP - Will not write it yourself, but unfortunately didn't learn STL before.

Start working: Not afraid, use Google Site std :: map, y, search Chinese content is only 2-3 items, most of them are repeated, I will not use it, I can see it is wrong, a Demo Can't find it. Still old, the real search English, NB, find SGI help http://www.sgi.com/tech/stl/map.html, look at it a little fur, continue to find http: // p2p.wrox.com/archive/c_plus_plus_programming/2001-06/27.ASP is more detailed, it can be done, no problem.

First write code test, one turn:

Use std :: map to do test the easiest test, but I want to loop using const char * when I started, I don't seem to make it, change std :: map Try, Kao Use VC6 is compiled. However, if the Dong Dong complies with the standard, it seems that the MS compiler is not done, the grievance is full, change std :: map , this is Of course, you can't have problems with MS yourself.

It is time to make it, and the test code can be performed completely. . . Have a spectrum

---------------------------------------- Test code ------ -------------------

// maptest1.cpp: defines the entry point for the console application.//

#include "stdafx.h"

#include

#include // # include #include using namespace std;

Class st {public: int _i; char _s [10];

PUBLIC: STT (INT I = 0) {MEMSET (this, 0, sizeof (* this)); _i = i;}};

Struct ltstr {bool operator () (const char * s1, const char * s2) const {// return strcmp (S1, S2) <0; return_stricmp (S1, S2) <0;}};

Void _map_test2 () {typedef std :: map test_map; typedef test_map :: item Test_it;

Test_map mm; for (int i = 0; i <10; i ) {cstring stmp; stmp.format ("% 02d", i);

STT T (1 I); STRCPY (T._S, STMP); mm [stmp] = t; //mm.insert(test_map ::Value_type (stmp, t));} mm.insert (Test_map :: Value_Type ("AA", STT (11)))); mm.insert (test_map :: value_type ("aa", st (12))); mm.insert (TEST_MAP :: Value_type ("aa", st (13)) INT size = mm.size ();

Test_it it; for (IT = mm.begin (); it! = Mm.end (); it ) {stt * pt = & it-> second; // int T = it-> second._i;}

IT = mm.find ("06"); if (it! = mm.end ()) {stt * pt = & it-> second; // int t = it-> second;}

IT = mm.find ("aa"); if (it! = mm.end ()) {stt * pt = & it-> second; // int Ti = it-> second;

IT = mm.find ("aa"); if (it! = mm.end ()) {stt * pt = & it-> second; // int t = it-> second;}}

Void _map_test1 () {typedef st :: map test_map; typedef test_map :: item Test_it;

Test_map mm; for (int i = 0; i <10; i ) {cstring stmp; stmp.format ("% 02d", i);

INT T = (1 i); mm [stmp] = t; //mm.insert(test_map :: value_type (stmp, t));} mm.insert (Test_map :: value_type ("aa", 11)) Mm.insert (TEST_MAP :: Value_Type ("aa", 12)); mm.insert (Test_map :: Value_Type ("AA", 13));

INT size = mm.size ();

Test_it it; for (it = mm.begin (); it! = Mm.end (); it ) {INT T = it-> second;

IT = mm.find ("06"); if (it! = mm.end ()) {INT T = it-> second;}

IT = mm.find ("aa"); if (it! = mm.end ()) {INT T = it-> second;}

IT = mm.find ("aa"); if (it! = mm.end ()) {INT T = it-> second;}}

INT main (int Argc, char * argv []) {_map_test2 (); _map_test1 (); return 0;} ----------------

Please familiar with STL's friends point to how to use std :: map under MSVC6

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

New Post(0)