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

STL algorithms fiasco?

each day, I am getting convinced the current STL algorithms (e.g:
for_each) are only useful in few or trivial cases

a good example is dealing with "private classes". let's imagine a
Collection class which is responsible for creating and owning
CollectionItem objects

so, making Collection a friend of CollectionItem allows me to declare
CollectionItem constructor and destructor private.
with that, I am sure only Collection will create and destroy
CollectionItem objects

so, I have something like that:

Collection ctor:
{
data.push_back(new CollectionItem);
....
}

Collection dtor:
{
for( int x = 0; x < data.size(); ++x ) {
delete data[x];
}

this works fine, but it won't compile if I use for_each in dtor

see:

/** generic pointer deleter */
template<typename T>
inline void deleter(T * ptr) { delete ptr; }

Collection dtor:
{
for_each(data.begin(),data.end(),&deleter<Collecti onItem>);
}

it only will compile if CollectionItem dtor is made public :( :( :(

do we have to wait until lambda or closure things appear? and will
they work in the case presented above?

Diego

Feb 27 '07 #1
4 1367
Diego Martins wrote:
each day, I am getting convinced the current STL algorithms (e.g:
for_each) are only useful in few or trivial cases

a good example is dealing with "private classes". let's imagine a
Collection class which is responsible for creating and owning
CollectionItem objects

so, making Collection a friend of CollectionItem allows me to declare
CollectionItem constructor and destructor private.
with that, I am sure only Collection will create and destroy
CollectionItem objects

so, I have something like that:

Collection ctor:
{
data.push_back(new CollectionItem);
...
}

Collection dtor:
{
for( int x = 0; x < data.size(); ++x ) {
delete data[x];
}

this works fine, but it won't compile if I use for_each in dtor

see:

/** generic pointer deleter */
template<typename T>
inline void deleter(T * ptr) { delete ptr; }

Collection dtor:
{
for_each(data.begin(),data.end(),&deleter<Collecti onItem>);
}

it only will compile if CollectionItem dtor is made public :( :( :(

do we have to wait until lambda or closure things appear? and will
they work in the case presented above?
A note: if you make 'deleter' a [static] member of 'Collection',
it should work, no?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 27 '07 #2
On Feb 27, 11:35 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Diego Martins wrote:
each day, I am getting convinced the current STL algorithms (e.g:
for_each) are only useful in few or trivial cases
a good example is dealing with "private classes". let's imagine a
Collection class which is responsible for creating and owning
CollectionItem objects
so, making Collection a friend of CollectionItem allows me to declare
CollectionItem constructor and destructor private.
with that, I am sure only Collection will create and destroy
CollectionItem objects
so, I have something like that:
Collection ctor:
{
data.push_back(new CollectionItem);
...
}
Collection dtor:
{
for( int x = 0; x < data.size(); ++x ) {
delete data[x];
}
this works fine, but it won't compile if I use for_each in dtor
see:
/** generic pointer deleter */
template<typename T>
inline void deleter(T * ptr) { delete ptr; }
Collection dtor:
{
for_each(data.begin(),data.end(),&deleter<Collecti onItem>);
}
it only will compile if CollectionItem dtor is made public :( :( :(
do we have to wait until lambda or closure things appear? and will
they work in the case presented above?

A note: if you make 'deleter' a [static] member of 'Collection',
it should work, no?

V
you're right.
in this case, I'll have to abandon this generic function and put it
inside collection.
or put a friend declaration to the deleter...

I think this is excessive verbose for using a STL algorithm :(

many languages have a construct similar to this:
for each Item in Collection {
use Item;
}

a std::transform could be generalized like that:
// sum algorithm
for each (Item1, Item2, Item3) in (Col1,Col2,Col3) { Item1 =
Item2+Item3; }

:(

Feb 27 '07 #3
Hello,

Diego Martins wrote:
>
many languages have a construct similar to this:
for each Item in Collection {
use Item;
}
The thing coming closest would be boost::lambda or similar libraries,
there are functors in the current STL as well.
>
a std::transform could be generalized like that:
// sum algorithm
for each (Item1, Item2, Item3) in (Col1,Col2,Col3) { Item1 =
Item2+Item3; }
I cannot make any sense out of this. Does it mean that that every
element in Col1 is assigned the sums of any element in Col2 and any
element in Col3? Why so many assignments to the same place, why
assigning all elements in Col1 the sum of the last elements of Col2 and
Col3?

STL has taken the way of ranges (begin,end), not the "item in Container"
idiom, because ranges can be done without language extensions, and they
extend pointer ranges in a natural way, so it can even be used with
pointers, which comes handy at times, e.g. initialization.

Bernd Strieder

Feb 27 '07 #4
Bernd Strieder wrote:
Hello,

Diego Martins wrote:
>many languages have a construct similar to this:
for each Item in Collection {
use Item;
}

The thing coming closest would be boost::lambda or similar libraries,
there are functors in the current STL as well.
>a std::transform could be generalized like that:
// sum algorithm
for each (Item1, Item2, Item3) in (Col1,Col2,Col3) { Item1 =
Item2+Item3; }

I cannot make any sense out of this. Does it mean that that every
element in Col1 is assigned the sums of any element in Col2 and any
element in Col3? Why so many assignments to the same place, why
assigning all elements in Col1 the sum of the last elements of Col2 and
Col3?

STL has taken the way of ranges (begin,end), not the "item in Container"
idiom, because ranges can be done without language extensions, and they
extend pointer ranges in a natural way, so it can even be used with
pointers, which comes handy at times, e.g. initialization.

Bernd Strieder
He is trying to advocate python-like syntax in C++. That construct
means, take each element in each collection and put them in
a tuple (Item1, Item2, Item3) where Item1 is an element of Col1,
Item2 is an element of Col2, etc. Then process them. To some degree
I do find it convenient to use such a syntax but then again, this is
not python. I am a strongly-typed person :)

Feb 27 '07 #5

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

Similar topics

7
by: Anders Borum | last post by:
Hello! I'm starting to dive into algorithms in general (sorting, trees etc.) and am currently reading a book from Robert Sedgewick called "Algorithms in C++, 3rd. Edition" and would like other...
26
by: vlsidesign | last post by:
I am a newbie and going through "The C programming language" by Kernighan & Richie on my own time (I'm not a programmer but I want to learn because it can save me time in my normal job, and it is...
3
by: arnuld | last post by:
i am looking for "algorithms in C++" book. Knuth is FULL of Mathematics, not my kind of author. i checked ACCU and got these (listing only those that are available in my country: 1. Algorithms...
17
by: Happy Man | last post by:
Truth Seeker http://www.thisistruth.org/truth.php?f=TruthSeeker No one is compelled to accept the truth, but it is certainly a shame upon the human intellect when a man is not even...
2
by: =?Utf-8?B?Q3JtTmV3Ymll?= | last post by:
Hi, 1) I want to hone my problem solving skills and be good at logic. How do I achieve this? 2) Where can I find c# puzzles or c# algorithms. 3) How do I print the values of a N X N matrix...
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
1
by: ShubhaDorothy | last post by:
I have a global object 'obj' defined in a file x.cpp without any storage specifier. The object 'obj' is defined (constructor) in another dll.( statically linked dll). when i run the constructor of...
53
by: Vicent Giner | last post by:
Hello. I am new to Python. It seems a very interesting language to me. Its simplicity is very attractive. However, it is usually said that Python is not a compiled but interpreted programming...
12
by: subramanian100in | last post by:
Below is my understanding about count algorithms. Return type of count and count_if algorithms is iterator_traits<InputIterator>::difference_type. If the container contains more than...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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.