Re: Accelerated C++ Exercise 3-4 Solution Question
On Mar 31, 5: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.
Good for you.
Note: The book has not introduced functions yet, so I am not using any
in my solution.
If you have coded in C then you are still allowed to use functions
in C++ :)
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
using std::sort;
a matter of style. Most would either just say using namespace std
or fully qualify each std function.
int
main() {
vector<string> sVec;
vector<int> iVec;
don't declare vars until you need them. Move iVec down until
after all the input occurs.
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();
I would use iterators to move through the vector
// string::size_type stringsize;
for (unsigned int i = 0; i < vectorsize; ++i) {
iVec.push_back(sVec[i].size());
}
for( vector<string>::iterator i=sVec.begin(); i!=sVec.end(); +
+i)
iVec.push_back(i->size());
a matter of style (I don't use {} for single lines
// 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;
Instead of indexing, I would have used iVec.front() and iVec.back()
to retrieve the first and last element of a vector.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]