Here is a good example on why either you love C++ or hate it with such terse expression oriented code; I think its pretty cool.
If you want to copy one string to another, one option can be something like this.
void mycopy(char *p, char *q) {
int len = strlen(q);
for(int i=0; i<=len; i++)
p[i] = q[i];
}
However this achieves the same thing as above and is more efficient:
void mycopy(char *p, char *q) {
while(*p++ = *q++);
}
Of course why would you write your own version when you have standard string copy fundtion strcpy in <string.h>
Other similar posts you might be interested to check out:
- April 18, 2010 -- invalid use of incomplete type ‘blah’ (1)
When you try and compile some code and you get an error along the lines of invalid use of an incomplete type 'whatever type' then in most cases it means you need to include the header file where that type is displayed.
For example I had the following events in my header file:
[sourcecode lang="cpp" toolbar="false"]
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
[/sourcecode]
When when I tried to comp... - April 5, 2010 -- Finding an element in a list (0)
Often you need to search through an array or list to find a specific element and of course you need this search to be as fast and efficient as possible. One of the best ways to do this is using a binary predicate function.
A binary function is a function object (which are also called Functors) and is any object which can be called as if it was a function. Depending on your language and platform of choice, Function objects are also known as callback functions, function pointers and delegates (... - March 1, 2010 -- Printing code and making it look pretty (0)
If you are on Linux and want to print some code and also make it look pretty then check out a2ps (Any to postscript filter). Of course if you can avoid printing in the first place and saving paper and trees and make it greener that is ideal - however there are times that is not possible. I tried printing from CDT, but the printing options from CDT just looks plain ugly and big fonts and can spread over 10 pages for a simple code file (spanning 293 lines). Sure I can tweak the font in CDT, but th...
Tags: .code