CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Threaded View

  1. #1
    Join Date
    Aug 2004
    Posts
    4

    Question STL map testing ??

    Code:
    #include <iostream>
    #include <ext/hash_map>
    #include <string>
    #include <map>
    #include <stdio.h>
    #include <string.h>
    
    using namespace std;
    
    class subInfo{
        public:
            float Weight;
            subInfo(){}
            subInfo(float _w){
                Weight = _w;
            }
            ~subInfo(){}
            bool operator<(const subInfo & rsh){
                return  Weight < rsh.Weight;
            }
    };
    
    typedef map<string , subInfo > _hash;
    
    void DeKey(const char* key,int & t, int & t2){
    
         memcpy(&t,key,sizeof(int));
         memcpy(&t2,key+sizeof(int),sizeof(int));
    
    }
    
    int main(){
    
    
        _hash hs;
        _hash::iterator find;
    
        int tid=1;
    
        for(int i=1;i<10;i++){
    
            char *key = new char[8];
            memset(key,0,8);
    
    
            memcpy(key,(char*)&tid,4);
            memcpy(key+sizeof(int),(char*)&i,4);
    
            cout<<"size:"<<sizeof(key)<<endl;
            cout<<"leng:"<<strlen(key)<<endl;
    
            find=hs.find(string(key));
            if(find == hs.end()){
                subInfo  sub((float)i);
                hs.insert(make_pair(string(key),sub));
                cout<<"key:["<<key<<"]inserted"<<endl;
            }else {
    
                int j=0,k=0;
                DeKey(find->first.c_str(),j,k);
                cout<<"key:["<<j<<k<<"] founded"<<endl;
                delete[] key;
            }
    
        }
        cout<<"----------------------------------------------------"<<endl;
        cout<<"hash size:"<<hs.size()<<endl;
        cout<<"----------------------------------------------------"<<endl;
    
       return 0;
    }
    // Q1. only one iterm was inserted. what's wrong??
    // Q2. when i finished inserting iterm, release key-memory ?
    Last edited by Mick; August 27th, 2004 at 06:58 AM. Reason: code tags

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured