473,757 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with std::map find()

Hi everybody,

This should be trivial, but I just can't see it.
The following statement in a method doesn't work for me:
(there's only one entry in myMap)

map<const char*, int>::const_ite rator it = myMap.find(key) ;

where key is a const char* passed by the class method.

However, if I do:

map<const char*, int>::const_ite rator it = registry.begin( );

Then the iterator points to the right value, and (*it).first displays the
right key.
I have checked the key value being passed to find() and is correct, so that
shouldn't be a problem...
Any hints?

Cheers

--
=============== =============== =============== =============== ============
Manuel Diaz-Gomez | ATLAS Bldg. 32/SB-008 tel. +41 22 76 76304
CERN EP Division
CH-1211 Geneva 23
SWITZERLAND
=============== =============== =============== =============== ============
Jul 22 '05 #1
3 7459
>map<const char*, int>::const_ite rator it = myMap.find(key) ;

Your map compares C-string addresses, not C-string contents
Jul 22 '05 #2
Manuel Maria Diaz Gomez wrote:
Hi everybody,

This should be trivial, but I just can't see it.
The following statement in a method doesn't work for me:
(there's only one entry in myMap)

map<const char*, int>::const_ite rator it = myMap.find(key) ;

where key is a const char* passed by the class method.

However, if I do:

map<const char*, int>::const_ite rator it = registry.begin( );

Then the iterator points to the right value, and (*it).first displays the
right key.
I have checked the key value being passed to find() and is correct, so that
shouldn't be a problem...
Any hints?


What Walter said.

Use std::string instead of const char* as the key type.
Jul 22 '05 #3
That solved the problem!!

Thanks!

Manuel

"Manuel Maria Diaz Gomez" <Ma************ *********@cern. ch> wrote in message
news:c6******** **@sunnews.cern .ch...
Hi everybody,

This should be trivial, but I just can't see it.
The following statement in a method doesn't work for me:
(there's only one entry in myMap)

map<const char*, int>::const_ite rator it = myMap.find(key) ;

where key is a const char* passed by the class method.

However, if I do:

map<const char*, int>::const_ite rator it = registry.begin( );

Then the iterator points to the right value, and (*it).first displays the
right key.
I have checked the key value being passed to find() and is correct, so that shouldn't be a problem...
Any hints?

Cheers

--
=============== =============== =============== =============== ============
Manuel Diaz-Gomez | ATLAS Bldg. 32/SB-008 tel. +41 22 76 76304
CERN EP Division
CH-1211 Geneva 23
SWITZERLAND
=============== =============== =============== =============== ============

Jul 22 '05 #4

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

Similar topics

24
17324
by: Duane Hebert | last post by:
2 questions: Given a map defined as std::map<int,string> stringmap; //How do you access the data_type (string) via an iterator? std::string spoo("doh");
44
8803
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice, I chose to use std::map. However, the program runs at less than half the speed of an equivalent program that uses ordinary handcoded binary trees. The result is not changed very much if I replace std::string as the key with a simple string class...
4
1460
by: Grey Plastic | last post by:
I do not understand why this code fails to compile (under gcc): #include <map> using namespace std; class Foo { map<Foo*,int> myMap; public: int lookup(const Foo& f) const { myMap.find(&f); } };
14
4061
by: Flzw | last post by:
Well I have a map like this : std::map <string, CObject> ObjectList; I have a function like this : CObject* NewObject( char* Name, CArg* Arg) { std::string key = Name; ObjectList = CObject( Name, Arg);
13
4636
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and which also implements ==, <, <=, >, >=, etc. (Those operators are tested and working correctly.) If I assign map = "world", it saves the MyString's correctly in the map. But a subsequent call to map.find("hello") returns map.end(). Even more...
13
9674
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
2
2401
by: mergaite_lietuvaite | last post by:
Hi, I try to use map. My code is very easy, but there are some errors.... (by compiling). Plaese, could you say, what I do wrong.. struct less_array { bool operator()(const FArray p1, const FArray p2) const { return p1 < p2 ;
7
6262
by: DevNull | last post by:
Hi there everyone, I'm creating a very simple immediate mode command interpreter. The final purpose is to provide a pluggable control and command console for a MUD server I have written. The basic theory is we wrap the functions we want exposed to the console in a function with a prototype of int func(State*)
7
5838
by: Matthias Pfeifer | last post by:
Hi there, a std::map is build from std::pair elements where for a pair p p.first is called the key and p.second the value. Is there a way to keep the map sorted by the values? Ie is there a std::key_compare in the stl that offeres this functionality? It's ok if this would make finding a key more expensive. Writing my own would be possible, but why do it if it's already there... matthias
8
4074
by: mveygman | last post by:
Hi, I am writing code that is using std::map and having a bit of an issue with its performance. It appears that the std::map is significantly slower searching for an element then a sequential search in a vector. Has anyone run into this before?
0
9487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9297
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10069
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.