473,394 Members | 1,889 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,394 software developers and data experts.

Need help with STL.

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_true() 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.
Jul 22 '05 #1
10 1417
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41**********************@news.optusnet.com.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_true() 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

Jul 22 '05 #2
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41**********************@news.optusnet.com.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_true() 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<MyClass>& v)
{
return std::find_if(v.begin(), v.end(), pred) != v.end();
}

int main()
{
std::vector<MyClass> 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<MyClass>& v)
{
return std::find(v.begin(), v.end(), true) != v.end();
}

int main()
{
std::vector<MyClass> vec (10);
std::cout << any_true(vec) << '\n';
vec[5] = true;
std::cout << any_true(vec) << '\n';

return 0;
}

HTH,
-Mike
Jul 22 '05 #3
"Jason Heyes" <ja********@optusnet.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_true() 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_true ) ) != objects.end();
Jul 22 '05 #4
"Unforgiven" <ja*******@hotmail.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(MyClass::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?
Jul 22 '05 #5
"Daniel T." <po********@eathlink.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_true ) ) != 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_ref_t, say? Thanks.
Jul 22 '05 #6
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41**********************@news.optusnet.com.au ...
"Daniel T." <po********@eathlink.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_true ) ) != 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_ref_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_equal(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_equal), 32);
bool found = find_if(objects.begin(), objects.end(), pred) !=
objects.end();

Thanks.
Jul 22 '05 #7
In article <41***********************@news.optusnet.com.au> ,
"Jason Heyes" <ja********@optusnet.com.au> wrote:
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:41**********************@news.optusnet.com.au ...
"Daniel T." <po********@eathlink.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_true ) ) != 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_ref_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_equal(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_equal), 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_equal ), 32 ) ) != objects.end();

Or if you really want the pred:

binder2nd< const_mem_fun1_ref_t< bool, MyClass, int> >
MyClassIsEquals32 = bind2nd( mem_fun_ref( &MyClass::is_equal ), 32 );
bool found =
find_if( objects.begin(), objects.end(), MyClassIsEquals32 ) !=
objects.end();
Jul 22 '05 #8
In article <41**********************@news.optusnet.com.au>,
"Jason Heyes" <ja********@optusnet.com.au> wrote:
"Daniel T." <po********@eathlink.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_true ) ) != objects.end();


What happens if objects is const?


Then a const_mem_fun_ref_t is created instead of a mem_fun_ref_t, and
the code works.
Jul 22 '05 #9
"Daniel T." <po********@eathlink.net> wrote in message
news:po******************************@news05.east. earthlink.net...
In article <41**********************@news.optusnet.com.au>,
"Jason Heyes" <ja********@optusnet.com.au> wrote:
"Daniel T." <po********@eathlink.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_true ) ) != objects.end();


What happens if objects is const?


Then a const_mem_fun_ref_t is created instead of a mem_fun_ref_t, and
the code works.


Is there a const_mem_fun_ref_t in the standard library or must I write my
own?
Jul 22 '05 #10
In article <41***********************@news.optusnet.com.au> ,
"Jason Heyes" <ja********@optusnet.com.au> wrote:
"Daniel T." <po********@eathlink.net> wrote in message
news:po******************************@news05.east. earthlink.net...
In article <41**********************@news.optusnet.com.au>,
"Jason Heyes" <ja********@optusnet.com.au> wrote:
"Daniel T." <po********@eathlink.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_true ) ) != objects.end();

What happens if objects is const?


Then a const_mem_fun_ref_t is created instead of a mem_fun_ref_t, and
the code works.


Is there a const_mem_fun_ref_t in the standard library or must I write my
own?


There is one in the stardard.
Jul 22 '05 #11

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

Similar topics

0
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...
7
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...
15
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...
9
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...
3
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...
3
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...
4
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...
8
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...
2
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...
0
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 ...
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: 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
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
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.