C++ standard/de facto STL algorithm wrappers
Asked Answered
C

4

8

Are there any standard/de facto standard (boost) wrappers around standard algorithms which work with containers defining begin and end. Let me show you what I mean with the code:

// instead of specifying begin and end
std::copy(vector.begin(), vector.end(), output);
// write as
xxx::copy(vector, output);

I know it can be written easily, but I am looking specifically for something ubiquitous. Thanks.

Cairistiona answered 1/2, 2010 at 19:44 Comment(1)
An excellent question indeed. I've always found the idea of basing algorithms on iterators quite messy... std::copy(input.begin(), input.end(), std::back_inserter(output)); that really hurts my eyes... Unfortunately, whenever it comes down to actually modifying the structure of the underlying container (adding / removing) elements, then Boost.Range does not work :/Linsang
R
8

There is an extension to the Boost Range library called RangeEx which contains range wrappers for all stl algorithms, plus some new ones.

It has recently been accepted into Boost and so it's not yet in the current "official" release (1.41). Until this changes, you can download the latest version from the Boost Vault.

Don't know if this will ever become part of the C++ standard, but the fact that it's in Boost means that it will be the de facto standard.

Riffraff answered 1/2, 2010 at 20:15 Comment(1)
fyi, RangeEx has been integrated into Boost Range as of 1.43.0Zamindar
P
2

The next standard will (hopefully!) amend this. In the meantime, take a look at Boost.Range and its various uses although I’m not aware of an interface to the standard algorithms.

Precocious answered 1/2, 2010 at 19:49 Comment(4)
TR1 does not include a range facility. The Wikipedia page for C++0x lists a few items about range semantics, but they are referring to the range-based for loop which is new in C++0x. AFAIK the standard algorithms are not being modified in the new standard -- though I could be wrong....Numberless
@unknown: No, TR1 isn’t strictly a new standard, it’s just a bunch of recommendations. I was speaking of C++0x (although that’s indeed missing from the Wikipedia article – but it was planned. Not sure if it made it into the final draft).Precocious
It was part of Stroustrup's "concepts" proposal which isn't in C++0x. The library changes would have been great to have, but that had to be balanced against the complexity of concepts as a language feature.Derte
@Jason: hmm, pity. But concepts aren’t actually needed for this, since the range is just a holder for two iterators so I don’t see why this was removed along with concepts.Precocious
N
1

The only case where I've seen something like this are those algorithms based upon the boost::range library, but even these do not actually modify the standard algorithms like std::copy or std::remove_if -- the algorithm in question needs to be written to take advantage of such a range wrapper.

For an example, see the Boost String Algorithms library.

Numberless answered 1/2, 2010 at 19:48 Comment(0)
C
0

I will add my own finding: Adobe source libraries (rangex from boost supersedes algorithms part) ASL

Cairistiona answered 1/2, 2010 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.