Re: vector of vectors
On 02/12/14, Frank wrote:
There could be a misconception on my side: I deliberately
stripped off everything I thought irrelevant, and just for
the purpose to save the time of anyone willing to help.
I've often seen things like that in programming examples,
so I did the same.
I had to fill in some stuff and ended up with the following example:
#include <vector>
#include <algorithm>
template< typename VEC2_ELEMENT // element type
>
class vec2 : public std::vector <std::vector<VEC2_ELEMENT> >
{
public:
// something here
int longestindex() {
int retval;
int maxSoFar = -1;
for (int i = 0; i < this->size(); ++i) {
int currentSize = this->operator[] (i).size ();
if (maxSoFar < currentSize) {
maxSoFar = currentSize;
retval = i;
}
}
return retval;
}
// longest returns the # of elements of the sub-vector
// with the most elements
int longest()
{
if (this->size() > 0)
{
// longestindex returns the index of the sub-vector
// with the most elements
int index = this->longestindex();
// return this->at(index).size(); // works
return (*this)[longestindex()].size(); // doesn't work
}
else
return -1;
}
} ;
int main () {
vec2<int> foo;
foo.push_back(std::vector<int> (3));
foo.push_back(std::vector<int> (8));
foo.push_back(std::vector<int> (5));
int retval = foo.longest();
}
I just took it as a simple coding exercise for the evening :-)
> Sorry, where are my manners...
> many thanks for your effort.
You're welcome.
Stuart