473,511 Members | 15,408 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return an iterator?

I have a class Foo that contains a map<string,string> internally. Now,
I want that users can iterate over the entries in the map. What's the
best way to do that?

I tried a method like
map<string,string>::const_iterator get_const_iterator(){
return my_map.begin();
}
but then users also need to know where the end() of the map is in
order to for-loop over the elements.

So, do I have to provide a begin() and end() method in my Foo class?
Or is there a better solution?

Thanks
Markus
Jul 22 '05 #1
6 1701
In article <c1**************************@posting.google.com >,
ma*******@gmx.de (Markus Dehmann) wrote:
I have a class Foo that contains a map<string,string> internally. Now,
I want that users can iterate over the entries in the map. What's the
best way to do that?

I tried a method like
map<string,string>::const_iterator get_const_iterator(){
return my_map.begin();
}
but then users also need to know where the end() of the map is in
order to for-loop over the elements.

So, do I have to provide a begin() and end() method in my Foo class?
Or is there a better solution?


Nope, that's it:

class Class {
map<string, string> my_map;
public:
typedef map<string, string>::const_iterator const_iterator;
const_iterator begin() const {
return my_map.begin();
}
const_iterator end() const {
return my_map.end();
}
};
Jul 22 '05 #2
On 10 Aug 2004 14:47:31 -0700, ma*******@gmx.de (Markus Dehmann)
wrote:
I have a class Foo that contains a map<string,string> internally. Now,
I want that users can iterate over the entries in the map. What's the
best way to do that?

I tried a method like
map<string,string>::const_iterator get_const_iterator(){
return my_map.begin();
}
(BTW this function should be const...you cannot modify the map through
a const_iterator, hence you should be able to call it on const Foo
objects.)
but then users also need to know where the end() of the map is in
order to for-loop over the elements.

So, do I have to provide a begin() and end() method in my Foo class?
Or is there a better solution?


Another (IMHO better) solution would be to provide const access to the
map itself, i.e. a member function such as:

const map<string,string> & the_map() const { return my_map; }
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #3
"Bob Hairgrove" <in*****@bigfoot.com> wrote in message
news:7m********************************@4ax.com...
[snip]
Another (IMHO better) solution would be to provide const access to the
map itself, i.e. a member function such as:

const map<string,string> & the_map() const { return my_map; }


The begin()/end() approach buys you a little flexibility if you ever decide
to drop std::map to use a different container. In theory, one could replace
the container and provide an iterator that provided the same interface as
std::map::const_iterator. That's not a big deal for small programs, so your
suggestion is probably just as good.

--
David Hilsee
Jul 22 '05 #4
"Markus Dehmann" <ma*******@gmx.de> wrote in message
I have a class Foo that contains a map<string,string> internally. Now,
I want that users can iterate over the entries in the map. What's the
best way to do that? So, do I have to provide a begin() and end() method in my Foo class?
Or is there a better solution?


Yes, that's one way as others pointed out. There are 2 other ways I've
used. Though neither way is superior to the others, and I've used all of
them in my own code:

(2) Write a nested class Foo::MapIterator that contains private members
d_begin and d_end pointing to the begin and end of the map. There will be a
functions Foo::MapIterator::valid() const or the like to tell if the
iterator is valid, Foo::MapIterator::operator++() etc.

Foo::MapIterator iter = foo.mapiterator();
for ( ; iter.valid(); ++iter) {
/* do stuff */
}

Unfortunately, in this method you cannot use standard algorithms and others
from boost which expect two iterator arguments. But if you don't need the
generality, then this method is fine.

(3) Provide a template function Foo::for_each_element_in_map.

template <class Oper>
void Foo::for_each_element_in_map(const Oper& oper) {
std::for_each(d_map.begin(), d_map.end(), oper);
}
Jul 22 '05 #5
On Tue, 10 Aug 2004 20:13:00 -0400, "David Hilsee"
<da*************@yahoo.com> wrote:
"Bob Hairgrove" <in*****@bigfoot.com> wrote in message
news:7m********************************@4ax.com.. .
[snip]
Another (IMHO better) solution would be to provide const access to the
map itself, i.e. a member function such as:

const map<string,string> & the_map() const { return my_map; }


