L
lokki
Hello,
can anybody tell me what's wrong with following example code?
char *k, *v;
k = new char[3];
strcpy(k, "a2");
v = new char[10];
strcpy(v, "987654321");
mp.insert(std:
air<const char*, const char*>(k, v));
std::cout << "map size " << mp.size() << std::endl;
// find
TStringMapConstIterator i = mp.find("a1");
if (i != mp.end())
{
std::cout << " found: " << i->second << std::endl;
}
else
{
std::cout << "not found" << std::endl;
}
// remove
for (TStringMapIterator j = mp.begin(); j != mp.end(); j++)
{
delete[] (*j).first;
delete[] (*j).second;
}
mp.clear();
std::cout << "deleted" << std::endl;
I'm using gnu c++ compiler and there are few typedefs before this code
to ensure proper comparison and hash computing of const char* in
hash_map
namespace __gnu_cxx
{
template<> struct hash<std::string>
{
size_t operator()(const std::string & __s) const
{ return __stl_hash_string(__s.c_str()); }
};
struct tmt_eq_str__
{
bool operator() (const char *a, const char *b) const
{
return !strcmp(a, b);
}
};
}
typedef __gnu_cxx::hash_map<const char*, const char*,
__gnu_cxx::hash<const char *>, __gnu_cxx::tmt_eq_str__> TStringMap;
typedef __gnu_cxx::hash_map<const char*, const char*,
__gnu_cxx::hash<const char *>,
__gnu_cxx::tmt_eq_str__>::const_iterator TStringMapConstIterator;
typedef __gnu_cxx::hash_map<const char*, const char*,
__gnu_cxx::hash<const char *>, __gnu_cxx::tmt_eq_str__>::iterator
TStringMapIterator;
The problem is removal of the elements. Both key and value pairs were
created by new operator. That's why I assumed, that they should be
deleted before callig mp.clear() function.
Problem is that program hangs in infinite loop when deleting elements.
There wasn't such problem when compiling similar code under VC++ 8.0
Thanks in adavance
can anybody tell me what's wrong with following example code?
char *k, *v;
k = new char[3];
strcpy(k, "a2");
v = new char[10];
strcpy(v, "987654321");
mp.insert(std:
std::cout << "map size " << mp.size() << std::endl;
// find
TStringMapConstIterator i = mp.find("a1");
if (i != mp.end())
{
std::cout << " found: " << i->second << std::endl;
}
else
{
std::cout << "not found" << std::endl;
}
// remove
for (TStringMapIterator j = mp.begin(); j != mp.end(); j++)
{
delete[] (*j).first;
delete[] (*j).second;
}
mp.clear();
std::cout << "deleted" << std::endl;
I'm using gnu c++ compiler and there are few typedefs before this code
to ensure proper comparison and hash computing of const char* in
hash_map
namespace __gnu_cxx
{
template<> struct hash<std::string>
{
size_t operator()(const std::string & __s) const
{ return __stl_hash_string(__s.c_str()); }
};
struct tmt_eq_str__
{
bool operator() (const char *a, const char *b) const
{
return !strcmp(a, b);
}
};
}
typedef __gnu_cxx::hash_map<const char*, const char*,
__gnu_cxx::hash<const char *>, __gnu_cxx::tmt_eq_str__> TStringMap;
typedef __gnu_cxx::hash_map<const char*, const char*,
__gnu_cxx::hash<const char *>,
__gnu_cxx::tmt_eq_str__>::const_iterator TStringMapConstIterator;
typedef __gnu_cxx::hash_map<const char*, const char*,
__gnu_cxx::hash<const char *>, __gnu_cxx::tmt_eq_str__>::iterator
TStringMapIterator;
The problem is removal of the elements. Both key and value pairs were
created by new operator. That's why I assumed, that they should be
deleted before callig mp.clear() function.
Problem is that program hangs in infinite loop when deleting elements.
There wasn't such problem when compiling similar code under VC++ 8.0
Thanks in adavance