473,769 Members | 5,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Re: Is a std::map<> ordered?

joseph cook wrote:
A map is always sorted using std::less
Not always. By default, yes, but you can specify other comparators, eg:

std::map<int, int, std::greaterrev ersedMap;
Nov 20 '08 #1
6 5438
On Nov 20, 7:48*am, Juha Nieminen <nos...@thanks. invalidwrote:
joseph cook wrote:
A map is always sorted using std::less

* Not always. By default, yes, but you can specify other comparators, eg:

std::map<int, int, std::greaterrev ersedMap;
Or at runtime:

std::map<int, intmyMap(myPred icate);

Ali
Nov 20 '08 #2
On 2008-11-20 14:40:19 -0500, ac******@gmail. com said:
On Nov 20, 7:48Â*am, Juha Nieminen <nos...@thanks. invalidwrote:
>joseph cook wrote:
>>A map is always sorted using std::less

Â* Not always. By default, yes, but you can specify other comparators, e
g:
>>
std::map<int , int, std::greaterrev ersedMap;

Or at runtime:

std::map<int, intmyMap(myPred icate);
Not really. There's a third type argument to std::map which specifies
the map's predicate type, with a default of std::less<T>. This
constructor takes an argument with the same type as the template's
predicate argument, so you can't pass arbitrary predicate objects. This
constructor is only useful with a user-defined predicate type that can
be initialized with something other than its default constructor.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 20 '08 #3
Pete Becker wrote:
On 2008-11-20 14:40:19 -0500, ac******@gmail. com said:
>On Nov 20, 7:48 am, Juha Nieminen <nos...@thanks. invalidwrote:
>>joseph cook wrote:
A map is always sorted using std::less

Not always. By default, yes, but you can specify other comparators, e
g:
>>>
std::map<in t, int, std::greaterrev ersedMap;

Or at runtime:

std::map<int, intmyMap(myPred icate);

Not really. There's a third type argument to std::map which specifies
the map's predicate type, with a default of std::less<T>. This
constructor takes an argument with the same type as the template's
predicate argument, so you can't pass arbitrary predicate objects. This
constructor is only useful with a user-defined predicate type that can
be initialized with something other than its default constructor.
Well, basically that just does mean that it depends on a runtime value.
Otherwise, you wouldn't need those constructor arguments in the first place.
Nov 21 '08 #4
ac******@gmail. com wrote:
std::map<int, intmyMap(myPred icate);
I don't think you can do that because the comparator template
parameter is set by default to std::less, and unless myPredicate casts
implicitly to type std::less, that won't work. You have to do it like:

std::map<int, int, MyPredicateType myMap(myPredica te);

If 'myPredicate' is a function, the syntax becomes awkward:

std::map<int, int, bool(*)(int, int)myMap(myPre dicate);

This becomes even more awkward if the key and data types of the map
are something more complicated than int.

The next standard will offer a tool to alleviate the problem:

std::map<int, int, decltype(myPred icate)myMap(myP redicate);
Nov 21 '08 #5
On 2008-11-21 06:34:27 -0500, Rolf Magnus <ra******@t-online.desaid:
Pete Becker wrote:
>On 2008-11-20 14:40:19 -0500, ac******@gmail. com said:
>>On Nov 20, 7:48 am, Juha Nieminen <nos...@thanks. invalidwrote:
joseph cook wrote:
A map is always sorted using std::less

Not always. By default, yes, but you can specify other comparators, e
g:

std::map<int , int, std::greaterrev ersedMap;

Or at runtime:

std::map<in t, intmyMap(myPred icate);

Not really. There's a third type argument to std::map which specifies
the map's predicate type, with a default of std::less<T>. This
constructor takes an argument with the same type as the template's
predicate argument, so you can't pass arbitrary predicate objects. This
constructor is only useful with a user-defined predicate type that can
be initialized with something other than its default constructor.

Well, basically that just does mean that it depends on a runtime value.
Otherwise, you wouldn't need those constructor arguments in the first place.
It means that std::map<int, intmyMap(myPred icate) is an error unless
the type of myPredicate is std::less<int>, in which cast it's
irrelevant. In particular, it is not a runtime replacement for
std::map<int, int, std::greater<in t>>.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 21 '08 #6
On 2008-11-21 08:46:16 -0500, Juha Nieminen <no****@thanks. invalidsaid:
ac******@gmail. com wrote:
>std::map<int , intmyMap(myPred icate);

I don't think you can do that because the comparator template
parameter is set by default to std::less, and unless myPredicate casts
implicitly to type std::less, that won't work. You have to do it like:

std::map<int, int, MyPredicateType myMap(myPredica te);

If 'myPredicate' is a function, the syntax becomes awkward:

std::map<int, int, bool(*)(int, int)myMap(myPre dicate);
Well, yes, and as we've seen in many Ginsu knife commercials, a normal
knife can't slice a tomato. The way to write this code is, of course,
with appropriate typedefs:

typedef bool (*pred)(int,int );
std::map<int, int, predmyMap(myPre dicate);
>
This becomes even more awkward if the key and data types of the map
are something more complicated than int.
Not at all. Again, typedefs.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 21 '08 #7

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

Similar topics

5
8748
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k) throw(std::runtime_error)
13
4638
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...
19
6164
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not found anything). Here's the problem, I have two sets of files, the name of a file contains a number which is unique for each set but it's possible (even probable) that two files in different sets have the same numbers. I want to store these...
1
3756
by: Maxwell | last post by:
Hello, I having having oodles of trouble using the std lib in my MC++ (VS.NET 2003) Class library. I figured out a simple sample to reproduce the errors I am having. Create a MC++ (VS.NET 2003) class library and type in the following code below: #include <map> #include<string>
10
2974
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The constructors and copy constructors in 'A' report when they are called. 'whoami' is just a unique identifier assigned to every object of type 'A'. The output of the program is: constructor 0 constructor 1
2
2260
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It uses map<and heavy overloading. The problem is, the output file differs from input, and for the love of me I can't figure out why ;p #include <iostream> #include <fstream> #include <sstream>
3
2940
by: newbie | last post by:
Same thing g++ complains when using hash_map<>, but is happy with map<--I understand hahs_map is not standardized, but since the compiler didn't complain something like 'hash_map<not defined', I suppose it's supported and should behave well when I used it correctly. BUT it didn't. Here is my code snippet: class MyKey { public: virtual void foo() { return; }
12
5837
by: jabbah | last post by:
Actually I'm quite sure I've missed something trivial here, but I just can't find it. Seemingly I cannot read from a const map& I try #include <iostream> #include <map> using namespace std;
2
2789
by: jabbah | last post by:
I have some data in a map and I want to sort it. Currently I have implemented it like this: #include <iostream> #include <map> #include <string> using namespace std; int main(){
0
9583
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
9423
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
10210
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
9990
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
9860
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7406
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
5297
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...
1
3955
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 we have to send another system
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.