The begin()/end() approach buys you a little flexibility if you ever decide
to drop std::map to use a different container. In theory, one could replace
the container and provide an iterator that provided the same interface as
std::map::const_iterator.


I'm not so sure ... map<> is a peculiar beast (behavior of its
operator[] compared to vector::operator[], for example). Clients will
probably want to know the exact type of container they are using,
especially if it's a map.

If you really want to replace the container, you could also provide a
typedef for it and merely change the typedef.

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #6
In message <06********************************@4ax.com>, Bob Hairgrove
<in*****@bigfoot.com> writes
On Tue, 10 Aug 2004 20:13:00 -0400, "David Hilsee"
<da*************@yahoo.com> wrote:
"Bob Hairgrove" <in*****@bigfoot.com> wrote in message
news:7m********************************@4ax.com. ..
[snip]
Another (IMHO better) solution would be to provide const access to the
map itself, i.e. a member function such as:

const map<string,string> & the_map() const { return my_map; }
The begin()/end() approach buys you a little flexibility if you ever decide
to drop std::map to use a different container. In theory, one could replace
the container and provide an iterator that provided the same interface as
std::map::const_iterator.


I'm not so sure ... map<> is a peculiar beast (behavior of its
operator[] compared to vector::operator[], for example). Clients will
probably want to know the exact type of container they are using,
especially if it's a map.


I'd have said it depends entirely on the interface (at the Concept
level) you _choose_ to present to the clients. You may be using a map
internally for your own nefarious purposes, but if you present the data
conceptually to them as a non-modifiable sequence accessible only by
const forward (or maybe bidirectional) iterators, they have no reason to
know or care about the underlying map.

If you really want to replace the container, you could also provide a
typedef for it and merely change the typedef.

--
Bob Hairgrove
No**********@Home.com


--
Richard Herring
Jul 22 '05 #7

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

Similar topics

8
1885
by: Huibuh | last post by:
I start hating RETURN!!! list<happy> //#include <list>// is searched via iterator (ptr) so long until the argument "number" of the class "happy" is equal now. The function "findhappy" works...
5
2079
by: Bob | last post by:
Hi, I have a std::vector, say myVec, of some user defined object, say myOb. In my code, I have a function that searches myVec for a particular myOb. The way I was doing this was searching...
3
2079
by: learning_C++ | last post by:
I hope to use template. in my code I hope to sum the vector<int> and vector<double>. But there are several errors: sumtemplate.cpp:6: error: ISO C++ forbids declaration of `sum' with no type...
11
3209
by: snnn | last post by:
On the book <Generic Programming and the STL>( Matthew . H . Austern ),this function is defined as iterator set::begin() const. However, why should a const object returns a non-const iterator?...
1
2965
by: Marcin Kaliciński | last post by:
Hi, map::erase(iterator) and map::erase(iterator, iterator). In STL shipped with MSVC return value of these functions is iterator. In gcc 3.3 it is void. Which one is conforming? cheers,...
3
7073
by: Allerdyce.John | last post by:
Hi, When I return an iterator, should I return an iterator, or should I return a reference to the iterator? for example: vector<A> aVector; Should it be: vector<A>::Iterator...
6
3037
by: TOMERDR | last post by:
Hi,i am new to stl and i have a question regarding iterators I have class day contains a map of appointments: map<time_t,Appointment> m_Appointments; I would like to write a function...
2
19853
by: Steve Richter | last post by:
very confused on how to implement the IEnumerable and IEnumerator interfaces on a generic type. I understand I should just use a foreach loop in the GetEnumerator method and use "yield return",...
6
3113
by: antani | last post by:
VolumeType::ISetType::iterator findFirstIteratorMarked(const VolumeType::ISetType::iterator & it,const VolumeType::ISetType::iterator & e_it) { VolumeType::ISetType::iterator _it=it; while...
3
1937
by: Jess | last post by:
Hello, The C++ reference says the return result of "copy" is an output iterator. I'm wondering how I can assign the returned iterator to some other iterator. I tried int main(){ string...
0
7138
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...
0
7353
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,...
1
7075
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...
0
5662
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,...
1
5063
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...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3212
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
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 ...
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.