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.
Next: Pointers and Dynamic Memory >>
More C++ Articles
More By Chrysanthus Forcha