Re: Accelerated C++ Exercise 3-4 Solution Question
On Mar 31, 11:57 am, "bwai...@yahoo.com" <bwai...@yahoo.com> wrote:
I'm in the process of working my way through Accelerated C++, so far
I'm finding the book to be an excellent read. Thanks for the
suggestion in the FAQ.
Since I know that I will be working with string data in some programs
I want to write, I decided to tackle exercise 3-4, which asks to write
a program that will report the longest and shortest string that the
program receives.
My solution is below. The program works, but I'd like to hear
people's thoughts on it and hear about different approaches. How else
would you approach the problem below?
I have programmed in C before, and I'm trying to avoid using a C
solution below. I really want to learn what C++ has to offer and
utilize it.
Thanks,
Brian
Note: The book has not introduced functions yet, so I am not using any
in my solution. Also, the book hasn't introduced program
organization, so I have not organized my data away from the calcs and
function calls. And I'm not doing any function error checking below
either.
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using std::sort;
int
main() {
vector<string> sVec;
vector<int> iVec;
string x;
// sVec contains all the strings
while (cin >> x)
sVec.push_back(x);
// check vector string size
vector<string>::size_type vectorsize;
vectorsize = sVec.size();
// string::size_type stringsize;
for (unsigned int i = 0; i < vectorsize; ++i) {
iVec.push_back(sVec[i].size());
}
// check vector integer size
vector<int>::size_type iVectorsize;
iVectorsize = iVec.size();
sort(iVec.begin(), iVec.end());
cout << "min: " << iVec[0] << endl
<< "max: " << iVec[iVectorsize-1] << endl;
return 0;
}
I think I'd use
int main() {
string x;
if (! (cin>>x)) {
cout << "No strings seen << endl; // You fail if iVec is empty.
return 0;
}
string::size_type min, max;
min = max = x.size();
while (cin >> x) {
string::size_type size = x.size();
if (min > size) min = size;
if (max < size) max = size;
}
cout << "min: " << min << endl
<< "max: " << max << endl;
return 0;
}
The basic idea is that you don't need to store the input strings -
just the minimum and maximum length. Read the first string and use
that to set the longest and shortest length seen, then loop reducing
the min and increasing the max as required.
Other than that:
for (unsigned int i = 0; i < vectorsize; ++i) {
iVec.push_back(sVec[i].size());
}
In general, when iterating over a container, I would use:
for (string::const_iterator it = sVec.begin(); it != sVec.end(); +
+it ) {
iVec.push_back( it->size() );
}
Somebody will be along shortly to show you how to do this with one of
the standard algorithms and some complex piece of template programming
(or a separate function). I find these complicated and just stick to
the standard (for me) idiom above.
vector<int>::size_type iVectorsize;
iVectorsize = iVec.size();
Prefer initialization over assignment, and write this as:
vector<int>::size_type iVectorsize = iVec.size();
or (even better)
const vector<int>::size_type iVectorsize = iVec.size();
iVec[iVectorsize-1]
std::vector has a method back() which returns this. (It is an error
to call this on a zero size vector, but a zero size vector will break
your code too.)
If you are going to use vector<int>::size_type to define vectorsize,
it would be cleaner to use it for the definition of 'i'.
iVec should really be a vector of string::size_type rather than a
vector of int.
I wouldn't declare iVec until you are ready to initialize it.
.... but overall, pretty good.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]