Need Help on map <TCHAR*, long> 
Author Message
 Need Help on map <TCHAR*, long>

Hello,

I have defined a map  as follows:
map< TCHAR*, long > maptype;
maptype m;

I insert the elements as follows:

 if (!m.insert (maptype::value_type (_T("Harshal0"), 1)).second )
 {
  cout<< "Failed to insert 1" << endl;
 }
 if (!m.insert (maptype::value_type (_T("Harshal1"), 11)).second )
 {
  cout<< "Failed to insert 11" << endl;
 }
 if (!m.insert (maptype::value_type (_T("Harshal2"), 111)).second )
 {
  cout<< "Failed to insert 111" << endl;
 }
 if (!m.insert (maptype::value_type (_T("Harshal3"), 1111)).second )
 {
  cout<< "Failed to insert 1111" << endl;
 }

And finally search for elements in the inserted map as follows:

Iterator  = m.find(_T("Harshal3"));

I have a valid iterator returned here. However, If I search in the following
manner, I do not get the required element:

 _tcscpy (strl, _T("Harshal3"));
  Iterator  =  m.find(strl);

It returns m.end()

Could anyone please explain the behaviour of this?

Thank you and Regards,
Harshal Haridas



Sat, 04 Oct 2003 14:52:51 GMT  
 Need Help on map <TCHAR*, long>

Quote:

> I have defined a map  as follows:
> map< TCHAR*, long > maptype;

>  _tcscpy (strl, _T("Harshal3"));
>   Iterator  =  m.find(strl);

> It returns m.end()

> Could anyone please explain the behaviour of this?

You're searching for a pointer, not a string, and because your string has a
different pointer than the one you originally inserted, it won't find
it...the easiest solution overall is to not store pointers in your map,
rather, strings (the class), as those have the correct comparison operators
defined for what you are trying to do.

The second option is to supply a comparator for TCHAR*s as the third
template parameter for your map.

Ken



Sat, 04 Oct 2003 15:06:34 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. <<<<<<<Parsing help, please>>>>>>>>

2. File Format conversion, ascii freeform -->.csv <-->.wk1<-->dbf<-->?HELP

3. Help needed on std::map<>

4. Help needed on std::map<>

5. <<<>>>Need C code advice with functions and sorting.<<<>>>

6. >>I need help!<<(duh)

7. <><><>HELP<><><> PCMCIA Motorola Montana 33.6

8. >>>Windows Service<<<

9. >>>> NEED CAB SDK HELP <<<

10. <<<< C Grammar for yacc needed >>>>

11. Beginner mistake with <map>: help will be appreciated

12. <<ndbm.h>> - please help

 

 
Powered by phpBB® Forum Software