My first project

M

Michael DOUBEZ

kwikius a écrit :
Michael said:
And using POD:
int array [] = {1,2,3,4,5};
std::vector<int> vect;
//should work
vect.insert(&array[0],&array[sizeof(array)/sizeof(int)]);

hmm.... clearly superior to:

for_each(array,push_back(vect));

:)

Yes, I agree that POD usage in this case is as clear as the bottom of my
boots. ;)

And you are right, it doesn't work any more (I used to do that before
iterators became iterators :) ) but I am sure one can easily design
something like the normal_iterator in no time.

Regards,
Michael
 
M

Michael DOUBEZ

kwikius a écrit :
I would be interested to see what the boost lambda version looks like
too. Not knocking it. Just have no idea. Anyone care to reveal it ?

With your notation:

for_each(array,vect.push_back(_1));


Michael
 
M

Michael DOUBEZ

Michael DOUBEZ a écrit :
kwikius a écrit :

With your notation:

for_each(array,vect.push_back(_1));

I was quick:
for_each(array,bind(&std::vector::push_back,vect,_1));

Sorry
Michael
 
K

kwikius

Michael said:
Michael DOUBEZ a écrit :

I was quick:
for_each(array,bind(&std::vector::push_back,vect,_1));

Unfortunately if you run it you will find there is a problem:

int main()
{
// int array [] = {1,2,3,4,5};

boost::array<int,5> array = {1,2,3,4,5};

std::vector<int> vect;

for_each(array,boost::bind(&std::vector<int>::push_back,vect,_1));
//for_each(array,push_back(vect));
std::cout << "elements in vect= " << vect.size() <<'\n';
for_each(vect,output(std::cout,'\n'));

}
This compiles and runs but doesnt do what it should. OTOH array version
fails with some const non const issue.

Question is how complicated are we prepared to get in the interests of
simplicity ;-)

I'm also quite amused that my custom for_each came in handier than the
official version ;-)

That said I bet we get lambda in the next version of the standard, but
no simple for_each. They'll not miss an opportunity to make the
blindingly simple impossibly complicated :)

regards
Andy Little
 
C

Caleb

Okay, thanks for all the help, it's very much appreciated, I'm just
going to keep doing the room exits the way I'm doing them....it's
working out just fine ;)
 
B

BobR

kwikius wrote in message ...
Michael said:
I don't see the advantage of defining a for_each specifically for arrays
since there is boost::array that works fine (even though it is not
really a container).

I covered that in the example code FWIW. If you want to use
boost::array ( or another container) you can do, but you have to
specify the size, which can be inconvenient when adding stuff to the
initialiser list.

int main(){
// int array [] = {1,2,3,4,5};
boost::array<int,5> array = {1,2,3,4,5};
std::vector<int> vect;
for_each(array,push_back(vect));
for_each(array,output(std::cout,'\n'));
}
regards
Andy Little

Why not?:

int main(){
int array[] = {1,2,3,4,5};
std::vector<int> vect( array, array+(sizeof array/sizeof *array) );

std::copy( vect.begin(), vect.end(),
std::eek:stream_iterator<int>(std::cout, " ") );
return 0;
}

Sorry, I must have missed a point somewhere. :-}
Were you guys trying to find a home for 'for_each' or Boost?
 
K

kwikius

BobR said:
kwikius wrote in message ...
Michael said:
I don't see the advantage of defining a for_each specifically for arrays
since there is boost::array that works fine (even though it is not
really a container).

I covered that in the example code FWIW. If you want to use
boost::array ( or another container) you can do, but you have to
specify the size, which can be inconvenient when adding stuff to the
initialiser list.

int main(){
// int array [] = {1,2,3,4,5};
boost::array<int,5> array = {1,2,3,4,5};
std::vector<int> vect;
for_each(array,push_back(vect));
for_each(array,output(std::cout,'\n'));
}
regards
Andy Little

Why not?:

int main(){
int array[] = {1,2,3,4,5};
std::vector<int> vect( array, array+(sizeof array/sizeof *array) );

Why not? Because Its a mess! 2 sizeof, raw pointers and 4 repeats of
the array variable.
Yuch. If you read the O.P you also need to assign that. So we have:

vect = std::vector<int>( array, array+(sizeof array/sizeof
*array) );

compared with :

for_each(array,push_back(vect));

What is loverly concise and expressive in comparison withe the previous
coding crime!
Sorry, I must have missed a point somewhere. :-}

Maybe... Its my fault I guess, for casting my pearls before swine ...

:)

regards
Andy Little
 
M

Michael DOUBEZ

kwikius a écrit :
Michael said:
Michael DOUBEZ a écrit :
I was quick:
for_each(array,bind(&std::vector::push_back,vect,_1));

Unfortunately if you run it you will find there is a problem:

int main()
{
// int array [] = {1,2,3,4,5};

boost::array<int,5> array = {1,2,3,4,5};

std::vector<int> vect;

for_each(array,boost::bind(&std::vector<int>::push_back,vect,_1));
//for_each(array,push_back(vect));
std::cout << "elements in vect= " << vect.size() <<'\n';
for_each(vect,output(std::cout,'\n'));

}
This compiles and runs but doesnt do what it should. OTOH array version
fails with some const non const issue.

Question is how complicated are we prepared to get in the interests of
simplicity ;-)

I'm also quite amused that my custom for_each came in handier than the
official version ;-)

That said I bet we get lambda in the next version of the standard, but
no simple for_each. They'll not miss an opportunity to make the
blindingly simple impossibly complicated :)

True but I am not a specialist of lambda programing :)
The main advantage I saw was the following notation:
for_each(vect,std::cout<<_1<<'\n');

It is true that in this case, your way is more elegant but then again I
expect a single call to std::vector::insert to be more efficient (which
is of no import here) because beeing able to compute the distance
between begin() and end() he can allocate the memory (or fail to do it)
at once and make only one move of objects. I have to check this in the
implementation of gcc.

Before calling your function, I would have to call std::vector::reserve
or use the following implementation:
template <typename T,int N>
void vector_insert(std::vector<T> &v, T (&ar)[N])
{
v.reserve(v.size()+N);
for (int i = 0; i < N;++i){
v.push_back(ar);
}
}

and then :
vector_insert(vect,array);

I hope the compiler inlines the functions correctly, otherwise I may
have a function for each size of array I use (which is not the case
begin/end range).

Michael
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,274
Latest member
JessMcMast

Latest Threads

Top