473,387 Members | 3,801 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,387 software developers and data experts.

return value of function

Bob
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 myVec for the element that has
a member equal to a value that was passed into the function. For
example:

bool myFunc(const std::string& s)
{
std::vector<myOb>::iterator i = myVec.begin();

while(i != myVec.end()) {
if((*i).name == s) return true;
}
return false;
}

This hopefully returns true if the element is found, or false if not.
However, I don't really want to return true or false, I was wanting to
return either the offset from myVec.begin() (and maybe -1 if not
found), or a reference to the element itself.

How should I do this? Basically, I'm not sure how to return an offset
from begin(), and I didn't know what to return for the reference in
the case of 'element not found'. Or, should I really be returning the
iterator, and de-referencing that in my calling program?

Many thanks for your valued advice,
Bob.
Jul 22 '05 #1
5 2071
Bob wrote:

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 myVec for the element that has
a member equal to a value that was passed into the function. For
example:

bool myFunc(const std::string& s)
{
std::vector<myOb>::iterator i = myVec.begin();

while(i != myVec.end()) {
if((*i).name == s) return true;
}
return false;
}

This hopefully returns true if the element is found, or false if not.
However, I don't really want to return true or false, I was wanting to
return either the offset from myVec.begin() (and maybe -1 if not
found), or a reference to the element itself.

How should I do this? Basically, I'm not sure how to return an offset
from begin(), and I didn't know what to return for the reference in
the case of 'element not found'. Or, should I really be returning the
iterator, and de-referencing that in my calling program?

Many thanks for your valued advice,
Bob.


Without knowing the specifics of your problem, the first idea is to
return an iterator to the element if found, myVec.end() otherwise.

Don't write it as above. Look up the standard function template
std::find_if. All you need to do is to define a predicate function object
which stores a const std::string& s, accepts myOb and returns true
if s == myOb.name, something like

class MyPred {
const std::string& s_;
public:
MyPred(const std::string& s) : s_(s) {}
bool operator()(const myOb& a) const {
return s_ == a.name;
}
};

Denis
Jul 22 '05 #2
"Bob" <bo******@hotmail.com> wrote in message
news:77**************************@posting.google.c om...
However, I don't really want to return true or false, I was wanting to
return either the offset from myVec.begin() (and maybe -1 if not
found), or a reference to the element itself. [..] Or, should I really be returning the
iterator, and de-referencing that in my calling program?


Yes, seems like the best solution. And if no match was found, you return
myVec.end (). Tho, then you have to check if the returned iterator is valid
before dereferncing it.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 22 '05 #3

"Bob" <bo******@hotmail.com> wrote in message
news:77**************************@posting.google.c om...
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 myVec for the element that has
a member equal to a value that was passed into the function. For
example:

bool myFunc(const std::string& s)
{
std::vector<myOb>::iterator i = myVec.begin();

while(i != myVec.end()) {
if((*i).name == s) return true;
}
return false;
}

This hopefully returns true if the element is found, or false if not.
However, I don't really want to return true or false, I was wanting to
return either the offset from myVec.begin() (and maybe -1 if not
found), or a reference to the element itself.
You can't return a reference to the element in the case where the element
isn't found. You could throw an exception in this case however.

You could also return a pointer to the element, and a null pointer in the
not found case.

How should I do this? Basically, I'm not sure how to return an offset
from begin(), and I didn't know what to return for the reference in
the case of 'element not found'.
return i - myVec.begin();

Or, should I really be returning the iterator, and de-referencing that in my calling program?


Yes probably, although checking the return value against myVec.end() in the
calling program is tedious.

john
Jul 22 '05 #4
On Wed, 12 May 2004 08:18:00 +0200, Bob wrote:
[...]
However, I don't really want to return true or false, I was wanting to
return either the offset from myVec.begin() (and maybe -1 if not found),


Have a look at distance()

HTH, Darius.
Jul 22 '05 #5
Bob,

use the stl find() algorithm - this will return the iterator to the
element you want or end() to indicate t he object was not found,
why reinvent the wheel when you can rip one off somebody else car :).
std::vector<myOb>::iterator i find( myVec.begin(), myVec.end(), foo)

if ( i!= myVec.end())
{
// found.
myObj bar = *i;
EurekaImFound(bar);
}
else
{
// not found.
}
Do you really need the offset, the iterator would give you direct
access to the object anyway.

dave
"Bob" <bo******@hotmail.com> wrote in message
news:77**************************@posting.google.c om...
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 myVec for the element that has
a member equal to a value that was passed into the function. For
example:

bool myFunc(const std::string& s)
{
std::vector<myOb>::iterator i = myVec.begin();

while(i != myVec.end()) {
if((*i).name == s) return true;
}
return false;
}

This hopefully returns true if the element is found, or false if not.
However, I don't really want to return true or false, I was wanting to
return either the offset from myVec.begin() (and maybe -1 if not
found), or a reference to the element itself.

How should I do this? Basically, I'm not sure how to return an offset
from begin(), and I didn't know what to return for the reference in
the case of 'element not found'. Or, should I really be returning the
iterator, and de-referencing that in my calling program?

Many thanks for your valued advice,
Bob.

Jul 22 '05 #6

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

Similar topics

17
by: strout | last post by:
function F(e) { return function(){P(e)} } Can anybody tell me what the code is doing? If return another function all in a function I would do function F(e)
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
3
by: tshad | last post by:
I am trying to set up a class to handle my database accesses. I can't seem to figure out how to get the return value from my dataReader from these routines (most of which I got elsewhere). They...
5
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, I think the VB .NET compiler should at least issue a warning when a function does not return value. C# and C++ compilers treat this situation as an error and I believe this is the right...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
18
by: Ed Jay | last post by:
<disclaimer>js newbie</disclaimer> My page has a form comprised of several radio buttons. I want to poll the buttons to determine which button was selected and convert its value to a string. I...
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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
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...

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.