473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How can i return an iterator from a fucntion?


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 FindAppointment(Duration duration)
which return stl style result
but i dont want to return an iterator of type
map<time_t,Appointment>::Iterator
i want to return an iterator to Appointment.

Is it possible?

Note that i dont want to use exceptions

Thanks in advance.

May 22 '06 #1
6 3032
TOMERDR wrote:
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 FindAppointment(Duration duration)
which return stl style result
but i dont want to return an iterator of type
map<time_t,Appointment>::Iterator
i want to return an iterator to Appointment.

Is it possible?

Note that i dont want to use exceptions

Thanks in advance.


Iterators allow you to traverse *containers*. Is Appointment a
container? If not, do you just want to return the value that your
look-up function (note the spelling) finds? Presumably your function
has some way of translating a Duration (the parameter type you pass in)
to a time_t (the key type for your map) and then calls std::map::find()
with the key to retrieve the value associated with it.

If that's a correct guess at your algorithm, then the iterator your
received from std::map::find() (assuming the find was successful)
refers to a std::pair<time_t,Appointment>. You can then return the
iterator itself, a copy of the Appointment that the iterator refers to,
or a pointer to the Appointment that the iterator refers to. It just
depends on what interface you need for the calling functions.

Re exceptions: std::map::find() doesn't throw an exception by itself,
but adding things to a map, for instance, can do so whether you like it
or not.

Cheers! --M

May 22 '06 #2
TOMERDR wrote:
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 FindAppointment(Duration duration)
which return stl style result
What's "stl style result"?
but i dont want to return an iterator of type
map<time_t,Appointment>::Iterator
Why not?
i want to return an iterator to Appointment.
What's "an iterator to Appointment"?
Is it possible?
Everything is possible, once the requirements are known.
Note that i dont want to use exceptions


How is *that* relevant? I strongly doubt anybody would suggest throwing
an exception just to return a value (although it's actually possible).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 22 '06 #3
TOMERDR wrote:
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 FindAppointment(Duration duration)
which return stl style result
"stl style result" means nothing.
but i dont want to return an iterator of type
map<time_t,Appointment>::Iterator
Why not?
i want to return an iterator to Appointment.
An iterator is a value that represents an element in a container. An
Appointment is not an element in a container, it is the value in a
key-value pair element.
Is it possible?
You could return a reference to it instead:

Appointment& FindAppointment(Duration d)
{
map<time_t,Appointment>:: iterator itor = ...;
if (itor != m_Appointments.end())
return itor->second;

throw appointment_not_found();
}
Note that i dont want to use exceptions


Return a pointer instead.
Jonathan

May 22 '06 #4

ok suppose i will use what you suggest:

Appointment& FindAppointment(Duration d)
{
map<time_t,Appointment>:: iterator itor = ...;
if (itor != m_Appointments.end())
return itor->second;
throw appointment_not_found();

}

Is it safe to call it from a loop?

while(continue){
try {
Appointment app=FindAppointment(GetDuration())
AddApointmetToContainer(app)
}
catch(appointment_not_found e)
{
//do nothing
}
}

May 22 '06 #5
TOMERDR wrote:
Is it safe to call it from a loop?
Kind of an odd question. Why wouldn't it be?
while(continue){
Syntax error. "continue" is a C++ keyword that cannot be used in this
context. The parentheses here must contain a boolean expression.
try {
Appointment app=FindAppointment(GetDuration())
AddApointmetToContainer(app)
}
catch(appointment_not_found e)
Throw by value, catch by reference.
{
//do nothing
}


Swallowing exceptions is rarely a reflection of sound design. If the
exception is "designed to be swallowed," you're using exceptions for
flow control, which is bad. Otherwise, swallowing an exception is
basically pretending to know how to handle a condition you don't really
know how to handle, and denying anyone who *does* know how to handle it
the opportunity to do so.

Anyway, I wouldn't want to use exceptions here. Have you heard of the
Null Object Pattern? Alternatively, just return a pointer instead, and
return NULL rather than throwing an exception. Or just return the
silly iterator.

Luke

May 22 '06 #6
TOMERDR wrote:
ok suppose i will use what you suggest:
Please learn to quote correctly on Usenet. See
http://en.wikipedia.org/wiki/Top-post#Inline_replies for more
informations and http://cfaj.freeshell.org/google/ if you are using
Google Groups.
Appointment& FindAppointment(Duration d)
You might want to pass 'd' by const reference if it is not a [typedef
for a] built-in.
{
map<time_t,Appointment>:: iterator itor = ...;
You might want to typedef the map, it will make such usage much mroe
easy.
if (itor != m_Appointments.end())
return itor->second;
throw appointment_not_found();
}

Is it safe to call it from a loop?
Of course, why not?
while(continue){
'continue' is a keyword.
try {
Appointment app=FindAppointment(GetDuration())
FindAppointment returns a reference, don't copy objects when it is not
necessary.

Appointment& app=FindAppointment(GetDuration());
AddApointmetToContainer(app)
}
catch(appointment_not_found e)
Always catch by reference:

catch(appointment_not_found& e)
{
//do nothing
Err.. are you sure? In this case, just return a pointer instead of a
reference and return 0 if the value was not found. Although it may be
normal in your design not to find some appointments, it certainly looks
weird.
}
}


Last thing. Please remember that Usenet is a "public place". What you
write is read by many people. Try to respect netiquette and try to post
valid code (at least, end your statements with semicolons!)
Jonathan

May 22 '06 #7

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

Similar topics

8
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
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
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
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?...
3
by: Felix Kater | last post by:
Hi, normally I use a local variable assigned to different return values inside the function and return it like this: int f(){ int my_err_code; my_err_code=1;
3
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...
2
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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,...

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.