943,522 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 89436
  • C++ RSS
You are currently viewing page 6 of this multi-page discussion thread; Jump to the first page
Dec 21st, 2007
0

Re: C and C++ Timesaving Tips

Referring to Mr.BountyX tips on time saving ,,
"I know this may not seem like a super time saving technique, but the way you name your variables will help save time and reduce compile time errors.."

Yes indeed. This helps a lot.

Variable Naming conventions are very important in programming and it also reduces the code reviewing time a lot .

Eg:

consider a program which has

int RecId;

For a novel programmer who is looking at your code , may wonder at a particular point of the code about the data type of the variable.
b'coz "RecId" doesnt anyhow says whether it is a integer or string or char .

Some thing like this would help the programmer easily to understand ...

int nRecID.

The ' n ' at the begin says it as integer. so when ever someone look at it he can easily understand
datatype of the variable instead of looking at the declaration again ..

Famous conventions:
eg:

int nNumber;
string sData;
float fVal
CString csData;

For Global variable's prefix it like
' g_VARIABLENAME''
Reputation Points: 11
Solved Threads: 5
Newbie Poster
Aashath is offline Offline
18 posts
since Dec 2007
Dec 21st, 2007
0

Re: C and C++ Timesaving Tips

C++ STL


Quote ...
vector<int> vec_nVal;
// Store elements into vector ...
// Accessing vector elements :

int ninitialize;
for ( ninitialize = 0 ; ninitialize < vec_nVal.size() ; ninitialize++ )
{
int nVal = vec_nVal[ninitialize];

cout<< nVal ;
}
Two Time saving Tips;
1. Try avoiding function call at the for loop condition check
Quote ...
for ( ninitialize = 0 ; ninitialize < vec_nVal.size() ; i++ )
Function calls are costlier . So in the above code , for every iteration,vector::size() function will be called , which is not recommended
unless or otherwise if you know that vector size is going to change inside the loop .

For example : If the vector contains 100K elements then vector::size will be called 100K times.

so the best practice will be like , getting the size of the vector before the loop begin and substitute the variable accordingly . something like this ...

Quote ...
"int nVecSize = vec_nVal.size();
for ( ninitialize = 0 ; ninitialize < nVecSize ; i++ ) "
2 . Use Iterators
One of the main concept of stl's are iterator. use it when ever required .

The above sample program can be written like this for the best performance and time saving .

Quote ...
vector<int>::iterator itrNumber;
itrNumber = vec_nVal.begin();

while( itrNumber != vec_nVal.end())
{
int nVal =*(itrNumber );
cout<<nVal;
itrNumber++
}
by using iterator the element retrieval time will be faster when compare to the previous one[ vector[] ] .

The same holds good for all stl containers . (eg:stl::map)

I am so sure that the above one will improve your performance and save your time leap and bound if you are using it excessively.
Reputation Points: 11
Solved Threads: 5
Newbie Poster
Aashath is offline Offline
18 posts
since Dec 2007
Dec 22nd, 2007
0

Re: C and C++ Timesaving Tips

W.r.t std::vector.... Especially while loading ( doing a push_back )if u already know the the number of elements to be inserted then u can reserve it before u start loading. Use the reserve(...) function.
Reputation Points: 10
Solved Threads: 4
Newbie Poster
Rajith Cherian is offline Offline
22 posts
since Dec 2007
Jan 6th, 2008
0

Re: C and C++ Timesaving Tips

This was asked in another forum I go to and I came up with the following:

Reading contents of a file directly to a vector
cpp Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. int main( void ) {
  7. std::ifstream in( "shorts.txt", std::ios::binary );
  8.  
  9. if ( in ) {
  10. std::vector< short > vs;
  11. vs.insert( vs.begin(), std::istream_iterator<short>(in), std::istream_iterator<short>() );
  12. std::copy( vs.begin(), vs.end(), std::ostream_iterator<short>(std::cout, "\n"));
  13. }
  14.  
  15. return 0;
  16. }
It's along the same lines as Naure's 'printing vector contents without a loop' trick. Use with caution though. Especially if you have a mix of types:
cpp Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. int main( void ) {
  7. std::ifstream in( "shorts.txt", std::ios::binary );
  8.  
  9. if ( in ) {
  10. std::vector<short> vshort;
  11. std::vector<std::string> vstr;
  12.  
  13.  
  14. // Read shorts until fail
  15. vshort.insert( vshort.end(), std::istream_iterator<short>(in), std::istream_iterator<short>() );
  16. // Clear stream
  17. in.clear();
  18.  
  19. // Read strings until fail, inadvertently reads to end of file.
  20. vstr.insert( vstr.end(), std::istream_iterator<std::string>(in), std::istream_iterator<std::string>() );
  21.  
  22. std::copy( vshort.begin(), vshort.end(), std::ostream_iterator<short>(std::cout, "\n") );
  23. std::copy( vstr.begin(), vstr.end(), std::ostream_iterator<std::string>(std::cout, "\n") );
  24. }
  25.  
  26. return 0;
  27. }
Will store numbers in the vector of strings until it fails. Then after clearing the stream it'll read in the rest of the contents until finished. But of course this is the case in all file IO.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Jan 16th, 2008
-2

Re: C and C++ Timesaving Tips

tips
1 . use loops
2. use pointer
3.use functions
4. use method overloading
5 use typedef for initialisation
Last edited by Narue; Jan 16th, 2008 at 9:38 am. Reason: Snipped spam
Reputation Points: 7
Solved Threads: 2
Newbie Poster
bector is offline Offline
13 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: not sure why program crashes
Next Thread in C++ Forum Timeline: helpppppp me with this program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC