Click to See Complete Forum and Search --> : STL map testing ??


ezuzz
August 25th, 2004, 02:53 AM
#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 ?

Mick
August 27th, 2004, 06:58 AM
<moved thread>

Philip Nicoletti
August 27th, 2004, 07:33 AM
In a couple places in your loop, you use :


string(key)


This will create a string from the char* key variable, however, it stops
when it sees a NULL (that's how it knows how many characters to
put in the string). I suspect that your first character is NULL. If this
is the case, try something like this:


string sKey(key,key+8);

find=hs.find(sKey);
if(find == hs.end()){
subInfo sub((float)i);
hs.insert(make_pair(sKey,sub));
cout<<"key:["<<key<<"]inserted"<<endl;
}else {

Kheun
August 27th, 2004, 07:43 AM
1. If you only inserting pairs which the keys are exactly the same. As string is null-terminated, your string becomes "\01". You may try stringstream to generate your key instead.

2. For all memory allocated through new, you have to delete them to avoid memory leak.


#include <iostream>
#include <sstream>
#include <map>

using namespace std;

typedef map<string , subInfo > _hash;

void DeKey(const string& key, int &t1, int &t2)
{
istringstream in(key);
in >> t1 >> t2;
}


int main(){

_hash hs;
_hash::iterator find;

int tid=1;

for(int i=1;i<10;i++){

ostringstream out;
out << tid << " " << i;

string key(out.str());
cout<<"length:"<<key.length()<<endl;


find=hs.find(key);

if(find == hs.end()){
subInfo sub((float)i);
hs.insert(make_pair(key,sub));
cout<<"key:["<<key<<"]inserted"<<endl;
}else {

int j=0,k=0;
DeKey(find->first,j,k);
cout<<"key:["<<j<<k<<"] founded"<<endl;
}

}
cout<<"----------------------------------------------------"<<endl;
cout<<"hash size:"<<hs.size()<<endl;
cout<<"----------------------------------------------------"<<endl;

return 0;
}