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 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();
}
};
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
"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
"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);
}
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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?...
|
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,...
| |
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...
|
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...
|
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",...
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
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...
| |