FBB::PtrIter(3) Iterator returning pointer when dereferenced


#include <bobcat/ptriter>


The PtrIter class template implements an input iterator whose operator* returns the address of the element the iterator refers to. Consider a std::unordered_map<std::string, DataType>. Its begin member returns an iterator whose operator* returns a std::pair<std::string, DataType> (const) &. This is usually what you want, but now assume we want to display the map's contents, sorted by its keys. Sorting can simply be performed by defining a support vector containing pointers to the elements in the map, and then sorting the strings the pointers point at.

PtrIter is a tool that can be used to construct such a support vector, as shown in the EXAMPLE section.

PtrIter is a class template requiring one template type parameter: Iterator, the iterator's type (e.g., vector<string>::iterator)

PtrIter's users don't have to specify PtrIter's template type. The function template ptrIter, when provided with an iterator returns the matching PtrIter object.


All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.


std::iterator<std::input_iterator_tag, ...>


  • PtrIter<Iterator> ptrIter(Iterator const &iter):
    this function template returns a PtrIter object for the function's Iterator argument. This function template simplyfies the construction of a PtrIter as no template parameters need to be specified (see also the EXAMPLE section)


  • PtrIter(Iterator const &iter):
    The iter parameter must be initialized with an existing input iterator, offering operator*, operator++, operator== and operator!=. As PtrIter is a class template, its template type parameters must be specified when defining a PtrIter object. E.g.,
        PtrIter<set<string>::iterator> PtrIter(mySet.begin());
    The copy and move constructors are available.


  • PtrType operator*() const:
    the address of the entity the iterator refers to is returned;
  • PtrIter &operator++():
    the iterator is (pre)incremented to the next position;
  • bool operator==(PtrIter const &other) const:
    true is returned if the two iterators are equal;
  • bool operator!=(PtrIter const &other) const:
    true is returned if the two iterators are unequal; The copy and move assignment operators are available.


PtrIter defines the following type:
  • typedef decltype(&*Iterator()) PtrType:


All members of std::iterator<std:::input_iterator_tag, ...> are available, as FBB::PtrIter inherits from this class.


#include <algorithm>
#include <unordered_map>
#include <vector>
#include <cstring>
#include <iostream>
#include <bobcat/ptriter>
using namespace std;
using namespace FBB;
int main()
    cout << "Enter lines, the first word will be the map's key; "
                                            "^D when done.\n";
    string key;
    string line;                    
    unordered_map<string, string> map;
    while (cin >> key && getline(cin, line))    // fill the map
        map[key] = line;
    cout << '\n';
                                        // initialize a support
    vector<decltype(&*map.begin())>     // vector, using ptrIter
        support(ptrIter(map.begin()), ptrIter(map.end()));
                                        // sort 'support'
    typedef unordered_map<string, string>::value_type VT;
    sort(support.begin(), support.end(), 
        [&](VT const *p1, VT const *p2)
            return strcasecmp(p1->first.c_str(), p2->first.c_str()) < 0;
    for(auto &element: support)         // display sorted by key
        cout << element->first << ' ' << element->second << '\n';


bobcat/ptriter - defines the class interface


None Reported.


  • bobcat_4.02.00-x.dsc: detached signature;
  • bobcat_4.02.00-x.tar.gz: source archive;
  • bobcat_4.02.00-x_i386.changes: change log;
  • libbobcat1_4.02.00-x_*.deb: debian package holding the libraries;
  • libbobcat1-dev_4.02.00-x_*.deb: debian package holding the libraries, headers and manual pages;
  • http://sourceforge.net/projects/bobcat: public archive location;


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).


Frank B. Brokken ([email protected]).