472,372 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,372 software developers and data experts.

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::greaterreversedMap;
Nov 20 '08 #1
6 5290
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::greaterreversedMap;
Or at runtime:

std::map<int, intmyMap(myPredicate);

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::greaterreversedMap;

Or at runtime:

std::map<int, intmyMap(myPredicate);
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<int, int, std::greaterreversedMap;

Or at runtime:

std::map<int, intmyMap(myPredicate);

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(myPredicate);
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, MyPredicateTypemyMap(myPredicate);

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

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

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(myPredicate)myMap(myPredicate);
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::greaterreversedMap;

Or at runtime:

std::map<int, intmyMap(myPredicate);

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(myPredicate) 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<int>>.

--
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(myPredicate);

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, MyPredicateTypemyMap(myPredicate);

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

std::map<int, int, bool(*)(int, int)myMap(myPredicate);
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(myPredicate);
>
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
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)...
13
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...
19
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...
1
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)...
10
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...
2
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...
3
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...
12
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
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(){
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.