Need help with STL.

J

Jason Heyes

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;
if (object.is_true())
{
found = true;
break;
}
}

Any help is very appreciated.
 
U

Unforgiven

Jason Heyes said:
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;
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?
 
M

Mike Wahler

Jason Heyes said:
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;
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
 
D

Daniel T.

Jason Heyes said:
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;
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();
 
J

Jason Heyes

Unforgiven said:
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?

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>::eek:perator() 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?
 
J

Jason Heyes

Daniel T. said:
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.
 
J

Jason Heyes

Jason Heyes said:
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;
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.
 
D

Daniel T.

Jason Heyes said:
Jason Heyes said:
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;
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();
 
J

Jason Heyes

Daniel T. said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top