![]() |
| ||
| C and C++ Timesaving Tips Post your tips for making life easier in C and C++. I'll start: Standard vector object initialization The biggest problem with the standard vector class is that one can't use an array initializer. This forces us to do something like this: #include <iostream>Anyone who's had the rule of redundancy pounded into their head knows that the previous code could be wrapped in a loop: #include <iostream>However, it's not terribly elegant, especially for a vector of complex types. So, Narue's first timesaving tip for C++ is to use a temporary array so that you can make use of an initializer. Because the vector class defines a constructor that takes a range of iterators, you can use the array to initialize your vector: #include <iostream> |
| ||
| Re: C and C++ Timesaving Tips One of my favorite discoveries in C++ regards displaying such a list. Initially one might do this. #include <iostream>Or perhaps even this. #include <iostream>But I happend upon the following that can make things even easier. #include <iostream>The output for all of these is merely this. Quote:
|
| ||
| Re: C and C++ Timesaving Tips From the code snippet on the very same subject is another way to initialize a vector ... [php] // another way to load a vector ... vector<char> cV(50, '-'); cout << "this vector has 50 char - :\n"; for(k = 0; k < cV.size(); k++) { cout << cV[k]; } cout << endl; [/php] |
| ||
| Re: C and C++ Timesaving Tips How to remove duplicate elements from a vector ... [php] // load a vector with an integer array and remove duplicates ... int b[] = {0, 1, 2, 3, 4, 3, 4, 5, 6, 4}; // load using (array, array + NumberOfElements) vector<int> iV4(b, b + sizeof(b)/sizeof(int)); // remove the duplicates with sort(), erase() and unique() cout << "removed the duplicate elements:\n"; sort( iV4.begin(), iV4.end() ); iV4.erase( unique( iV4.begin(), iV4.end() ), iV4.end() ); // show the result for(k = 0; k < iV4.size(); k++) { cout << setw(8) << iV4[k]; } cout << endl; ... [/php] |
| ||
| Re: C and C++ Timesaving Tips Removing a newline in C The fgets function in C is annoying in that it copies a newline character to the buffer: #include <stdio.h>When you run this program and type "test", this is the output: |testThis is a good feature for dealing with long lines and such, but for the most part it serves only to frustrate beginners. Experienced programmers have learned to remove the newline with tricks such as: #include <stdio.h>Or more commonly: #include <stdio.h>The annoying part of this issue is that one often is required to declare a variable, test for a newline character explicitly, and replace it explicitly. A quick timesaver when writing code is the strcspn function: #include <stdio.h>The downside to this timesaver is that it doesn't save time during execution. The strcspn trick will usually be slower than most other methods of removing a newline. However, because the performance hit of input far outweighs any difference in speed for removing the newline character. |
| ||
| Re: C and C++ Timesaving Tips Quote:
char *fget(char *s, int size, FILE * stream) |
| ||
| Re: C and C++ Timesaving Tips >i'd just write a new function and get it over with That's one way to go about it, but since fgets is already there and is presumably written with performance in mind, it's smarter to make use of it: char *reads ( char *s, size_t limit, FILE *stream )It's also easier to verify correctness when you use the standard library instead of trying to rewrite it. |
| ||
| Re: C and C++ Timesaving Tips Quote:
Quote:
speaking of weird code, my function should return s and not ptr. |
| ||
| Re: C and C++ Timesaving Tips >It's easier to verify correctness when your functions have a single exit point (hint) That's debatable. Often it's harder in my experience because you have to bend over backward with unnecessary constructs to ensure a single exit point. For trivial functions such as the ones we posted, it's hardly brain surgery to verify correctness. For non-trivial functions, it's difficult to maintain simplicty and still adhere to structured programming "good practice". I prefer to err on the side of simplicity and transparency. >had in mind when devising the stdio library was performance. Performance was a primary concern for the design of C and its libraries. As another programmer who's implemented the C89 library in its entirety, I'm not entirely sure what you're referring to. Let me know in PM so we don't end up going too far off topic. >speaking of weird code, my function should return s and not ptr Oh good, I thought maybe my mentioning correctness was too subtle of a hint. ;) |
| ||
| Re: C and C++ Timesaving Tips Seeing as this thread is about timesaving what about inheritance? Its a good time saver especially for the masses intent on game creation. Say you make a class such as class CBASE_ENTITY if you wanted to specialise and create objects such as moving platforms / players / weapon models ect you can simply class CBASE_PLAYER : pubic CBASE_ENTITY { Without re-coding the entire class. Quite a few people forget inheritance and end up making some really messy classes! Dont forget that pointers are interchangable between derived and base classes which is VERY VERY useful.... An example of this timesaving would be to create a model cass, inherit a weapon class off it, and inherit weapons off the weapon base class. Then also pointers can be moved around the weapons derived classes.... im sure you can work out where this is going! If you look at the Half-Life source they make extensive use of inheritance to make a class oreentated (spelling) source which works very well.... |
| All times are GMT -4. The time now is 6:58 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC