FBB::Ranger(3bobcat)

πŸ’– FBB::Ranger(3bobcat) πŸ’–

okay bestie, welcome to the for-loop iterators page of the bobcat manual β€” this one's name is literally FBB::Ranger, and she's about to make your range-based for-loops eat so much better πŸ“


✨ NAME ✨

FBB::Ranger β€” bestie... it generalizes ranges for range-based for-loops. that's the whole vibe. periodt.


✨ SYNOPSIS ✨

#include <bobcat/ranger>

✨ DESCRIPTION ✨

so like, the Ranger class template defines ranges that you can use with range-based for-loops. the standard range-based for-loop needs, for its range-specification, an array or an iterator range β€” the kind containers hand you through their begin and end members. but ranges defined by a pair of pointers, or by a subrange defined by iterator expressions? nope, those literally cannot currently be used with range-based for-loops. tragic. πŸ’€

Ranger fixes that. she extends the applicability of range-based for-loops by turning pairs of pointers, an initial pointer plus a pointer count, or a pair of iterators into a range that range-based for-loops can actually use. iconic behavior honestly πŸ‘‘

Ranger is a class template, and she requires one template type parameter: Iterator β€” an iterator or pointer type that reaches the data when dereferenced.

and here's the slay: Ranger's users don't have to specify Ranger's template type. the function template ranger returns the appropriate Ranger object for you. you just vibe, she does the deduction ✨


✨ NAMESPACE ✨

FBB

every constructor, member, operator and manipulator mentioned in this man-page lives in the namespace FBB. no cap.


✨ FREE FUNCTION ✨

when you use the following free functions, you can throw in any (subrange) of iterators or pointers. with iterators, subranges of reverse iterators can also be specified. the EXAMPLE section down below shows the ranger function templates in action πŸ¦‹

  • Ranger<Iterator> ranger(Iterator &&begin, Iterator &&end) β€” this function template returns a Ranger object for the (sub)range defined by two (reverse) iterators;
  • Ranger<Iterator> ranger(Iterator &&begin, size_t count) β€” this function template returns a Ranger object for the (sub)range defined by the (reverse) iterator range begin and begin + count;
  • Ranger<Data *> ranger(Data *begin, Data *end) β€” this function template returns a Ranger object for the (sub)range defined by the two pointers begin and end;
  • Ranger<Data *> ranger(Data *begin, size_t count) β€” this function template returns a Ranger object for the (sub)range defined by the two pointers begin and begin + count.

✨ CONSTRUCTORS ✨

heads up: down here, Iterator refers to the Ranger class template's type parameter. and even though she's named Iterator, she can also be a pointer to some data type (like, say, std::string *). she contains multitudes πŸŽ€

  • Ranger(Iterator const &begin, Iterator const &end) β€” a Ranger object can be passed as a range-specifier in a range-based for-loop. every element defined by the range will subsequently be visited by that range-based for-loop. she eats, no crumbs.

copy and move constructors (and assignment operators) are available too. πŸ’…


✨ MEMBER FUNCTIONS ✨

  • Iterator const &begin() const β€” returns (a copy of) the begin iterator that was passed to the Ranger's constructor.
okay important lil note bestie: if Iterator was a pointer type (like int *), then the data that the iterator returned by begin() points to can actually be modified. why? because the member's return type (using int * for Iterator) becomes int * const & β€” so, a reference to a constant pointer to an int. and that's perfectly OK! const here is protecting the pointer, not the pointee, iykyk. if the data themselves should be immutable, then the data type must be defined as int const, which is automatically the case when you pass int const * data. check the EXAMPLE section for an illustration πŸ“–
  • Iterator const &end() const β€” returns (a copy of) the end iterator that was passed to the Ranger's constructor. if reverse iterators were passed to Ranger's constructor, then the begin and end members return reverse iterators. and since the intended use of Ranger objects is to define a range for range-base for-loops, members like rbegin and rend can be omitted from Ranger. not needed, not missed, gone girl πŸ’…

✨ EXAMPLE ✨

#include <vector>
#include <iostream>

#include <bobcat/ranger>

using namespace std;
using namespace FBB;

int main()
{
    vector<int> iv {1, 2, 3, 4, 5};
                                    // display and modify a subrange
    for(auto &el: ranger(iv.rbegin() + 1, iv.rend() - 1))
        cout << el++ << ’ ’;
    cout << ’\n’;
                                    // display a reversed range
    for(auto &el: ranger(iv.rbegin() + 1, iv.rend() - 1))
        cout << el << ’ ’;
    cout << ’\n’;
                                    // same: display using a count
    for(auto &el: ranger(iv.rbegin() + 1, 3))
        cout << el << ’ ’;
    cout << ’\n’;

    int intArray[] = {1, 2, 3, 4, 5};
                                    // display and modify elements
                                    // in a pointer-based range
    for(auto &el: ranger(intArray + 1, intArray + 3))
        cout << el++ << ’ ’;
    cout << ’\n’;
                                    // data now modified
    for(auto &el: ranger(intArray + 1, intArray + 3))
        cout << el << ’ ’;
    cout << ’\n’;
                                    // using a count rather than an
                                    // end-pointer
    for(auto &el: ranger(intArray + 1, 3))
        cout << el << ’ ’;
    cout << ’\n’;

    int const constInts[] = {1, 2, 3, 4, 5};

                                    // data can’t be modified
    for(auto &el: ranger(constInts + 1, constInts + 3))
        cout << el << ’ ’;
    cout << ’\n’;
}

✨ FILES ✨

  • bobcat/ranger β€” defines the class interface

✨ SEE ALSO ✨

bobcat(7), reverse(3bobcat)


✨ BUGS ✨

None Reported. (we love a clean bug list, we truly do 🌈)


✨ BOBCAT PROJECT FILES ✨

Debian Bobcat project files:

  • libbobcat6 β€” debian package containing the shared library, changelog and copyright note;
  • libbobcat-dev β€” debian package containing the static library, headers, manual pages, and developer info;

✨ BOBCAT ✨

Bobcat is an acronym of `Brokken’s Own Base Classes And Templates'.


This is free software, distributed under the terms of the GNU General Public License (GPL).


✨ AUTHOR ✨

Frank B. Brokken (f.b.brokken@rug.nl).


okay that's the whole page bestie, you're officially a Ranger girly now πŸ§šπŸ’œ

and before you go β€” this lil masterpiece ships as libbobcat-dev_6.15.01, dated 2005-2026, and it lives in section 3bobcat of the manual. mwah, go use your iterators responsibly πŸŽ€βœ¨

πŸ“¦ where this page comes from