map
来源:互联网 发布:淘宝空间图片传不上去 编辑:程序博客网 时间:2024/05/02 02:26
/*RedHat Linux 7.2 + gcc(g++)2.96
--源程序--
// strmap1.cpp
//
*/
#pragma warning(disable:4786)
//...
#include <map>
#include <string>
//...
#include <iostream>
using namespace std;
class strmap1
{
typedef std::map<std::string, int> 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.erase(s);
}
};
int main(int argc, char* argv[])
{
cout << "(strmap1)string map 1(simple use std::map<string, int>)" << 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;
}