I have a class TContainer that is an aggregate of several stl collections pointers to TItems class.
I need to create an Iterator to traverse the elements in all the
This is the simplest code I was able to produce (for custom iterators). Note that I'm only beginning to explore this area. This calls built-in upper_bound
function to perform binary search on an integer function, x^2
as an example.
#include
#include
using namespace std;
class Iter
{
public:
int x;
Iter() { x = -1; }
Iter(int a) { x = a; }
bool operator!=(Iter &i2) const { return x != i2.x; }
void operator++() { x++; }
void operator+=(int b) { x += b; }
int operator-(const Iter &i2) const { return x - i2.x; }
int operator*() const {
cout << "calculating for x " << x << endl;
return x*x;
}
typedef random_access_iterator_tag iterator_category;
typedef int value_type;
typedef int difference_type;
typedef int* pointer;
typedef int& reference;
};
main ()
{
ios::sync_with_stdio(false);
cout << upper_bound(Iter(0), Iter(100), 40).x << endl;
}
// :collapseFolds=1:folding=explicit:
And this is how the output looks like:
calculating for x 50
calculating for x 25
calculating for x 12
calculating for x 6
calculating for x 9
calculating for x 8
calculating for x 7
7