473,466 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

STL map.find problem in using a compare class

3 New Member
Hi all, I am new to STL and I have a problem with my map. I made a class(Sort) for comparison on the map but the problem is map.find will not work.

Can someone explain why find was not able to locate the key?

here is the code:
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<map>
  3. #include<string>
  4.  
  5. using namespace std;
  6. class Sort
  7. {
  8.     public:
  9.         bool operator() (string const &_A, string const &_B) const
  10.         {
  11.             double num1, num2;
  12.             num1 = atoi(_A.c_str());
  13.             num2 = atoi(_B.c_str());
  14.             if( num1 && num2 ) {
  15.                 if( num1 <= num2 )
  16.                     return true;
  17.                 else return false;
  18.             }
  19.             else {
  20.                 if( _A.compare(_B) <= 0 ) return true;
  21.                 else return false;
  22.             }
  23.         }
  24. };
  25.  
  26. typedef map<string, string, Sort> EnumMap;
  27.  
  28. int main()
  29. {
  30.   string str="A";
  31.   EnumMap months;
  32.   EnumMap::iterator it;
  33.   months["1"] = "January";
  34.   months["7"] = "July";
  35.   months["2"] = "February";
  36.   months["3"] = "March";
  37.   months["4"] = "April";
  38.   months["5"] = "May";
  39.   months["6"] = "June";
  40.   months["8"] = "August";
  41.   months["9"] = "September";
  42.   months["10"] = "October";
  43.   months["11"] = "November";
  44.   months["12"] = "December";
  45.  
  46.   for ( it=months.begin() ; it!=months.end(); it++ ) {
  47.         cout << (*it).first << "==>" << (*it).second << endl;
  48.   }
  49.  
  50.   EnumMap::iterator t2= months.find("7");
  51.   if(t2!=months.end())
  52.     cout << "Found" << endl;
  53.   else cout << "Not Found" << endl;
  54. }
Apr 28 '10 #1

✓ answered by newb16

Comparison operator for map is '<' - strictly less. It checks equality as !(A<B) && !(B<A). So this
# if( num1 <= num2 )
# return true;
should be num1 < num2.

btw, my cygwin compiler doesn't like _B and thinks it's an integer constant.
ctype.h : #define _B 0200

5 5508
newb16
687 Contributor
Comparison operator for map is '<' - strictly less. It checks equality as !(A<B) && !(B<A). So this
# if( num1 <= num2 )
# return true;
should be num1 < num2.

btw, my cygwin compiler doesn't like _B and thinks it's an integer constant.
ctype.h : #define _B 0200
Apr 28 '10 #2
hype261
207 New Member
Why are you using std::map<std::string, std::string>? Since all your keys are integers it would be easier to use std::map<int, std::string> and it would be quicker also.
Apr 28 '10 #3
hagdanan
3 New Member
@hype261
Because sometimes we use keys that are not in integer form. And map do sort the keys when we insert on it, so if I use keys as "1", "2", "3", "10".. this will not be arrange numerically(since its string) as expected to be 1, 2, 3 and 10.. but this will be arranged alphabetically as "1", "10","2","3". That's why I have this class to check if the string can be converted to integer and compare as integer so I can have the correct sequence.
Apr 29 '10 #4
hagdanan
3 New Member
@newb16
Thanks a lot.

but I am wondering about the effect of the Sort class on the find method. I need to return false so that find will work?
Apr 29 '10 #5
newb16
687 Contributor
You need to return true only if A is less than B. If they are equal return false.
Apr 29 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: agrsaurabh | last post by:
Hello Everybody, I have created an AVI Player using DirectShow. This player is working fine. I have also created a View Window (with class name CViewWnd) using CFrameWnd class. This view...
1
by: TF | last post by:
I am developing a Windows Application in VB.NET that is using two class libraries CL_A (contains class A) & CL_B (contains class B). Also CL_A is used in CL_B. Now in my application when i try...
2
by: Paul Darroch | last post by:
Hello I am trying to convert a web site from ASP to ASP.NET and the class attribute is used in tables throughout the site. The problem is when I am trying to convert these pages I have a huge...
3
by: Paul Bromley | last post by:
I know this may seem a stupid question, but would value a reply. I write certain path settings to an ini file and use a class to read these settings into at startup of a project. Throughout the use...
2
by: fineman | last post by:
Hi all, I want to get a 64bit(8 bytes) Encrypt result use DES class in the VS2005. Though I encrypt data is 64bit(8 bytes), but DES return encrypt result that always is 128bit(16 bytes), I don't...
2
by: prasenjit2007 | last post by:
Hello, can u help me sending Email with attachments using the Class phpMailer. On the website I have a link to an html form in which I input the parameters with the names 1)from(textbox name)...
7
by: Sin Jeong-hun | last post by:
I have created a class library assembly (.dll) file. At the main GUI application, this assembly is referenced and used. But the problem is, if I redistribute these files with my application,...
3
by: rajanipro | last post by:
Hi buddies! Can you tell me how to invoke base class static method hidden by inheritance, using derived class as in the following case? using System; class ParentClass { public static...
6
by: webinfinite | last post by:
Hi, How to find a STL class size, for example: string. I would like to know what the size of the implementation of this class, I am not interested in the size of its object but the class itself....
1
by: trdmrk | last post by:
Hello, Ive found this site very helpful in the past, so i thought i may ask for help. I am developing an application for a school project. I am in the middle of it, I get the error:...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
1
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.