473,395 Members | 1,343 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,395 software developers and data experts.

STL's key_comp()

26
I was going through the stl::key_comp() function and found it's implementation to be pretty straight forward when it came implementation on integers.

For eg:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4.  
  5. int main ()
  6. {
  7.   set<int> myset;
  8.   set<int>::key_compare mycomp;
  9.   set<int>::iterator it;
  10.   int i,highest;
  11.  
  12.   mycomp = myset.key_comp();
  13.  
  14.   for (i=0; i<=5; i++) myset.insert(i);
  15.  
  16.   cout << "myset contains:";
  17.  
  18.   highest=*myset.rbegin();
  19.   it=myset.begin();
  20.   do {
  21.     cout << " " << *it;
  22.   } while ( mycomp(*it++,highest) );
  23.  
  24.   cout << endl;
  25.  
  26.   return 0;
  27. }
  28.  
would give out the result [0,1,2,3,4,5] on comparing each of the integers in the set.

I wanted to try the same on a higher-level data straucture such as a structure of type:
Expand|Select|Wrap|Line Numbers
  1. struct node
  2. {
  3.        int a,b;
  4. };
but had some problems. It gives me an error whenever i want to insert a set element, as the default comparison function does not allow it.

So, how do I use key_comp() and value_comp() to define our own comparison function??
Sep 10 '09 #1
3 3289
Banfa
9,065 Expert Mod 8TB
You probably need to define some of the comparison functions (operator== for example) on or for your struct.

Or define a binary predicate comparison class that can be passed to set as a template parameter when you want to create a set of those structures.

The basic point is that set contins unique entries so it needs to be able to compare the objects it stores, it can easily do that for the basic types but for user defined types you need to provide the set with a way of comparing different instances of the type.

Read this
Sep 10 '09 #2
newb16
687 512MB
Set requires operator< to be defined on keys, and to function properly (i.e. object considered by the container equal to itself and not equal to an object that differs from it), condition !(a < b) && !(b<a) must be true only if a==b. std::pair for example, compares the first items of both arguments and only if they are equal, compares the second items.
Sep 10 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
Set requires operator< to be defined on keys,
Or, you write a binary predicate function that takes two arguments of the set type and returns a bool. When you create the set object there is a constructor that takes a pointer to this function for later use when you need to compare objects in the set.
Sep 10 '09 #4

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

Similar topics

41
by: Allan Bruce | last post by:
Hi there, I am interested to know if there are any C++ programmers who do not use STL. I have been coding in C++ for a year or so now, although only recently have I started a large(ish)-scale...
2
by: Steve | last post by:
Just wondering if someone can point me to a good, easy to understand online tutorial on STL functions. All the programming manuals I have predate STL, and I would like to learn more about modern...
25
by: Norm Matloff | last post by:
Anyone have an answer--just a guess would do--as to what percentage of programmers who do their primary work in C++ make use of the STL? Norm Matloff
13
by: Alvin | last post by:
Correct me if I am wrong, but the STL standard only defines the interface only and not the implementation, right? For example, say I use a std::map. The speed and efficiency of the operator(key)...
11
by: RR | last post by:
I have Plauger's book on STL and I've found that it's different from the STL I have on a Linux box (libstdc++-2.96-112). In particular, the map::erase(iterator) method I have returns nothing...
0
by: Tony Johansson | last post by:
Hello! I have two classes called Handle which is a template class and a class Integer which is not a template class. The Integer class is just a wrapper class for a primitive int with some...
0
by: rajd99 | last post by:
Hi, I am getting the following error while compiling with sun C++ 5.5 and using SGI STL. Has anyone run into this. The SGI headers are one at http://www.sgi.com/tech/stl/download.html Thanks, ...
20
by: Andrew Roberts | last post by:
Any more info on this? Anyone know when it will be released?
6
by: Bart Simpson | last post by:
I am writing a shared object (Dll in Windows) and I am making extensive use of the STL. I am getting lots of warnings like this: ClassA.h(139): warning C4251: 'ClassA::indexarray' : class...
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...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...

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.