473,404 Members | 2,114 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,404 software developers and data experts.

stl and standard library functions

I am somewhat new to STL and the plethora of library functions they have. I
looked at sgi's stl info. and I was overwhelmed so I thought I would present
my question here.

I have
std::map<WWN*, Drive*> m_Drives

I want to take that and run a functor through all of them (the functor has
Drive* has the parameter type) and whichever ones meet the predicate functor
will need to be added to a list of Drive*s. (std::list<Drive*> driveList)

I can write a loop to do this and in fact, I will do it here to show what I
am trying to achieve. But I would rather use the library functions, if
possible.

for ( mapIter i = m_Drives.begin(); i != m_Drives.end(); ++i )
{
if ( i.second->getType == someType && i.second->getCapacity >=
someCapacity )
driveList.push_back(i.second);
}

My code may not compile since I just wrote it on the fly.
My for loop is only few lines long so maybe doing that is better than using
the standard library functions.

However, I read part of Scott Meyers's Effective STL stuff and he says that
standard functions are efficient, correct, and maintainable. Anyway, any
help would be nice.

Thanks.
Jul 19 '05 #1
6 2747
sks_cpp escribió:
I have
std::map<WWN*, Drive*> m_Drives

I want to take that and run a functor through all of them (the functor has
Drive* has the parameter type) and whichever ones meet the predicate functor
will need to be added to a list of Drive*s. (std::list<Drive*> driveList)

I can write a loop to do this and in fact, I will do it here to show what I
am trying to achieve. But I would rather use the library functions, if
possible.

for ( mapIter i = m_Drives.begin(); i != m_Drives.end(); ++i )
{
if ( i.second->getType == someType && i.second->getCapacity >=
someCapacity )
driveList.push_back(i.second);
}


Something like that (untested):

class SelectDrive {
public:
SelectDrive (std::list <Drive *> & driveList, Type type, int capacity)
:
driveList (driveList), type (type), capacity (capacity)
{ }
void operator () (const std::map <WWN *, Drive *>::value_type & i)
{
if (i.second->getType == someType && i.second->getCapacity >=
capacity)
driveList.push_back (i.second);
}
private:
std::list <Drive *> & driveList;
Type type;
int capacity;
};

An use as:

std::for_each (m_Drives.begin (), m_Drives.end (),
SelectDrive (driveList, someType, someCapacity) );

Regards.
Jul 19 '05 #2
In article <BS*********************@news2.central.cox.net>,
sk*****@hotmail.com says...
I am somewhat new to STL and the plethora of library functions they have. I
looked at sgi's stl info. and I was overwhelmed so I thought I would present
my question here.

I have
std::map<WWN*, Drive*> m_Drives

I want to take that and run a functor through all of them (the functor has
Drive* has the parameter type) and whichever ones meet the predicate functor
will need to be added to a list of Drive*s. (std::list<Drive*> driveList)


std::remove_copy_if seems to be what you're looking for. Using this
you'll have to reverse the sense of your predicate -- it copies
elements, removing those for which the predicate is true, rather than
only copying those for which it is true.

// warning: untested code.
bool your_predicate(std::pair<WWN*, Drive *> p) {
return (p.second->getType != someType ||
p.second->getCapacity < someCapacity);
}

std::remove_copy_if(m_drives.begin(), m_drives.end(),
std::inserter(driveList, driveList.end()),
your_predicate);

Please don't ask me why the committee preferred remove_copy_if to
copy_if -- I have no idea, and mostly disagree. At least to me, the
name std::remove_copy_if sounds like it should be a variant of
std::unique, or something on that order, but it's basically just the
(unfortunately nonexistent) std::copy_if having a really bad hair day.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #3
"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:MP************************@news.clspco.adelph ia.net...
std::remove_copy_if(m_drives.begin(), m_drives.end(),
std::inserter(driveList, driveList.end()),
your_predicate);
I don't want to remove from the map - just insert the ones that meet the
criteria into the list. Is that what the above code does? What does inserter
do?
Please don't ask me why the committee preferred remove_copy_if to
copy_if -- I have no idea, and mostly disagree. At least to me, the
name std::remove_copy_if sounds like it should be a variant of
std::unique, or something on that order, but it's basically just the
(unfortunately nonexistent) std::copy_if having a really bad hair day.


I might have to agree with you here.

