C++ STL MAP 使用方法与应用介绍(一) (二)

2014-11-24 02:37:46 · 作者: · 浏览: 3
element:192.168.200.33
=================================
| 192.168.0.128 | uestc |
| 192.168.200.3 | EE |
| 192.168.30.23 | CT |
=================================

Process returned 0 (0x0) execution time : 1.080 s
Press any key to continue.

*****************/

#include
#include
#include
using namespace std;
void map_insert(map < string, string > *mapStudent, string index, string x)
{
mapStudent->insert(map < string, string >::value_type(index, x));
}

int main(int argc, char **argv)
{
char tmp[32] = "";
map < string, string > mapS;

//insert element
map_insert(&mapS, "192.168.0.128", "uestc");
map_insert(&mapS, "192.168.200.3", "EE");
map_insert(&mapS, "192.168.200.33", "CS");

map < string, string >::iterator iter;

cout << "We Have Third Element:" << endl;
cout << "-----------------------------" << endl;

//find element
iter = mapS.find("192.168.0.33");
if (iter != mapS.end())
{
cout << "find the elememt" << endl;
cout << "It is:" << iter->second << endl;
}
else
{
cout << "not find the element" << endl;
}

//see element
for (iter = mapS.begin(); iter != mapS.end(); iter++ )
{

cout << "| " << iter->first << " | " << iter->
second << " |" << endl;

}
cout << "-----------------------------" << endl;

map_insert(&mapS, "192.168.30.23", "CT");

cout << "After We Insert One Element:" << endl;
cout << "-----------------------------" << endl;
for (iter = mapS.begin(); iter != mapS.end(); iter++ )
{

cout << "| " << iter->first << " | " << iter->
second << " |" << endl;

}

cout << "-----------------------------" << endl;

//delete element
iter = mapS.find("192.168.200.33");
if (iter != mapS.end())
{
cout << "find the element:" << iter->first << endl;
cout << "delete element:" << iter->first << endl;
cout << "=================================" << endl;
mapS.erase(iter);
}
else
{
cout << "not find the element" << endl;
}
for (iter = mapS.begin(); iter != mapS.end(); iter++ )
{

cout << "| " << iter->first << " | " << iter->
second << " |" << endl;

}
cout << "=================================" << endl;

return 0;
}
/***************
We Have Third Element:
-----------------------------
not find the element
| 192.168.0.128 | uestc |
| 192.168.200.3 | EE |
| 192.168.200.33 | CS |
-----------------------------
After We Insert One Element:
-----------------------------
| 192.168.0.128 | uestc |
| 192.168.200.3 | EE |
| 192.168.200.33 | CS |
| 192.168.30.23 | CT |
-----------------------------
find the element:192.168.200.33
delete element:192.168.200.33
=================================
| 192.168.0.128 | uestc |
| 192.168.200.3 | EE |
| 192.168.30.23 | CT |
=================================

Process returned 0 (0x0) execution time : 1.080 s
Press any key to continue.

*****************/