473,320 Members | 2,088 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,320 software developers and data experts.

operator< for std::bitset

Is there any good reason operator< is not defined for std::bitset? It
seems to me that:

1 - it would be useful.
2 - it is easy to implement inside the class template.
3 - it is impossible to implement efficiently (for bitsets too large
for to_ulong) outside of the class.

For now I will use boost::dynamic_bitset, which does implement it.

Guy
Nov 6 '08 #1
3 4694
Gu**********@gmail.com wrote:
Is there any good reason operator< is not defined for std::bitset?
I would venture a guess that a bitset was created as a collection of
*independent* bits none of which is more important than the other, and
therefore the type does not have the semantics of comparison with 'less
than' operators, only for equality.
It
seems to me that:

1 - it would be useful.
That's exactly on what the creators of the library do not agree with
you, I am guessing. You seem to attach some extra meaning to those
bits, the meaning the creators did not want to give the elements of the
collection.
2 - it is easy to implement inside the class template.
3 - it is impossible to implement efficiently (for bitsets too large
for to_ulong) outside of the class.

For now I will use boost::dynamic_bitset, which does implement it.
That's what the third-party libraries are for, extending the standard
library, providing elements that do not exist in it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 6 '08 #2
Gu**********@gmail.com escribió:
Is there any good reason operator< is not defined for std::bitset? It
seems to me that:

1 - it would be useful.
2 - it is easy to implement inside the class template.
3 - it is impossible to implement efficiently (for bitsets too large
for to_ulong) outside of the class.

For now I will use boost::dynamic_bitset, which does implement it.
template <size_t Bitsbool operator<
(const std::bitset<Bits>& valueA,const std::bitset<Bits>& valueB)
{
return valueA.to_ulong()<valueB.to_ulong();
}

Of course, this does not work inside namespace std; but should work
right on most cases.

Best regards,

Zara
Nov 6 '08 #3
Victor Bazarov wrote:
Gu**********@gmail.com wrote:
>Is there any good reason operator< is not defined for std::bitset?

I would venture a guess that a bitset was created as a collection of
*independent* bits none of which is more important than the other, and
therefore the type does not have the semantics of comparison with 'less
than' operators, only for equality.
It
seems to me that:

1 - it would be useful.

That's exactly on what the creators of the library do not agree with
you, I am guessing. You seem to attach some extra meaning to those
bits, the meaning the creators did not want to give the elements of the
collection.
Usefulness lies (as the disagreement shows) in the eye of the beholder. I
agree with the OP that the standard library didn't make a wise choice here:
there is no rationale _why_ bitset should only be used for cases where the
bits have no meanings. Put differently, which class should be used in cases
where the bits do have an additional meaning?

>2 - it is easy to implement inside the class template.
3 - it is impossible to implement efficiently (for bitsets too large
for to_ulong) outside of the class.
The efficiency concern of the OP (item 3) is perfectly valid. For that
reason alone, the standard library should at least specialize std::less<>
for bitset so that bitset object can be used in sets and maps with ease and
without performance penalty.
Best

Kai-Uwe Bux
Nov 6 '08 #4

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

Similar topics

5
by: SpOiLeR | last post by:
Hi. q1: Is std::bitset<N> part of standard or it's compiler extension? q2: Is std::bitset::to_string() part of standard? q3: My documentation say this about std::bitset::to_string(): ...
10
by: olson_ord | last post by:
Hi, I am not exactly new to C++, but I have never done operator overloading before. I have some old code that tries to implement a Shift Register - but I cannot seem to get it to work. Here's a...
18
by: ma740988 | last post by:
Trying to get more acclimated with the use of function objects. As part of my test, consider: # include <vector> # include <iostream> # include <algorithm> #include <stdexcept> #include...
10
by: woessner | last post by:
I'm using a C++ compiler on a DSP that doesn't provide the bitset class. Can anyone suggest a replacement for it? I've written a lot of code that uses the bitset class (before I knew about the......
10
by: Thierry Lam | last post by:
What does the following macro mean, especially the << sign: #define hello(x) (( int64 ) floor( (double) x ) << 32) Thanks Thierry
2
by: arnuld | last post by:
i am confused on some aspects of bitset class: /* C++ Primer 4/e * chapter 3 * * exercise 3.23 * */ #include <iostream>
5
by: arnuld | last post by:
i have solved the problem. any comments on it: /* C++ Primer 4/e * chapter 3 * * exercise 3.24 * STATEMENT: * consider the sequence 1,2,3,5,13,21. Initialize a "bitset<32>" object that...
5
by: swcamry | last post by:
class bitset::reference { friend class bitset; reference(); // no public constructor public: ~reference(); operator bool () const; //...
5
by: Sean Farrow | last post by:
Hi: I have an iterator defined as follows: std::map<std::bitset<6>, int>::iterator DotsIterator, SignsIterator; I get errors that std::bitset does not declare the < operator. Does this mean I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.