Getting Started -> Study STL Std :: Map <std :: string, int>

zhaozj2021-02-17  52

C : A more popular powerful function oriented object-oriented programming language, application and prospects are very broad.

STL: C standard template library, the function is extremely powerful, and the C programming will be quite easy to fully master.

Map: A relational container that matches a variety of data based on keywords.

String: "~! @ # $% ^ & * () _ ", such stuff is.

The following program code will simply demonstrate how to match the ID (int) through the name (String) keyword, due to the program (program, affected by the online data, I also started to tell the program into a runner, nonsense) too simple, no comments, For friends who want to get started, you can understand that you can give yourself a nonsense, even if you learn to move :) Master please skip.

The program is successfully tested in the following environments:

Win2K VC6

Redhat Linux 7.2 GCC (G ) 2.96

- Source Procedure -

// stramap1.cpp

//

#pragma Warning (Disable: 4786)

// ...

#include

#include

// ...

#include

Using namespace std;

Class strmap1

{

Typedef std :: map type_map;

TYPEDEF TYPE_MAP :: Iterator Type_iter;

TYPE_MAP MM;

TYPE_ITER IT;

Int ID;

PUBLIC:

Strmap1 (): IT (NULL), ID (0)

{

// init

ID = 0;

MM [I "] = ID;

MM ["YOU"] = ID;

MM ["HE"] = ID;

}

INT Find (const char * s)

{

COUT << "Find" << S << endl;

INT RET = 0;

IT = mm.find (s);

IF (mm.end ()! = it) {

RET = IT-> SECOND;

COUT << S << "'s id is" << Ret << ENDL;

} else {

COUT << "can't find" << s << "'s id" << endl;

}

Return Ret;

}

Int insert (const char * s)

{

Cout << "insert" << S << endl;

INT RET = ID;

mm.insert (TYPE_MAP :: Value_Type (S, RET));

// mm [s] = RET; // ok

Return Ret;

}

Void Remove (const char * s)

{

Cout << "remove" << s << endl;

mm.rase (s);

}

}

Int main (int Argc, char * argv [])

{

COUT << "(STRMAP1) STRING MAP 1 (Simple Use st :: Map )" << endl;

Strmap1 o;

Cout << Endl;

O.Find ("i");

Cout << Endl;

O.Find ("she");

Cout << Endl;

O.Find ("you");

O.Find ("HE");

Cout << Endl;

o.insert ("she");

O.Remove ("you");

O.Remove ("HE");

Cout << Endl;

O.Find ("you");

O.Find ("HE");

O.Find ("she");

Cout << Endl;

COUT << "Haha ~~~ Now Only i and she" << Endl;

Return 0;

}

- output - (strmap1) string map 1 (simple use std :: map ) find ii's id is 1find shecan't find she's idfind youyou's id is 2find hehe's id is 3insert sheremove youremove hefind youcan't Find you's idfind hecan't Find He's idfind she's id is 4haha ~~~ now Only i and she

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

New Post(0)