Specialization of a private class template with maps

I am a new C ++ programmer, I learned Java and ANSI C some time ago and decided to give it a chance.

Well, I love C ++, but I didn't like how iterators work:

In java, you can make the entire container private and implement the getter function for iterator, and the iterator has a hasNext() method that returns a boolean depending on whether it reaches the end of the container.

The only way I found something similar in C ++ is to write 2 getters, iteratorBegin() and iteratorEnd() , which return the internator corresponding to the first and last positions, increasing the iterator returned by iteratorBegin() , and comparing it with iteratorEnd() , allowed me to iterate over the container until reaching the end position

But I want to use only one getter method, and I thought: “Let me make my own iterator class”

So far, it’s so good that I have successfully dealt with sets and lists, but I can’t do it with maps, here is the code that bothers me: (the class is defined in a separate .h, this is called customIterator.cpp)

 template<typename T, typename D> const D& custIterator<T,D>::next() { const D& obj = (*it); if(hasNext()) { it++; } return obj; } //the above works fine template<typename T, typename D> const D& custIterator<map<T,D>,D>::next() //error in this line { D& obj = (*it).second; if(hasNext()) { it++; } return obj; } 

when compiling a specialized method, he says: error: "the map was not declared in this area although I added #include <map> on top of the file

I am using gcc version 4.4.5 (Debian 4.4.5-8) with code blocks

Please, I need some help.

Thanks for attention!

+1
c ++ iterator templates map partial-specialization
source share
1 answer

All standard containers are in the std namespace, so you should qualify it

 std::map<T,D> 

In general, I would recommend not trying to port Java idioms to C ++, since each language has its own idioms, and mixing them up confuses other programmers (just as I think #define BEGIN { not the best idea ever or).

What you are trying to do is known as ranges, and it is believed that some of them are a better solution than C ++ iterators, but until it turns into a language that, in my opinion, is harmful for every developer to invent your own ranges.

Further reading or tl; dr slides (there is a video with which I cannot find at the moment).

+5
source share

All Articles