Wednesday, November 18, 2015

Digest for comp.lang.c++@googlegroups.com - 10 updates in 5 topics

Noob <dontspam@me.com>: Nov 18 08:12PM -0200

On 18/11/2015 19:56, Ian Collins wrote:
 
> Does this add any value? Can't you sum as you go in the loop?
 
>> for (unsigned int i = 0; i < nvars-2; i++)
 
> What if nvars < 2??
 
Thank you Paavo and Ian.
 
The expression nvars-2 was a last minute typo, but I got the idea
about asserts. I'll also be using size_t from now on.
 
About "temp" and the loop, I am curious about some possible one-liner
for my return statement, eliminating "temp" and that loop, and possibly
letting the compiler do a better job. Any STL solution for that?
 
Thank you again!
Ian Collins <ian-news@hotmail.com>: Nov 19 11:17AM +1300

Noob wrote:
 
> About "temp" and the loop, I am curious about some possible one-liner
> for my return statement, eliminating "temp" and that loop, and possibly
> letting the compiler do a better job. Any STL solution for that?
 
Maybe now would be a good to for you to restate your requirements and
present fresh code?
 
--
Ian Collins
Paavo Helde <myfirstname@osa.pri.ee>: Nov 18 04:28PM -0600


> About "temp" and the loop, I am curious about some possible one-liner
> for my return statement, eliminating "temp" and that loop, and possibly
> letting the compiler do a better job.
 
double sum = 0.0;
// ...
return sum;
 
> Any STL solution for that?
 
STL algorithms are mostly meant for STL containers, but here there is no
need to create the 'temp' STL containar, meaning there is no need to use a
"STL solution".
Noob <dontspam@me.com>: Nov 18 08:39PM -0200

On 18/11/2015 20:17, Ian Collins wrote:
>> letting the compiler do a better job. Any STL solution for that?
 
> Maybe now would be a good to for you to restate your requirements and
> present fresh code?
 
Yes.
 
What I have now is
 
int eval_lgk(std::vector<int>& coords, std::vector<int>& factsgk) {
 
assert( factsgk.size() == coords.size() && coords.size() >= 1 );
 
size_t nvars = coords.size();
int partial = coords.back();
 
for (size_t i = 0; i < nvars-1; i++)
{
partial+= factsgk[i] * ( coords[i] - 1 );
}
 
return partial;
 
}
 
I still have an explicit loop that I'd like to get rid of, if possible.
I understand somewhere something will perform this loop, but I'm
looking for something that will do it efficiently without me coding it.
 
Thanks!
Noob <dontspam@me.com>: Nov 18 08:43PM -0200

On 18/11/2015 20:28, Paavo Helde wrote:
 
> STL algorithms are mostly meant for STL containers, but here there is no
> need to create the 'temp' STL containar, meaning there is no need to use a
> "STL solution".
 
I see, but I still have std::vectors to operate on.
 
Thanks!
ram@zedat.fu-berlin.de (Stefan Ram): Nov 18 10:36PM

>I'd like to write it in a way I can get good speed.
 
You already have good speed.
 
If you really want to optimize: Start by using a profiler.
agent@drrob1.com: Nov 18 05:33PM -0500

On Wed, 18 Nov 2015 14:30:21 +0100, "Alf P. Steinbach"
 
>Use Boost file system library.
 
>Cheers & hth.,
 
>- ALf
 
Hi ALf.
 
I look at Boost after you suggested that to me a week or so ago. I
found that to be worse than scandir, for a newbie to C++.
 
But I will look more into it. For the meantime, scandir is working
for me, and I understand it a little better now.
 
Thanks,
Rob
woodbrian77@gmail.com: Nov 18 02:14PM -0800

On Wednesday, November 18, 2015 at 12:41:53 AM UTC-6, Tobias Müller wrote:
 
> > The convention of placing data members last doesn't cover that case.
 
> Consequently I never use in-class function definitions (except for some
> rare cases where the body is empty and I'm lazy).
 
I'm using them more over the past year or two. I've
found some minor advantages from going this route in
terms of executable/binary code size.
 
> IMO even the most simple in-class function definitions hurt the readability
> of a class definition.
 
Well, there are some advantages to using in class
functions like less lines of code. Imo that makes
the library or program, as a whole, easier to work
with. The more compact form for the software
also helps in terms of downloading/bandwidth.
If a few compilers look like they can do a better
job with the internal version, I may go that route.
 
 
Brian
Ebenezer Enterprises - In G-d we trust.
http://webEbenezer.net
Ian Collins <ian-news@hotmail.com>: Nov 19 11:26AM +1300

Tobias Müller wrote:
 
>> The convention of placing data members last doesn't cover that case.
 
> Consequently I never use in-class function definitions (except for some
> rare cases where the body is empty and I'm lazy).
 
So you never use templates?
 
> IMO even the most simple in-class function definitions hurt the readability
> of a class definition.
 
Short, especially one line, public inline functions have their place in
non-template code, I prefer not to have inline protected or private
inline function. The former may be heavily used in client code, so
making them available to the optimiser for inlining makes sense.
 
--
Ian Collins
Juha Nieminen <nospam@thanks.invalid>: Nov 18 09:18AM

> Can anyone give me any guidelines, perhaps by giving a reference, on the general technique on translating recursive algorithms into iterative algorithms?
 
> For example, the basic (inorder/ preorder/ postorder) traversals of binary trees are trivial to implement recursively. I've heard it said that these methods can be done iteratively using a stack, but I can't find it implemented anywhere.
 
Technically speaking "iterative" means that you don't need a stack (which
size is proportional to the number of iterations). Even if you avoid
calling the same function from within itself, ie. you seemingly avoid
recursion, if you nevertheless need a stack of data which size is
proportional to the number of iterations, your algorithm is recursive.
 
Some algorithms can be implemented recursively and genuinely
iteratively (such as merge sort), but others can't (such as quicksort).
 
Oftentimes forcefully trying to make a naturally recursive algorithm
into an iterative one (either a genuine or a faux one) only makes
the program more complex than it needs to be. (However, depending on
the case it might make it more efficient, but not always.)
 
--- news://freenews.netfront.net/ - complaints: news@netfront.net ---
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: