Question and Answers
1. How do I convert a string to upper case?
To convert a C++ string to upper case you would need to process each char, e.g., something like
#include <cctype>
...
for(int i = 0; i < s.length(); ++i){
s[i] = toupper(s[i]);
}
or
#include <cctype>
...
for(string::iterator p = s.begin(); p != s.end(); ++p){
*p = toupper(*p);
}
2. How do I make a two-dimensional table/matrix/array using vector?
A matrix is just a vector of vectors, that is, a matrix is a vector of rows
where each row is a vector of ints, doubles, or whatever. Thus it's pretty
intuitive to write
vector< vector<int> >
but one "gotcha" is that there must be a space between the '>' characters, to
avoid confusion with the >> operator.
Also, initialization to specify the number of rows and columns is done like
this:
vector< vector<int> >m(2, vector<int>(3)); //2 rows of 3 columns each
// Don't forget the space between the '>' characters!
Now m[i][j] refers to the ith row and jth column of m.
To access the whole table m you can use nested loops like this:
for(int row=0; row<m.size(); ++row){
for(int column=0; column<m[row].size(); ++column){
cout << m[row][column] << ' ';
}
cout << endl;
}
Last modified 04/18/2008