| | |
C and C++ Timesaving Tips
![]() |
I guess everyone has heard about the Promo event :cheesy: (just kidding)
For anyone intrested in actually saving time while coding and to know of the best programming practices i actually recommend "The C++ programming languguage" by the inventor of C++.
Personally i feel that the code should be organized in the following way:
1. Header file (one per class) and each header having minimalistic clutter. I personally feel that developing good programming practices is a matter of experience, the more you are exposed everyday to correct modular programming practices the more your skills will be honed. Just having the knowledge of modular programming practices does not suffice. (Header.h)
2. Implementation of Header file (Header.cpp)
3. Driver file which utilises the above two files (Driver.cpp)
Hope it helped, bye.
For anyone intrested in actually saving time while coding and to know of the best programming practices i actually recommend "The C++ programming languguage" by the inventor of C++.
Personally i feel that the code should be organized in the following way:
1. Header file (one per class) and each header having minimalistic clutter. I personally feel that developing good programming practices is a matter of experience, the more you are exposed everyday to correct modular programming practices the more your skills will be honed. Just having the knowledge of modular programming practices does not suffice. (Header.h)
2. Implementation of Header file (Header.cpp)
3. Driver file which utilises the above two files (Driver.cpp)
Hope it helped, bye.
I don't accept change; I don't deserve to live.
Not sure if this belongs here, but non-the-less it has saved me LOT of time when I've to work with someone else's code. 
Use Astyle to format the C, C++, C#, and Java code.
Download
Documentation
Project Home
DISCLAIMER:
Be warned, if you're scared of blind tool runs that modify code, don't use this. If you use it on production/customer-release code be sure to test.
.

Use Astyle to format the C, C++, C#, and Java code.
Download
Documentation
Project Home
DISCLAIMER:
Be warned, if you're scared of blind tool runs that modify code, don't use this. If you use it on production/customer-release code be sure to test.
. vector.jpg So here's what happened.
I was on DaniWeb getting help, and while waiting on someone to reply I looked through the sticky threads. I sent my C++ professor the link to Performance Tips and this thread on Thursday (i think). He sent me a message back saying he liked the one about vector something or other.
So on Friday morning he tells the class he added two slides (he uses powerpoint with most of his lectures), and one of them was because of a tip on the website I go to (daniweb
). I attached one of the slides he added.
Congrats Narue!
•
•
•
•
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:
Anyone who's had the rule of redundancy pounded into their head knows that the previous code could be wrapped in a loop:C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); v.push_back(4); v.push_back(5); // Use the vector }
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:C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> using namespace std; int main() { vector<int> v; for (int i = 1; i < 6; i++) v.push_back(i); // Use the vector }
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <vector> using namespace std; int main() { int a[] = {1,2,3,4,5}; vector<int> v(a, a + 5); // Use the vector }
I was on DaniWeb getting help, and while waiting on someone to reply I looked through the sticky threads. I sent my C++ professor the link to Performance Tips and this thread on Thursday (i think). He sent me a message back saying he liked the one about vector something or other.
So on Friday morning he tells the class he added two slides (he uses powerpoint with most of his lectures), and one of them was because of a tip on the website I go to (daniweb
). I attached one of the slides he added.Congrats Narue!
Last edited by Duki; Sep 8th, 2007 at 1:29 pm.
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.
-Edsger Dijkstra
-Edsger Dijkstra
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
using boost.assign http://www.boost.org/libs/assign/doc/index.html is the easiest way to fill containers; and it also works with containers other than vector<>.
where is narue? even if daniweb does not miss her, the c++ forum does.
C++ Syntax (Toggle Plain Text)
#include <vector> #include <list> #include <map> #include <string> #include <boost/assign.hpp> using namespace std; using namespace boost::assign; int main() { vector<string> strings = list_of("abcd")("efg").repeat(5,"hi")("jkl") ; // "abcd" "efg" "hi" "hi" "hi" "hi" "hi" "jkl" strings += "mnop", "qrs", "tuvwx", "yz" ; // "abcd" "efg" "hi" "hi" "hi" "hi" "hi" "jkl" "mnop" "qrs" "tuvwx" "yz" list<int> numbers = list_of(100).repeat(7,22)(75)(80)(85)(90)(83) ; numbers += 555, 666, 1, 2, 3, repeat(4,99), 56, 75, 80 ; map<string,int> pbook = map_list_of("abcd",100)("efg",75)("hi",83) ; insert(pbook)("mnop",555)("qrs",4)("tuvwx",999)("yz",56) ; }
Last edited by vijayan121; Sep 8th, 2007 at 2:26 pm.
![]() |
Similar Threads
- Timesaving Tips (C++)
- Response to time saving tips sticky (C++)
Other Threads in the C++ Forum
- Previous Thread: not sure why program crashes
- Next Thread: helpppppp me with this program
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets





