473,809 Members | 2,797 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

implementing const_iterator and iterator

Hi,

For the learning experience, I am building a replacement for std::map.
I built a templated red-black tree, etc., and all the basic stuff is
working well. I implemented basic iterators and operator++, etc., and
begin(), end(), and it works as a drop in for std::map if you stick to
the methods I have implemented.

Anyway, I decided to go and add const_iterator support, thinking that
it would be simple, but I ran into a confusing error and hoped some of
the experts here could help. Please read the full post since what I am
showing here is my working end result, but I have questions about its
implementation.

Basically, I have a tree_iterator that takes a pointer to a node class.
It is something like this:

template<class T, class R>
class tree_iterator_b ase
{
public:
tree_iterator_b ase(Node<T> *t)
: iter_(t)
{
}

operator tree_iterator_b ase<T, const R>()
{
return tree_iterator_b ase<T, const R>(this->iter_);
}

.......
}
The Node class has standard tree stuff, left, right, etc. It is also
templated and is instantiated with a std::pair type class for working
as a map replacement.

Here are my typedefs for my iterators and basic other types:

typedef pair<Key, Value> pair_type;
typedef Node<pair_type> node_type;
typedef tree_iterator_b ase<pair_type, pair_type> iterator;
typedef tree_iterator_b ase<pair_type, const pair_type> const_iterator;

At first, my iterator typedefs only had one template parameter. it was
"pair_type" for the normal iterator and "const pair_type" for the
const_iterator. More on this later.

Now, I had a find method:

typename btree::iterator find(const Key& key)
{
node_type *n = find...(key); // find key
.....
return iterator(n);
}
all worked great with regular iterators, but then I tried:

mymap::const_it erator x = tree.find(...);

Then I get compile error saying it can not convert between
iterator<pair> and iterator<const pair>, since find returns
iterator<pair> and const_iterator is defined as iterator<const pair>.
This makes sense and I understand that, so I added the conversion
operator to my iterator class which can convert an iterator<pair> to a
iterator<const pair>. (I also have const version of find that does
return const_iterator but that doesn't come into play here since it is
a non-const map instantiation where a const_iterator is wanted).

I added the second template parameter to the iterator class because I
though I would use one as the return type and the other as the data
type, and the iterator class would use the second parameter to specify
the return type (i.e. const pair_type operator*() vs pair_type
operator*()). Now that I think of it, I am not sure I need the two
parameters actually now that the conversion operator is there.

It all seems to be working correctly now, but I felt a bit funny with
this solution. I began looking more that the gnu libstdc++
implementation and I could not find any conversion operators on their
iterators (bits/stl_tree.h) so I am curious how they manage it. I
don't see any major differences in their iterator typedefs.

Thanks for any tips on how this could be improved. I guess I am
probably missing something simple I can't seem to figure out.

Jun 21 '06 #1
1 6454
> Thanks for any tips on how this could be improved. I guess I am
probably missing something simple I can't seem to figure out.


Nevermind... I must have had a brain disorder. A second constructor
taking const T for the iterator provides implicit conversion.

Thanks

Jun 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
16897
by: john smith | last post by:
Hi, I'm a little confused as to why the following code generates and error when compiling: #include <iostream> #include <vector> #include <iterator> using namespace std; void f(vector<int>::const_iterator& b, vector<int>::const_iterator& e)
3
2792
by: CoolPint | last post by:
If I were to design my own container and its iterators, I understand that I need to provide both "iterator" and "const_iterator" so that begin() and end() return appropriate ones depending on the "constantness" of the container object. I also note that is it done through overloading begin() and end() like below: iterator begin(); const_iterator begin() const; So it seems to me that I need to have two separate iterator classes
2
2063
by: PengYu.UT | last post by:
I'm wonder whether 1. stl directly defined the 6 comparison operators(== != < > <= >=) directly for iterator and const_iterator 2. or it only define == and < and using std::rel_ops to get the other 4 operators. I made the following example for case 1. But I can not make it work. Could you please help me to make it work. Thanks,
5
4815
by: John Harrison | last post by:
I there a reliable and generic method to convert a const_iterator to an iterator (i.e. something like const_cast). I ask because I'm writing some methods which take and return iterators. A const version of the method takes and returns const iterators and the non-const version takes and returns non-const iterators. I'd like to avoid code duplication like this class X {
2
4128
by: ul | last post by:
Hello, Just wonder what is faster for std::vector, const_iterator od iterator? Or is this stl-realisation-dependent? Target compilator gcc platform is Linux. Thanks
4
2724
by: kotau | last post by:
Hi, I'm having trouble with something that would appear to have a simple solution. Here's a version of the code I'm working with: const Item* p 0; name::const_iterator i;
10
2441
by: Yahooooooooo | last post by:
Hi, learning c++, can some one check below program. its giving compiler error. #include <iostream> #include <list> using namespace std; typedef list<intListInt;
2
1967
by: subramanian100in | last post by:
Consider the code fragment: vector<intcontainer; container.push_back(0); container.push_back(1); container.push_back(2); Now I want to iterate through the 'container'. For this, instead of writing,
2
2799
by: Markus Dehmann | last post by:
I want to derive from std::set, like shown below. But when I try to declare an iterator over the contained elements I get an error, see the twp uncommented lines: #include <set> template<class T> class MySet : public std::set<T>{ public: MySet() : std::set<T>() {} void foo(){
0
9602
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10639
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5688
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.