473,388 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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

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;
}
....
Jul 22 '05 #1
3 2761

"David" <da***********@post.rwth-aachen.de> wrote in message
news:e0*************************@posting.google.co m...
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
Jul 22 '05 #2
David wrote:
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, ...>

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #3
David wrote:
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, ...>

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sachin_mzn | last post by:
Hi, When we use find method over a STL map. which searching algorithm is used internally. Is it a hash search? Or map implementation internally decide depending on element. -Sachin
3
by: mcassiani | last post by:
Hi, I need use map faster as possible (I store in the map data about open network connections). First a question, this code fragment is from "The C++ Programming........
1
by: ictheion | last post by:
Hi, I am having a problem utilizing ofstream*'s stored in a map (map<char, ofstream*>). I'm having basically the same problem with a map<char, boost::mutex*>. My code compiles, but at run-time I...
10
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The...
3
by: newbie | last post by:
Same thing g++ complains when using hash_map<>, but is happy with map<--I understand hahs_map is not standardized, but since the compiler didn't complain something like 'hash_map<not defined', I...
2
by: DaTurk | last post by:
Hi, I'm trying to hold a map of ints,and function pointers in C++ map<int, (*functPtr)(int, int)something I need to hold a list of callbacks. For some reason this syntax is not working. Any...
4
jlm699
by: jlm699 | last post by:
I've looked at the other articles about maps of maps and am still stuck on this! I'm trying to basically make an enumeration of a data monitoring app. Instead of displaying numbers for certain...
7
by: puzzlecracker | last post by:
I need to be able to use map<char, T*>, I can make it std::map<std::string, T*>, however, Key is passed by char, hence I would need to call c_str() all the time. What do you suggest?
6
by: Juha Nieminen | last post by:
joseph cook wrote: Not always. By default, yes, but you can specify other comparators, eg: std::map<int, int, std::greaterreversedMap;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.