elements not inserted into map<char*, ..> ?

D

David

Hello,
I try to store PROLOG_TERMs in a map which is again
stored in another map, to realise this I am using a
composite key of two strings.
The class compiles fine, but when I insert an element,
the "top level" map, i.e. the one accessed via 'key2'
is either not found or the term has not been inserted,
since the iteration does not produce any output nor
does the method return the correct value, i.e. 'FAIL'.

I already tried several alternative implementations with
'find(keyX)' none of which worked. Can anybody help please?

thanks beforehand,
David Kensche

typedef map<char*, PROLOG_TERM> termMap;
typedef map<char*, termMap > clusterMap;

class TermCache {
clusterMap clusters;

public:
/**
* Stores a copy of the PROLOG_TERM 'term' associated to
* double key (key1, key2). Fails with an error if there
* is already a value associated to the double key.
*/
int pc_record(char* key1, char* key2, PROLOG_TERM term) {
int solve = FAIL;
termMap cluster = clusters[key2];
termMap::const_iterator termKeyPair;
for(termKeyPair = cluster.begin(); termKeyPair != cluster.end();
++termKeyPair) {
cout << "current key == " << termKeyPair->first << "\n";
}
if(cluster.find(key1) == cluster.end()) {
cout << "cluster.find(" << key1 << ") == cluster.end(), new key,
thus succeed\n";
cluster.insert(termMap::value_type(key1, term));
solve = SUCCEED;
}
cout << "solve == " << solve << "\n";
return solve;
}
....
 
J

John Harrison

David said:
Hello,
I try to store PROLOG_TERMs in a map which is again
stored in another map, to realise this I am using a
composite key of two strings.
The class compiles fine, but when I insert an element,
the "top level" map, i.e. the one accessed via 'key2'
is either not found or the term has not been inserted,
since the iteration does not produce any output nor
does the method return the correct value, i.e. 'FAIL'.

I already tried several alternative implementations with
'find(keyX)' none of which worked. Can anybody help please?

The problem is that you are inserting in a copy of the map inside the map.
See fix below
thanks beforehand,
David Kensche

typedef map<char*, PROLOG_TERM> termMap;
typedef map<char*, termMap > clusterMap;

class TermCache {
clusterMap clusters;

public:
/**
* Stores a copy of the PROLOG_TERM 'term' associated to
* double key (key1, key2). Fails with an error if there
* is already a value associated to the double key.
*/
int pc_record(char* key1, char* key2, PROLOG_TERM term) {
int solve = FAIL;
termMap cluster = clusters[key2];

cluster is a *copy* of the map in clusters. To fix use a reference

termMap& cluster = clusters[key2];

now you aren't copying the map.

john
 
P

Peter van Merkerk

David said:
Hello,
I try to store PROLOG_TERMs in a map which is again
stored in another map, to realise this I am using a
composite key of two strings.
The class compiles fine, but when I insert an element,
the "top level" map, i.e. the one accessed via 'key2'
is either not found or the term has not been inserted,
since the iteration does not produce any output nor
does the method return the correct value, i.e. 'FAIL'.

I already tried several alternative implementations with
'find(keyX)' none of which worked. Can anybody help please?

The problem with std::map<char*, ...>, is that the lookup is based on
the pointer value, not on the string the pointer is pointing to. So
therefore the lookup will always fail, because even when two strings are
equal the pointers to those strings will be different. If you use
std::string as key it should work. In other words try:

std::map<std::string, ...>
 
P

Peter van Merkerk

David said:
Hello,
I try to store PROLOG_TERMs in a map which is again
stored in another map, to realise this I am using a
composite key of two strings.
The class compiles fine, but when I insert an element,
the "top level" map, i.e. the one accessed via 'key2'
is either not found or the term has not been inserted,
since the iteration does not produce any output nor
does the method return the correct value, i.e. 'FAIL'.

I already tried several alternative implementations with
'find(keyX)' none of which worked. Can anybody help please?

The problem with std::map<char*, ...>, is that the lookup is based on
the pointer value, not on the string the pointer is pointing to. So
therefore the lookup will always fail, because even when two strings are
equal the pointers to those strings will be different. If you use
std::string as key it should work. In other words try:

std::map<std::string, ...>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top