How do I rewrite the following code so that I am taking advantage of the
STL? The purpose of the code is to determine whether a vector contains any
objects of MyClass that are "true" (i.e., MyClass::is_tru e() returns true).
bool found = false;
vector<MyClass> ::size_type i;
for (i=0; i < objects.size(); i++)
{
MyClass object = objects[i];
if (object.is_true ())
{
found = true;
break;
}
}
Any help is very appreciated. 10 1439
"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:41******** **************@ news.optusnet.c om.au... How do I rewrite the following code so that I am taking advantage of the STL? The purpose of the code is to determine whether a vector contains any objects of MyClass that are "true" (i.e., MyClass::is_tru e() returns true).
bool found = false; vector<MyClass> ::size_type i; for (i=0; i < objects.size(); i++) { MyClass object = objects[i]; if (object.is_true ()) { found = true; break; } }
Any help is very appreciated.
You're using vectors, so you're already taking advantage of the STL. Yes,
it's possible to rewrite this code in a way that uses STL functionality even
more, but you didn't indicate what exactly it is you want. Do you want to
use iterators, std::find_if(), something else?
Also, my spider sense is tingling on this one... this isn't homework by any
chance, is it?
--
Unforgiven
"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:41******** **************@ news.optusnet.c om.au... How do I rewrite the following code so that I am taking advantage of the STL? The purpose of the code is to determine whether a vector contains any objects of MyClass that are "true" (i.e., MyClass::is_tru e() returns
true). bool found = false; vector<MyClass> ::size_type i; for (i=0; i < objects.size(); i++) { MyClass object = objects[i]; if (object.is_true ()) { found = true; break; } }
Any help is very appreciated.
#include <algorithm>
#include <iostream>
#include <vector>
class MyClass
{
public:
MyClass(bool arg = false) : state(arg)
{
}
bool is_true() const
{
return state;
}
private:
bool state;
};
bool pred(const MyClass& mc)
{
return mc.is_true();
}
bool any_true(const std::vector<MyC lass>& v)
{
return std::find_if(v. begin(), v.end(), pred) != v.end();
}
int main()
{
std::vector<MyC lass> vec (10);
std::cout << any_true(vec) << '\n';
vec[5] = true;
std::cout << any_true(vec) << '\n';
return 0;
}
An alternative:
Replaced member function 'is_true()' with 'operator bool()',
uses 'std::find()' instead of 'std::find_if() '. (We no longer
need the predicate function 'pred()').
#include <algorithm>
#include <iostream>
#include <vector>
class MyClass
{
public:
MyClass(bool arg = false) : state(arg)
{
}
operator bool () const
{
return state;
}
private:
bool state;
};
bool any_true(const std::vector<MyC lass>& v)
{
return std::find(v.beg in(), v.end(), true) != v.end();
}
int main()
{
std::vector<MyC lass> vec (10);
std::cout << any_true(vec) << '\n';
vec[5] = true;
std::cout << any_true(vec) << '\n';
return 0;
}
HTH,
-Mike
"Jason Heyes" <ja********@opt usnet.com.au> wrote: How do I rewrite the following code so that I am taking advantage of the STL? The purpose of the code is to determine whether a vector contains any objects of MyClass that are "true" (i.e., MyClass::is_tru e() returns true).
bool found = false; vector<MyClass> ::size_type i; for (i=0; i < objects.size(); i++) { MyClass object = objects[i]; if (object.is_true ()) { found = true; break; } }
Any help is very appreciated.
If you want to work it out for yourself, I suggest you look up find_if
and mem_fun_ref. Otherwise...
bool found =
find_if( objects.begin() , objects.end(),
mem_fun_ref( &MyClass:is_tru e ) ) != objects.end();
"Unforgiven " <ja*******@hotm ail.com> wrote in message
news:2q******** ****@uni-berlin.de... You're using vectors, so you're already taking advantage of the STL. Yes, it's possible to rewrite this code in a way that uses STL functionality
even more, but you didn't indicate what exactly it is you want. Do you want to use iterators, std::find_if(), something else?
Also, my spider sense is tingling on this one... this isn't homework by
any chance, is it?
-- Unforgiven
No this isn't homework. The sort of STL I had in mind was std::find_if()
used together with mem_fun_ref(). Is this how its done? Would you encourage
writing this sort of code?
bool found = find_if(objects .begin(), objects.end(),
mem_fun_ref(MyC lass::is_true)) != objects.end();
Something I noticed was that mem_fun_ref_t<R ,T>::operator() takes a
non-const reference only. Why isn't there a mem_fun_ref_t taking a const
reference so that my code works when objects is const?
"Daniel T." <po********@eat hlink.net> wrote in message
news:po******** *************** *******@news02. east.earthlink. net... If you want to work it out for yourself, I suggest you look up find_if and mem_fun_ref. Otherwise...
bool found = find_if( objects.begin() , objects.end(), mem_fun_ref( &MyClass:is_tru e ) ) != objects.end();
What happens if objects is const? The operator() function in mem_fun_ref_t
only takes a non-const reference. Do I need to write my own template class
called mem_fun_const_r ef_t, say? Thanks.
"Jason Heyes" <ja********@opt usnet.com.au> wrote in message
news:41******** **************@ news.optusnet.c om.au... "Daniel T." <po********@eat hlink.net> wrote in message news:po******** *************** *******@news02. east.earthlink. net... If you want to work it out for yourself, I suggest you look up find_if and mem_fun_ref. Otherwise...
bool found = find_if( objects.begin() , objects.end(), mem_fun_ref( &MyClass:is_tru e ) ) != objects.end();
What happens if objects is const? The operator() function in mem_fun_ref_t only takes a non-const reference. Do I need to write my own template class called mem_fun_const_r ef_t, say? Thanks.
I also want something similar for the following code where the member
function of MyClass being called takes a single argument.
bool found = false;
vector<MyClass> ::size_type i;
for (i=0; i < objects.size(); i++)
{
MyClass object = objects[i];
if (object.is_equa l(32))
{
found = true;
break;
}
}
Would this be the STL code to write?
typedef mem_fun_ref1_t< bool, MyClass, int> func_t;
binder2nd<func_ t> pred = bind2nd(func_t( MyClass::is_equ al), 32);
bool found = find_if(objects .begin(), objects.end(), pred) !=
objects.end();
Thanks.
In article <41************ ***********@new s.optusnet.com. au>,
"Jason Heyes" <ja********@opt usnet.com.au> wrote: "Jason Heyes" <ja********@opt usnet.com.au> wrote in message news:41******** **************@ news.optusnet.c om.au... "Daniel T." <po********@eat hlink.net> wrote in message news:po******** *************** *******@news02. east.earthlink. net... If you want to work it out for yourself, I suggest you look up find_if and mem_fun_ref. Otherwise...
bool found = find_if( objects.begin() , objects.end(), mem_fun_ref( &MyClass:is_tru e ) ) != objects.end();
What happens if objects is const? The operator() function in mem_fun_ref_t only takes a non-const reference. Do I need to write my own template class called mem_fun_const_r ef_t, say? Thanks.
I also want something similar for the following code where the member function of MyClass being called takes a single argument.
bool found = false; vector<MyClass> ::size_type i; for (i=0; i < objects.size(); i++) { MyClass object = objects[i]; if (object.is_equa l(32)) { found = true; break; } }
Would this be the STL code to write?
typedef mem_fun_ref1_t< bool, MyClass, int> func_t; binder2nd<func_ t> pred = bind2nd(func_t( MyClass::is_equ al), 32); bool found = find_if(objects .begin(), objects.end(), pred) != objects.end();
I wouldn't bother with the seperate 'pred' object unless I was using it
in more than one place:
bool found =
find_if( objects.begin() , objects.end(),
bind2nd( mem_fun_ref( &MyClass::is_eq ual ), 32 ) ) != objects.end();
Or if you really want the pred:
binder2nd< const_mem_fun1_ ref_t< bool, MyClass, int> >
MyClassIsEquals 32 = bind2nd( mem_fun_ref( &MyClass::is_eq ual ), 32 );
bool found =
find_if( objects.begin() , objects.end(), MyClassIsEquals 32 ) !=
objects.end();
In article <41************ **********@news .optusnet.com.a u>,
"Jason Heyes" <ja********@opt usnet.com.au> wrote: "Daniel T." <po********@eat hlink.net> wrote in message news:po******** *************** *******@news02. east.earthlink. net... If you want to work it out for yourself, I suggest you look up find_if and mem_fun_ref. Otherwise...
bool found = find_if( objects.begin() , objects.end(), mem_fun_ref( &MyClass:is_tru e ) ) != objects.end();
What happens if objects is const?
Then a const_mem_fun_r ef_t is created instead of a mem_fun_ref_t, and
the code works.
"Daniel T." <po********@eat hlink.net> wrote in message
news:po******** *************** *******@news05. east.earthlink. net... In article <41************ **********@news .optusnet.com.a u>, "Jason Heyes" <ja********@opt usnet.com.au> wrote:
"Daniel T." <po********@eat hlink.net> wrote in message news:po******** *************** *******@news02. east.earthlink. net... If you want to work it out for yourself, I suggest you look up find_if and mem_fun_ref. Otherwise...
bool found = find_if( objects.begin() , objects.end(), mem_fun_ref( &MyClass:is_tru e ) ) != objects.end();
What happens if objects is const?
Then a const_mem_fun_r ef_t is created instead of a mem_fun_ref_t, and the code works.
Is there a const_mem_fun_r ef_t in the standard library or must I write my
own? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sofia |
last post by:
My name is Sofia and I have for many years been running a personals site,
together with my partner, on a non-profit basis. The site is currently not
running due to us emigrating, but during its last year we got traffic of
between 2000 - 2500 unique visitors per day. We are now about to re-launch
the site from Sweden and we need to purchase a script to run it.
Having looked at what is available on the net I have realised that we need a...
|
by: Mike Kamermans |
last post by:
I hope someone can help me, because what I'm going through at the moment
trying to edit XML documents is enough to make me want to never edit XML
again.
I'm looking for an XML editor that has a few features that you'd expect
in any editor, except nearly none of them seem to have:
1 - Search and repalce with Regular Expressions.
2 - Search and Replace in an Xpath context.
3 - User specified tag-generation for either on a...
|
by: drdoubt |
last post by:
using namespace std
In my C++ program, even after applying , I need to use the std
namespace with the scope resolution operator, like, std::cout,
std::vector. This I found a little bit cumbersome to always include
std.
I somewhere found a trick to overcome this problem. By using
using std::cout;
|
by: sk |
last post by:
I have an applicaton in which I collect data for different parameters
for a set of devices. The data are entered into a single table, each
set of name, value pairs time-stamped and associated with a device.
The definition of the table is as follows:
CREATE TABLE devicedata
(
device_id int NOT NULL REFERENCES devices(id), -- id in the device
|
by: Bob.Henkel |
last post by:
I write this to tell you why we won't use postgresql even though we wish we
could at a large company. Don't get me wrong I love postgresql in many
ways and for many reasons , but fact is fact. If you need more detail I
can be glad to prove all my points. Our goal is to make logical systems.
We don't want php,perl, or c++ making all the procedure calls and having
the host language to be checking for errors and handleing all the...
| |
by: google |
last post by:
I have a database with four table. In one of the tables, I use about
five lookup fields to get populate their dropdown list. I have read
that lookup fields are really bad and may cause problems that are hard
to find. The main problem I am having right now is that I have a report
that is sorted by one of these lookup fields and it only displays the
record's ID number. When I add the source table to the query it makes
several records...
|
by: Phil |
last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and
made into a TIF file and placed on the client (could be in temp internet
dir). The reason we need it in TIF format is there are multiple pages per
invoice.
How can I grab the data, make the TIF, place it on the client and then Open
with the clients default program for veiwing TIF's (usually Microsoft Picture
and Fax Viewer).
Please help.
|
by: Sai Kit Tong |
last post by:
In the article, the description for
"Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports
or
Managed Entry Points"
suggests the creation of the class ManagedWrapper.
If I need to build multiple mixed mode dll's used by a consumer application,
do I have to implement multiple ManagedWrapper's (each embedded in
indiviudal DLL project) and call all of them in my consumer application?
|
by: Michael R. Pierotti |
last post by:
Dim reg As New Regex("^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$")
Dim m As Match = reg.Match(txtIPAddress.Text)
If m.Success Then
'No need to do anything here
Else
MessageBox.Show("You need to enter a valid IP Address", "Error!",
MessageBoxButtons.OK, MessageBoxIcon.Hand)
txtIPAddress.Focus()
Return
End If
|
by: U S Contractors Offering Service A Non-profit |
last post by:
Brilliant technology helping those most in need Inbox
Reply
U S Contractors Offering Service A Non-profit
show details
10:37 pm (1 hour ago)
Brilliant technology helping those most in need Inbox
Reply
from Craig Somerford <uscos@2barter.net>
hide details 10:25 pm
(3 minutes ago)
|
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: 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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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: 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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
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.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |