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<myO b>::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. 5 2086
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<myO b>::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()(cons t myOb& a) const {
return s_ == a.name;
}
};
Denis
"Bob" <bo******@hotma il.com> wrote in message
news:77******** *************** ***@posting.goo gle.com... 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)
"Bob" <bo******@hotma il.com> wrote in message
news:77******** *************** ***@posting.goo gle.com... 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<myO b>::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
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.
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<myO b>::iterator i find( myVec.begin(), myVec.end(), foo)
if ( i!= myVec.end())
{
// found.
myObj bar = *i;
EurekaImFound(b ar);
}
else
{
// not found.
}
Do you really need the offset, the iterator would give you direct
access to the object anyway.
dave
"Bob" <bo******@hotma il.com> wrote in message
news:77******** *************** ***@posting.goo gle.com... 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<myO b>::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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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)
|
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 of that software. That subject seems not to be addressed,
at least not directly, in the C FAQ where FAQ 5.2 seems most relevant.
References:
* C FAQ 5.2 Null pointers
(Including conditions where "casting" of
null pointer...
|
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 see how the return value comes to play here.
Ex
|
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 do work pretty
well, except for the change I made to get the return value.
For example, I have the following:
**********************************************************************
Public Overloads Function RunProcedure( _
ByVal storedProcName...
|
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 thing to do. And I wonder why VB .NET keeps
silence and makes such function return some default value instead. Isn't it
error-prone?
--
Dmitriy Lapshin
| |
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.
1. The Deserialization goes like:
#Region " Load "
|
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 then want to use the string on the same page.
My script is:
function checkRadio(field) {
for(var i=0; i < field.length; i++) {
|
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 exit(n)) statement provide both
function call and return features of the C programming language?
/* headers omitted */
int main (void)
|
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
The question may be silly or even meaning less but
please............
|
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 IsLvItemCheckedDelegate(ByVal ClientID As Integer)
As Boolean
Private Function IsLvItemChecked(ByVal ClientID As Integer) As Boolean
If lvServers.InvokeRequired = True Then
lvServers.Invoke(New IsLvItemCheckedDelegate(AddressOf IsLvItemChecked),...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |