473,772 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

best practices using STL <algorithm>

Hi All,

I'm just getting started learning to use <algorithm> instead of loads
of little for loops, and I'm looking for a bit of advice/mentoring re:
implementing the following...

I have a vector of boost::shared_p tr's, some of which may be NULL. I'd
like to find out if there are any non-NULL elements in the vector.
Here's what I have at the moment.
typedef std::vector< boost::shared_p tr<::CFiCamera> > tCameraVector;
tCameraVector m_vpCameras;
....
....
bool CIIDCCaptureFac ade::IsHardware Available() const
{
//check for at least one initialized (non-NULL) camera
tCameraVector:: const_iterator itCam = std::find_if(
m_vpCameras.beg in(), m_vpCameras.end (),
std::bind2nd( std::not_equal_ to<tCameraVecto r::value_type>( ),
tCameraVector:: value_type() )
);

return itCam != m_vpCameras.end ();
}
Firstly, will this implementation meet the spec? I think it will.

Secondly, does anyone have any feedback on whether this is the 'right'
way to implement this using stl? Of course that's subjective, but I'm
looking for a little reassurance that I'm not getting into bad habits
right off the bat.

Cheers and TIA,

Pete

Jun 7 '06 #1
5 4322
go****@thepete. net wrote:
I'm just getting started learning to use <algorithm> instead of loads
of little for loops,
Cheers to that.
I have a vector of boost::shared_p tr's, some of which may be NULL. I'd
like to find out if there are any non-NULL elements in the vector.
Here's what I have at the moment.

typedef std::vector< boost::shared_p tr<::CFiCamera> > tCameraVector;
tCameraVector m_vpCameras;

bool CIIDCCaptureFac ade::IsHardware Available() const
{
//check for at least one initialized (non-NULL) camera
tCameraVector:: const_iterator itCam = std::find_if(
m_vpCameras.beg in(), m_vpCameras.end (),
std::bind2nd( std::not_equal_ to<tCameraVecto r::value_type>( ),
tCameraVector:: value_type() )
);

return itCam != m_vpCameras.end ();
}
Hmmm, well it's not exactly self-documenting as written, is it? One of
the reasons to use <algorithm> is to make your intent more transparent.
Since you're already in boostville, I'd suggest using the Boost Lambda
and/or Boost Bind libraries for your predicate. The STL binders and
function objects are really needlessly convoluted and verbose by
comparison.
Firstly, will this implementation meet the spec? I think it will.
AFAICT, yes. If it compiles and passes unit test, I'd say you're
probably safe (not on that basis alone, but based on that and the fact
that I don't see anything obviously nonstandard).
Secondly, does anyone have any feedback on whether this is the 'right'
way to implement this using stl? Of course that's subjective, but I'm
looking for a little reassurance that I'm not getting into bad habits
right off the bat.


If you really must do it without the mentioned Boost libraries, I'd
make it more clear by instantiating an actual (static const) null
object, outside of the loop, with an appropriate name, and use it for
comparison. However, you really don't need to do an equality
comparison -- what you want is to check for null. I believe shared_ptr
supports conversion to a boolean type based on nullness, so something
like the following should work:

template <typename T>
bool containsAnyNull s(std::vector< boost::shared_p tr<T> > const& vec)
{
using boost::lambda:: _1;
return std::find_if(ve c.begin(), vec.end(), !_1) != vec.end();
}

Caveat, I didn't take the time to test this code because you're just
asking about style. But it should work fine.

Actually, it might be a little more STL-style to do it like:

template <typename FwIter>
FwIter find_null(FwIte r begin, FwIter end)
{
using boost::lambda:: _1;
return std::find_if(be gin, end, !_1);
}

And compare the return value to end().

Luke

Jun 8 '06 #2
go****@thepete. net писал(а):
Hi All,

I'm just getting started learning to use <algorithm> instead of loads
of little for loops, and I'm looking for a bit of advice/mentoring re:
implementing the following...

I have a vector of boost::shared_p tr's, some of which may be NULL. I'd
like to find out if there are any non-NULL elements in the vector.
Here's what I have at the moment.
typedef std::vector< boost::shared_p tr<::CFiCamera> > tCameraVector;
tCameraVector m_vpCameras;
...
...
bool CIIDCCaptureFac ade::IsHardware Available() const
{
//check for at least one initialized (non-NULL) camera
tCameraVector:: const_iterator itCam = std::find_if(
m_vpCameras.beg in(), m_vpCameras.end (),
std::bind2nd( std::not_equal_ to<tCameraVecto r::value_type>( ),
tCameraVector:: value_type() )
);

return itCam != m_vpCameras.end ();
}
Firstly, will this implementation meet the spec? I think it will.

Secondly, does anyone have any feedback on whether this is the 'right'
way to implement this using stl? Of course that's subjective, but I'm
looking for a little reassurance that I'm not getting into bad habits
right off the bat.

Cheers and TIA,

Pete


Hi.
Here is another solution.

#include <algorithm>
#include <boost/shared_ptr.hpp>

class some_class
{
private:
typedef std::vector<boo st::shared_ptr< int> > container_type;
public:
// ...
bool null_exsists() const
{
return (std::find(cont _.begin(), cont_.end(),
boost::shared_p tr<int>()) != cont_.end());
}
// ...
private:
container_type cont_;
}

Best regards,
Andrey

Jun 8 '06 #3
Thanks for the insightful response Luke, and for taking the time to help
me out. I do appreciate it!

Luke Meyers wrote:

template <typename T>
bool containsAnyNull s(std::vector< boost::shared_p tr<T> > const& vec)
{
using boost::lambda:: _1;
return std::find_if(ve c.begin(), vec.end(), !_1) != vec.end();
}

Caveat, I didn't take the time to test this code because you're just
asking about style. But it should work fine.


Firstly, AFAICT the above implementation will find the first NULL
element. I'm looking to find the first NON-null element.

Here's my revised implementation:

using boost::lambda:: _1;
const boost::shared_p tr<::CFiCamera> nullCamera;

tCameraVector:: const_iterator itCam = std::find_if(
m_vpCameras.beg in(), m_vpCameras.end (),
nullCamera != _1
);

I like your suggestion of creating nullCamera to explicitly show we're
comparing to a null entry. I thought about using the bool casting
operator of boost::shared_p tr but I think using lambda and nullCamera is
clearer. Also, I couldn't figure out the syntax of getting the _1 cast
to a bool in the find_if(...) call!

Cheers,

Pete
Jun 9 '06 #4
On Fri, 09 Jun 2006 10:24:02 -0700, Pete Hodgson
<ne************ ******@thepete. net> wrote:
Here's my revised implementation:

using boost::lambda:: _1;
const boost::shared_p tr<::CFiCamera> nullCamera;

tCameraVector: :const_iterator itCam = std::find_if(
m_vpCameras.beg in(), m_vpCameras.end (),
nullCamera != _1
);


Wrong group, try http://www.boost.org/more/mailing_lists.htm#users

Jun 9 '06 #5
Roland Pibinger wrote:
On Fri, 09 Jun 2006 10:24:02 -0700, Pete Hodgson
<ne************ ******@thepete. net> wrote:
Here's my revised implementation:

using boost::lambda:: _1;
const boost::shared_p tr<::CFiCamera> nullCamera;

tCameraVector: :const_iterator itCam = std::find_if(
m_vpCameras.beg in(), m_vpCameras.end (),
nullCamera != _1
);


Wrong group, try http://www.boost.org/more/mailing_lists.htm#users


boost is by many not considered off topic here since it is linked to
the standardization process of C++ and parts of it will very likely
make it into the next C++ standard revision.

We know what you think about boost so no need to repeat it.

Jun 9 '06 #6

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

Similar topics

20
3854
by: Steffen Brinkmann | last post by:
Hi! I tried to modify the transform algorithm in a way that it doesn't take iterators, but a reference to a container class and a value, because Mostly I need to do an operation of a container and a single number (e.g. multiply all the values in a vector by 3 or so). So, this is my intent: #ifndef ALGORITHM_EXT_HH #define ALGORITHM_EXT_HH
5
2476
by: Alexander Stippler | last post by:
Hello, I have got a list of indices stored as a stl::vector and a range given by two iterators, lets say . The values in this range are not ordered, but I have another range of values, lets say lower to upper. Now I want to remove all values from the stl vector in range having values in . I have just tried around with remove_if and some predicates, but I'm not used to the algorithms library. Perhaps someone can help me.
7
2607
by: Wei | last post by:
Hi all, I found out I can use the max function which is defined in STL <algorithmwithout including this header in my program. The compilers I used are GNU g++ 3.4.4 and Visual Studio C++ 2005. The program is as follows: #include <iostream> //#include <algorithm <-- I don't have to include <algorithmin
10
6076
by: arnuld | last post by:
WANTED: /* C++ Primer - 4/e * * Exercise: 9.26 * STATEMENT * Using the following definition of ia, copy ia into a vector and into a list. Use the single iterator form of erase to remove the elements with odd values from your list * and the even values from your vector.
0
10261
Oralloy
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...
0
10103
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 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...
1
10038
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,...
0
8934
agi2029
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 projectplanning, coding, testing, and deploymentwithout 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...
0
6713
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();...
0
5354
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...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
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
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.