473,399 Members | 2,278 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,399 software developers and data experts.

newbie question:why does this code work...

hi all...
i found this code in some past discussion on this forum.i do not understand why operator overloading is necessary.if i remove it iam getting errors...
Expand|Select|Wrap|Line Numbers
  1. #include <map>
  2.  
  3. class Keys {
  4. public:
  5. Keys(int k1, int k2)  
  6. {
  7.       key1=k1;
  8.       key2=k2;
  9.  }
  10. bool operator<(const Keys &right) const {
  11. return (key1 < right.key1 && key2 < right.key2);
  12. }
  13. int key1;
  14. int key2;
  15. };
  16.  
  17. int main() {
  18. std::map<Keys, int> mymap;
  19. mymap.insert(std:air<Keys, int>(Keys(3, 8), 5));
  20. return 0;
  21. }
Apr 22 '07 #1
3 1129
weaknessforcats
9,208 Expert Mod 8TB
You are using a map of Keys objects. A map is a tree of ordered values. The map insert needs to know how to compare objects by calling the < operator to preserve the order.
Apr 22 '07 #2
i get the gist of what you say.....
but plzzzzz can u explain more about the ordering a lil bit.....
it would be very helpful....
Apr 22 '07 #3
thank you for the previous explanation...
Apr 22 '07 #4

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

Similar topics

2
by: Vitali Gontsharuk | last post by:
Hi! I can't find an answer to the following question: does the "import" command in XML Schema import also element declarations? Or only type definitions? As far as I understood, the first takes...
6
by: Peter Oliphant | last post by:
I just discovered that the ImageList class can't be inherited. Why? What could go wrong? I can invision a case where someone would like to add, say, an ID field to an ImageList, possible so that...
4
by: Scott F. Brown | last post by:
Greetings all... I was playing around with compressing streams and came across a behavior that I do not understand. I create a stream (input) from the contents of a textbox. That stream is...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
7
by: gokkog | last post by:
Hello, Recently I have the book Programming Pearls. Nice read! Perhaps it is weekend, I cannot understand the following codes well: #define BITSPERWORD 32 #define SHIFT 5 #define MASK 0x1F...
5
by: r_ahimsa_m | last post by:
Hello, I am learning PHP5. I would like to ask experienced programmers if you use PHP5 code obfuscation? I am writing website in Linux and I consider using some code obfuscator because I don't...
12
by: Philipp.Weissenbacher | last post by:
Hi all! This is most certainly a total newbie question, but why doesn't the following code cause a segfault? void insertion_sort(int a, int length) { int i; for (i=0; i < length; i++) { int...
3
by: =?ISO-8859-1?Q?Arne_Vajh=F8j?= | last post by:
Russell Mangel wrote: You have a MsgFile object and it does not implement INoteItem, so you can not cast to it.
2
by: Ken Fine | last post by:
I have a question about ASP.NET output caching. I want to use screen scraping as a temporary hack to pull in some complex Classic ASP-rendered content into some ASP.NET pages: protected String...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...

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.