Thanks for your suggestions/comments.
Jul 19 '05 #4
"Julián Albo" <JU********@terra.es> wrote in message
news:3E***************@terra.es...
class SelectDrive {
public:
SelectDrive (std::list <Drive *> & driveList, Type type, int capacity)
:
driveList (driveList), type (type), capacity (capacity)
{ }
void operator () (const std::map <WWN *, Drive *>::value_type & i)
{
if (i.second->getType == someType && i.second->getCapacity >=
capacity)
driveList.push_back (i.second);
}
private:
std::list <Drive *> & driveList;
Type type;
int capacity;
};


Thanks for your coments. I found something interesting with your operator
method. It takes "value_type" as its parameter. Now, could it have taken a
pair? What if you overload the operators with ambiguous parameters, such as
"value_type" as you have it and "pair<WWN*, Drive*>" - is that a compiler
error or does one have precedence over the other?

Thanks.

Jul 19 '05 #5
In article <ql*********************@news2.central.cox.net>,
sk*****@hotmail.com says...
"Jerry Coffin" <jc*****@taeus.com> wrote in message
news:MP************************@news.clspco.adelph ia.net...
std::remove_copy_if(m_drives.begin(), m_drives.end(),
std::inserter(driveList, driveList.end()),
your_predicate);
I don't want to remove from the map - just insert the ones that meet the
criteria into the list.


That's part of what I meant about remove_copy_if having a deceiving
name. It leaves the contents of the original container unaffected. It
just removes the items from the copy -- which is to say that if they
meet the criteria, it doesn't copy them.
Is that what the above code does? What does inserter do?


Yes. std::inserter is just a utility function that creates an
insert_iterator for a particular spot in a particular container. In
case you're not familiar with insert_iterators (and
back_insert_iterators, etc.) they're iterators that insert items into a
collection -- i.e. they don't just overwrite items at locations, but
create space for new ones. e.g. with something like this:

std::vector x, y;

for(int i=0; i<10; ++i)
x.push_back(i);

std::copy(x.begin(), x.end(), back_inserter(y, y.end());

the back_inserter creates an iterator that basically does a push_back to
put each item into y. This saves you from having to resize y to the
correct size ahead of time, which would be trivial in the situation
above, but much more difficult in a situation like yours, where you
don't know ahead of time how many items you'll be inserting into the
list.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #6
In article <MP************************@news.clspco.adelphia.n et>,
jc*****@taeus.com says...

[ ... ]
std::copy(x.begin(), x.end(), back_inserter(y, y.end());


Oops -- with back_inserter, you don't need to specify an iterator, since
it always inserts at the back. I.e. the call above should be just:

std::copy(x.begin(), x.end(), back_inserter(y));

with std::inserter, you have to specify an iterator as the point where
the insertion(s) will take place, since (unlike back_inserter) it's not
implicit.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #7

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

Similar topics

8
by: Raymond Hettinger | last post by:
Comments are invited on the following proposed PEP. Raymond Hettinger ------------------------------------------------------- PEP: 329
3
by: Dave | last post by:
What are some recommended references for the C Standard Library portion of C++? Buying the Standard itself is impractical now that only the hardcopy version is available...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
6
by: junky_fellow | last post by:
On what basis it is decided that a particular funtion should be a part of Standard C library ? Do I get these standard C libraries along with the C compiler ? Or I can use the c complier from...
7
by: Lighter | last post by:
Is overriding a function of a library in accordance with C++ standard? The following code are passed by the VS 2005 and Dev C++. #include <cstdlib> #include <iostream> using namespace std;...
22
by: David Mathog | last post by:
One thing that keeps coming up in this forum is that standard C lacks many functions which are required in a workstation or server but not possible in an embedded controller. This results in a...
85
by: fermineutron | last post by:
Some compilers support __asm{ } statement which allows integration of C and raw assembly code. A while back I asked a question about such syntax and was told that __asm is not a part of a C...
4
by: dustin | last post by:
I've been hacking away on this PEP for a while, and there has been some related discussion on python-dev that went into the PEP: ...
20
by: J de Boyne Pollard | last post by:
MThe library functions which are included to allow process Mlaunch, forking, and termination, imply that it is both Mpossible and desirable for a process to fork itself. This is Ma fundamental...
270
by: jacob navia | last post by:
In my "Happy Christmas" message, I proposed a function to read a file into a RAM buffer and return that buffer or NULL if the file doesn't exist or some other error is found. It is interesting...
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: 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
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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 project—planning, coding, testing,...

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.