Topic: Comparing tuples of different lengths
Author: =?ISO-8859-1?Q?Joaqu=EDn_M_L=F3pez_Mu=F1oz?= <joaquin@tid.es>
Date: Wed, 21 Jan 2009 15:24:20 CST Raw View
The current C++0x standard draft requires that two tuples t and u have
the same length in order to be comparable with operator <. As I see
it, it would be a consistent and natural extension to allow t and u to
be compared in the following way when their lengths differ:
t < u iff t' < u', where t' is the tuple holding the m first
elements of t, with m=min(length(t),length(u)), and similarly
with u'.
This is entirely analogous to the way strings are compared when they
do not have the same length.
Such an extension would allow us to do things like, for instance, the
following:
// table of (family name,given name) entries
std::vector<std::tuple<std::string,std::string> > names;
...
std::sort(names);
// find all Smiths
std::equal_range(
names.begin(),names.end(),
std::make_tuple("Smith"));
Anyone else finds this extension useful?
Joaqu n M L pez Mu oz
Telef nica, Investigaci n y Desarrollo
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Balog Pal" <pasa@lib.hu>
Date: Thu, 22 Jan 2009 20:40:30 CST Raw View
"Joaqu n M L pez Mu oz" <joaquin@tid.es>
> The current C++0x standard draft requires that two tuples t and u have
> the same length in order to be comparable with operator <. As I see
> it, it would be a consistent and natural extension to allow t and u to
> be compared in the following way when their lengths differ:
>
> t < u iff t' < u', where t' is the tuple holding the m first
> elements of t, with m=min(length(t),length(u)), and similarly
> with u'.
>
> This is entirely analogous to the way strings are compared when they
> do not have the same length.
I don't think it is.
strings: "abcd" < "abcde"
your method would make them ==
> // table of (family name,given name) entries
> std::vector<std::tuple<std::string,std::string> > names;
> // find all Smiths
> std::equal_range(
> names.begin(),names.end(),
> std::make_tuple("Smith"));
It works for this very special case, but in general it would be confusing.
As one could think comparing different tuples is an error, others would thnk
the result is as your stated string analogy.
OTOH there could be factory predicates that do what you say, and could be
explicitly asked for instead of less<>.
> Anyone else finds this extension useful?
If it wasn't already in the tuple package I doubt it is std:: worthy at the
moment, but you can create it as a lib in boost:: and see how it fares -- if
popular could fit TR2 or something.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Howard Hinnant <howard.hinnant@gmail.com>
Date: Thu, 22 Jan 2009 20:58:21 CST Raw View
On Jan 21, 1:24 pm, Joaqu n M L pez Mu oz <joaq...@tid.es> wrote:
> The current C++0x standard draft requires that two tuples t and u have
> the same length in order to be comparable with operator <. As I see
> it, it would be a consistent and natural extension to allow t and u to
> be compared in the following way when their lengths differ:
>
> t < u iff t' < u', where t' is the tuple holding the m first
> elements of t, with m=min(length(t),length(u)), and similarly
> with u'.
>
> This is entirely analogous to the way strings are compared when they
> do not have the same length.
>
> Such an extension would allow us to do things like, for instance, the
> following:
>
> // table of (family name,given name) entries
> std::vector<std::tuple<std::string,std::string> > names;
> ...
> std::sort(names);
>
> // find all Smiths
> std::equal_range(
> names.begin(),names.end(),
> std::make_tuple("Smith"));
>
> Anyone else finds this extension useful?
Yes, although with some caveats. I prefer to use the definition of
lexicographical_compare ([alg.lex.comparison]) which is slightly
different than your definition (specifically: If one sequence is a
pre x of the other, then the shorter sequence is lexicographically
less than the longer sequence). And I prefer to include ==/!= in this
logic as well. I coded this logic into the CodeWarrior tuple as an
extension and am disappointed that this logic is not in the draft
standard. It seems obvious and logical to compare tuples of different
lengths and give the correct answer instead of fail to compile.
-Howard
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: SG <s.gesemann@gmail.com>
Date: Thu, 22 Jan 2009 20:57:44 CST Raw View
On 21 Jan., 22:24, Joaqu n M L pez Mu oz <joaq...@tid.es> wrote:
> The current C++0x standard draft requires that two tuples t and u have
> the same length in order to be comparable with operator <. As I see
> it, it would be a consistent and natural extension to allow t and u to
> be compared in the following way when their lengths differ:
>
> t < u iff t' < u', where t' is the tuple holding the m first
> elements of t, with m=min(length(t),length(u)), and similarly
> with u'.
>
> This is entirely analogous to the way strings are compared when they
> do not have the same length.
The relation '<' (logically) implies all the other ordering relations
including equality. If that what you said was true the strings "abc"
and "abcd" would be equal. If you made these two strings equal you
would not be able to add both strings to the same std::set for
example. That's bad. I don't want some strings of unequal length to be
equal.
Using G++ 3.4.5 string("abc")<string("abcd") yields true.
> Such an extension would allow us to do things like, for instance, the
> following:
>
> // table of (family name,given name) entries
> std::vector<std::tuple<std::string,std::string> > names;
> ...
> std::sort(names);
>
> // find all Smiths
> std::equal_range(
> names.begin(),names.end(),
> std::make_tuple("Smith"));
>
> Anyone else finds this extension useful?
No, I'm not convinced. I mean it would probably not break things. But
its use is at least questionable. IMHO, you should use your own
comparator with your own idea of equality in that case. Maybe with the
help of a lambda function:
typedef std::tuple<std::string,std::string> entry;
std::vector<entry> names;
:
std::equal_range(
names.begin(),names.end(),
"Smith",
[](entry const& x, char const* psz) { return get<0>(x) <
psz; } );
Cheers!
SG
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Juan Antonio Zaratiegui Vallecillo <zara@coit.es>
Date: Thu, 22 Jan 2009 20:53:35 CST Raw View
[ Please include substantive reasons for any further responses -- mod. ]
Joaqu n M L pez Mu oz escribi :
> The current C++0x standard draft requires that two tuples t and u have
> the same length in order to be comparable with operator <. As I see
> it, it would be a consistent and natural extension to allow t and u to
> be compared in the following way when their lengths differ:
>
> t < u iff t' < u', where t' is the tuple holding the m first
> elements of t, with m=min(length(t),length(u)), and similarly
> with u'.
>
> This is entirely analogous to the way strings are compared when they
> do not have the same length.
>
> Such an extension would allow us to do things like, for instance, the
> following:
>
> // table of (family name,given name) entries
> std::vector<std::tuple<std::string,std::string> > names;
> ...
> std::sort(names);
>
> // find all Smiths
> std::equal_range(
> names.begin(),names.end(),
> std::make_tuple("Smith"));
>
> Anyone else finds this extension useful?
>
You have my vote!
Zara
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Andrew Koenig" <ark@acm.org>
Date: Thu, 22 Jan 2009 20:58:31 CST Raw View
"Joaqu n M L pez Mu oz" <joaquin@tid.es> wrote in message
news:337f5856-63de-4f36-9378-6085911014d2@g39g2000pri.googlegroups.com...
> The current C++0x standard draft requires that two tuples t and u have
> the same length in order to be comparable with operator <. As I see
> it, it would be a consistent and natural extension to allow t and u to
> be compared in the following way when their lengths differ:
> t < u iff t' < u', where t' is the tuple holding the m first
> elements of t, with m=min(length(t),length(u)), and similarly
> with u'.
> This is entirely analogous to the way strings are compared when they
> do not have the same length.
I don't think so.
Consider t = "ab" and u = "abc". By your definition, m=2; so t'=t and
u'='"ab". Hence t'<u' is false, but string("ab") < string("abc") is true.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]