vector as key for a map

C

Christopher

I have a situation where I need to map object Bs by an entire vector
of other object As.
So, I need to make sure the vector has an operator < ?
I look up vector and it says, it compares "lexcographically". Look
that word up in the dictionary and it says "pertaining to lexography."
Not much help. As long as object A has comparison operators defined, I
should be Ok right? How does the map compare the vector?
 
C

Christopher

The std::map class template takes an optional third parameter that defines a
comparison class.


I know, but why write one if it already does what I think it does?
Question is, does it already do what I think it does?
 
J

joecook

I know, but why write one if it already does what I think it does?
Question is, does it already do what I think it does?

Lexicographical means compared the same way a dictionary entry would
be compared. It will look at the first element of each vector, and if
the first element is different, the comparison is done. Only if they
are equal, does it move on to the second parameter (and third, etc if
necessary).

How the elements are compared depends on the comparison operator of
the contained type (vector::value_type)

Joe Cook
 
D

Daniel Pitts

Christopher said:
I have a situation where I need to map object Bs by an entire vector
of other object As.
So, I need to make sure the vector has an operator < ?
I look up vector and it says, it compares "lexcographically". Look
that word up in the dictionary and it says "pertaining to lexography."
Not much help. As long as object A has comparison operators defined, I
should be Ok right? How does the map compare the vector?
Lexicographically means compare the first to the first, if equal,
compare second to the second, etc...

What did the dictionary say about lexicography?

Should your map key depend on the order of As, or just the set?
 
K

Kai-Uwe Bux

Christopher said:
I have a situation where I need to map object Bs by an entire vector
of other object As.
So, I need to make sure the vector has an operator < ?

No. You need to make sure that A has an operator<. Then vectors (in fact,
any sequence containers) of As will have an operator<.
I look up vector and it says, it compares "lexcographically".

Yup, that's how the operator< for sequences works (in terms of operator< for
the elements of the sequence).
Look
that word up in the dictionary and it says "pertaining to lexography."
Not much help. As long as object A has comparison operators defined, I
should be Ok right? How does the map compare the vector?

All that you need to know about lexicographic comparison in order to check
whether it is appropriate for keys in a map is that it induces a strict
weak order. You are in luck: it does.


Best

Kai-Uwe Bux
 
J

James Kanze

Lexicographically means compare the first to the first, if
equal, compare second to the second, etc...
What did the dictionary say about lexicography?

It says that it pertains to the compiling of dictionaries. So
in general use, lexicographic order would be dictionary order.
Except that dictionaries obey all kinds of wierd, locale
dependent rules. (Case insensitive, obviously, but Mc sorts as
Mac, or accented characters sort like the unaccented
equivalents, except when the values are otherwise identical, in
which case, the accented character sorts after the unaccented,
but the sorting starts at the end, and not the beginning...)

In fact, lexicographical order is a well defined mathematic
term; based originally on dictionary order (whence the name),
but given a strict, locale independent definition:

Given two partially ordered sets A and B, the
lexicographical order on the Cartesian product A × B is
defined as (a,b) ≤ (a′,b′) if and only if a < a′ or (a = a′
and b ≤ b′).

(From the Wikipedia, which in this case seems correct.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top