Home arrow C++ arrow Page 4 - C++ Programming Tips
C++

C++ Programming Tips


In this article, I give you programming tips that can improve your C++ code. I will cover use of parentheses, long expressions, modifying objects, and more. By the time I'm done, you will hopefully have learned a few ways to make your code faster as well as easier to understand and maintain.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
March 12, 2008
TABLE OF CONTENTS:
  1. · C++ Programming Tips
  2. · Modifying Objects
  3. · Advanced Parameter Passing
  4. · List
  5. · Pointers and Dynamic Memory

print this article
SEARCH DEVARTICLES

C++ Programming Tips - List
(Page 4 of 5 )

Character strings or string class strings

If you use the string class, you have a more extensive set of capabilities than if you use character strings.

Generic vector extraction

Consider the following:

Template (class T)

Void GetValues(vector<T> &A)

{

A.resize(0);

T Val;

While (cin >> Val)

{

A.push_back(Val);

}

}


The syntax template <class T> indicates that what follows is a generic form. In this situation, the form of the function is given. This function template allows different functions to be instantiated based on the particular type of vector used as the actual parameter. For example, if we have the following definitions:

vector<int> X;

vector<float> Y;


the invocations


GetValues(X);

GetValues(Y);


cause functions


void GetValues(vector<int> &A)

{

A.resize(0);

int Val;

while (cin >> Val)

{

A.push_back(Val);

}

}


void GetValues(vector<float> &A)

{

A.resize(0);

float Val;

while (cin >> Val)

{

A.push_back(Val);

}

}

to be defined and invoked.


Accessing multidimensional arrays

Multidimensional arrays are stored in row-major order. Often contiguous sections of memory, called pages, are brought in with the expectation that values near a desired value are more likely to be referenced than values defined elsewhere in memory. Since a page is contiguous memory, it is more likely to contain a complete row than a complete column. So it is generally more efficient to process array elements row-by-row rather than column-by-column. This efficiency comes about in how memory values are brought into the central processing unit, as explained above.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- More Tricks to Gain Speed in Programming Con...
- Easy and Efficient Programming for Contests
- Preparing For Programming Contests
- Programming Contests: Why Bother?
- Polymorphism in C++
- Overview of Virtual Functions
- Inheritance in C++
- Extending the Basic Streams in C++
- Using Stringstreams in C++
- Custom Stream Manipulation in C++
- General Stream Manipulation in C++
- Serialize Your Class into Streams in C++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials