P
Pete
Is anyone familiar with this book?
Exercise 6-1 of Accelerated C++ asks us to reimplement the frame() and hcat()
operations using iterators. I've posted my answers below, but I'm wondering
if I'm off track here.
First of all, the book has been brisk but reasonable up to chapter 5, and
then suddenly, it exploded. I had to grit my teeth to get through chapter 6,
but the exercises for chap 6 are pretty tame.
Secondly, it was astonishing to see the function that splits lines into words
evaporate into a really small and easily understandable piece of code. I
don't see that happening in my answers to 6-1. My iterator versions don't
look any more efficient or less cluttered.
If anyone is familiar with this book, I would appreciate hearing comments
about whether my answers are on the right track or whether I've missed the
point of the exercise.
Thanks!
Pete
// Horizontal concatenation of two vectors containing strings (indexes)
//
vector<string>
oldhcat( const vector<string> &left, const vector<string> &right )
{
vector<string> ret;
// Add one to leave a space between the pictures.
vector<string>::size_type width1 = maxWidth(left) + 1;
vector<string>::size_type i = 0, j = 0; // i: right, j: left
// Continue until we've seen all rows from both pictures.
while ( i != left.size() || j != right.size() )
{
string s;
// If there's a line of left, copy it to s.
if ( i != left.size() ) s = left[i++];
s += string(width1 - s.size(), ' '); // Pad to full width
// If there's a line of right, copy it to s.
if ( j != right.size() ) s += right[j++];
ret.push_back(s);
}
return ret;
}
// Horizontal concatenation of two vectors containing strings (iterators)
//
vector<string>
hcat( const vector<string> &left, const vector<string> &right )
{
vector<string> ret;
// Add one to leave a space between the pictures.
vector<string>::size_type width1 = maxWidth(left) + 1;
vector<string>::const_iterator i = left.begin();
vector<string>::const_iterator j = right.begin();
// Continue until we've seen all rows from both pictures.
while ( i != left.end() || j != right.end() )
{
string s;
// If there's a line of left, copy it to s.
if ( i != left.end() ) s = *(i++);
s += string(width1 - s.size(), ' '); // Pad to full width
// If there's a line of right, copy it to s.
if ( j != right.end() ) s += *(j++);
ret.push_back(s);
}
return ret;
}
// Put a frame around a vector of strings (indexes)
//
vector<string> oldframe( const vector<string> &v )
{
vector<string> ret;
string::size_type maxlen = maxWidth(v); // width of widest element
// Fix the anomalous zero-input border condition
int starNumber = ( maxlen == 0 ) ? 2 : maxlen + 4;
string border( starNumber, '*' );
ret.push_back(border); // Top border
for ( vector<string>::size_type i = 0; i != v.size(); ++i )
ret.push_back("* " + v + string(maxlen - v.size(), ' ') + " *");
ret.push_back(border); // Bottom border
return ret;
}
// Put a frame around a vector of strings (iterators)
//
vector<string> frame( const vector<string> &v )
{
vector<string> ret;
string::size_type maxlen = maxWidth(v); // width of widest element
// Fix the anomalous zero-input border condition
int starNumber = ( maxlen == 0 ) ? 2 : maxlen + 4;
string border( starNumber, '*' );
ret.push_back(border); // Top border
vector<string>::const_iterator iter;
for ( iter = v.begin(); iter != v.end(); ++iter )
ret.push_back("* " + *iter + string(maxlen - iter->size(), ' ') + " *");
ret.push_back(border); // Bottom border
return ret;
}
Exercise 6-1 of Accelerated C++ asks us to reimplement the frame() and hcat()
operations using iterators. I've posted my answers below, but I'm wondering
if I'm off track here.
First of all, the book has been brisk but reasonable up to chapter 5, and
then suddenly, it exploded. I had to grit my teeth to get through chapter 6,
but the exercises for chap 6 are pretty tame.
Secondly, it was astonishing to see the function that splits lines into words
evaporate into a really small and easily understandable piece of code. I
don't see that happening in my answers to 6-1. My iterator versions don't
look any more efficient or less cluttered.
If anyone is familiar with this book, I would appreciate hearing comments
about whether my answers are on the right track or whether I've missed the
point of the exercise.
Thanks!
Pete
// Horizontal concatenation of two vectors containing strings (indexes)
//
vector<string>
oldhcat( const vector<string> &left, const vector<string> &right )
{
vector<string> ret;
// Add one to leave a space between the pictures.
vector<string>::size_type width1 = maxWidth(left) + 1;
vector<string>::size_type i = 0, j = 0; // i: right, j: left
// Continue until we've seen all rows from both pictures.
while ( i != left.size() || j != right.size() )
{
string s;
// If there's a line of left, copy it to s.
if ( i != left.size() ) s = left[i++];
s += string(width1 - s.size(), ' '); // Pad to full width
// If there's a line of right, copy it to s.
if ( j != right.size() ) s += right[j++];
ret.push_back(s);
}
return ret;
}
// Horizontal concatenation of two vectors containing strings (iterators)
//
vector<string>
hcat( const vector<string> &left, const vector<string> &right )
{
vector<string> ret;
// Add one to leave a space between the pictures.
vector<string>::size_type width1 = maxWidth(left) + 1;
vector<string>::const_iterator i = left.begin();
vector<string>::const_iterator j = right.begin();
// Continue until we've seen all rows from both pictures.
while ( i != left.end() || j != right.end() )
{
string s;
// If there's a line of left, copy it to s.
if ( i != left.end() ) s = *(i++);
s += string(width1 - s.size(), ' '); // Pad to full width
// If there's a line of right, copy it to s.
if ( j != right.end() ) s += *(j++);
ret.push_back(s);
}
return ret;
}
// Put a frame around a vector of strings (indexes)
//
vector<string> oldframe( const vector<string> &v )
{
vector<string> ret;
string::size_type maxlen = maxWidth(v); // width of widest element
// Fix the anomalous zero-input border condition
int starNumber = ( maxlen == 0 ) ? 2 : maxlen + 4;
string border( starNumber, '*' );
ret.push_back(border); // Top border
for ( vector<string>::size_type i = 0; i != v.size(); ++i )
ret.push_back("* " + v + string(maxlen - v.size(), ' ') + " *");
ret.push_back(border); // Bottom border
return ret;
}
// Put a frame around a vector of strings (iterators)
//
vector<string> frame( const vector<string> &v )
{
vector<string> ret;
string::size_type maxlen = maxWidth(v); // width of widest element
// Fix the anomalous zero-input border condition
int starNumber = ( maxlen == 0 ) ? 2 : maxlen + 4;
string border( starNumber, '*' );
ret.push_back(border); // Top border
vector<string>::const_iterator iter;
for ( iter = v.begin(); iter != v.end(); ++iter )
ret.push_back("* " + *iter + string(maxlen - iter->size(), ' ') + " *");
ret.push_back(border); // Bottom border
return ret;
}