简体   繁体   中英

Why doesn't std::bitset come with iterators?

It appears that std::bitset does not come with STL iterators.
Therefore, I cannot do the following:

std::bitset<8> bs;
for (auto it: bs) {
    std::cout << "this can not be done out of the box\n";
}

Instead I must:

std::bitset<8> bs;
for (std::size_t i = 0; i < bs.size(); ++i) {
    std::cout << bs[i] << '\n';
}

Without iterators, I also can't use bitsets with any of the STL algorithms.
Why did the committee decide to exclude iterators from bitset?

I don't think there was ever an actual decision to exclude iterators from bitset.

Rather, bitset is one of the classes that predates the proposal to add the original Standard Template Library to the C++ standard. When it was designed, essentially none of the standard library included iterators.

Then, Stepanov's library was proposed for addition, and quite a bit of it was accepted. In response to that, additions were made to some existing classes (eg, std::string ) to allow them to be used like the new container classes.

This was all happening quite late in the standards process though--in fact, they already bent the rules in a few places to add what they did. Among other things, just about the same time as the containers/iterators/algorithms were added to the library, the committee voted to consider the standard "feature complete", so from that point onward they'd only work on fixing bugs and such, not adding new features.

As such, even if a proposal had been written to add an iterator interface to bitset , about the only way the committee could have accepted it would have been to treat this as a bug being fixed rather than a new feature being added. If there'd been a really solid proposal, I suppose they could have done that, but I don't think there was such a proposal, and it would have been stretching the point quite a bit, so even a really good proposal might easily have been rejected.

Since then, there was one proposal, LEWG 1112 , that would have added an iterator interface to std::bitset . This was proposed for C++11, and was proposed specifically to support the range-based for loop that was also being added in C++11. It suffered a rather ignominious fate: it was originally accepted, and wording was drafted. Then it looked like the proposal for adding Concepts to the language would be accepted, so this wording was rewritten to use the shiny, wonderful new concepts. Sometime later, concepts were removed from the language, and rather than rewording the proposal so it no longer depended on concepts, they tentatively marked it as "NAD Future", which means they treated it as not being a defect, and deferred any further work until some (indefinite) time in the future (and as far as I can see, haven't revisited it since).

You can create your own iterator, or just use mine

Usage is as follows:

#include <bitset>
#include "bitset_iterator.h"

std::bitset<32> indices{ 0b10101010101010101010101010101010 };

for (const auto& index : indices) {
  std::cout << index << ", ";
}
// Prints "1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM