打印

hash_map的问题请教?

hash_map的问题请教?

从网上下载了STLport-4.5.3,安装使用hash_map的功能。按网上的资料写了代码如下:

namespace __gnu_cxx
{
        template<> struct hash<const string>
        {
                size_t operator()(const string& s) const
                {
                        return __stl_hash_string(s.c_str());
                }
        };
        template<> struct hash<string>
        {
                size_t operator()(const string& s) const
                {
                        return __stl_hash_string(s.c_str());
                }
        };
}

class feature
{
public:
        u_int8_t fPackets;
       
};

int main()
{
        typedef hash_map<string,feature> flowhash;
        flowhash hm;
        flowhash::iterator itr;

        string s1 = "172.16.";
        feature f;
        f.fPackets = 10;

        hm.insert(pair<string,feature>(s1,f));
        itr = hm.begin();
        while(itr != hm.end())
        {
                cout << itr->first<<":" ;
                feature fs = (feature)itr->second;
                cout << fs.fPackets<<endl;
                itr ++;
        }
}
问题有二:
1、我必须把u_int8_t改成int,cout << fs.fPackets<<endl;语句才有输出?(好象u_int8_t只占一个字节吧?)
2、为什么需要前面的模板定义?(对C++不怎么懂,是不是因为hash_map没有内置对string进行hash的操作)

还请仁兄指教,先谢了!!!

TOP

1) i guess u_int8_t is a unsigned char type, in that case, if you output a char, 10 will be the ASCII character of the char... however, 10 in ASCII is not a real character.... so you'll not get anything in command line if you use 10...
2) Since hash_map is not part of the standard STL, there are certain other parts of the STL that it doesn't work with. You need to supply some extra code yourself, for the hash function to work correctly with other parts of the STL, which are standard across all implementations.

hope these answered your question

TOP

谢谢!
第一个问题确实是样,100输出的是'd'

TOP


感谢一直以来您对我们的支持!
当前时区 GMT+8, 现在时间是 2008-10-12 07:54 京ICP证060528 号

Designed By 17DST