define classes with vectors?

Feb 6, 2009 at 5:01am
is this possible or desirable ever? this is sort of an open question


for instance, if i had a sports statistics input file of the format like <team name> <wins> <losses>

and i read it into three different vectors, would it make any sense to define classes if i already have the info read and stored in vectors? under what circumstances would i want to do this

this might be a vague or dumb oop question, but the context would be to run different loops analyzing the data and outputting various teams / aggregate wins or losses based on other functions

would it be advantageous to use classes or not?
Feb 6, 2009 at 5:52am
I think it would make more sense to have a class called Team that stored those three values, and then a vector of Teams.

But anyway: yeah, it's possible and often necessary to write classes that contain vectors or even more complex structures.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct File{
	std::string name;
	Attributes attr;
};

//Note: My inheritance is rusty, so this may not make sense. If it doesn't, I'm
//probably doing it wrong.
struct Directory::File{
	std::vector<File *> contents;
};

struct DirectoryTree{
	Directory *root;
};
Feb 6, 2009 at 6:19am
thanks helios, wow i think all of that is over my head =/ i am pretty mediocre at oop so i generally working around it when i can (habit i am trying to break)

but if i understand you correctly, you are saying make a "team" class defining <team> <wins> <losses> as their types

then read the input file into a "team" vector...

if i ran a while loop for it, would i extract with

>> team_class_name

and it would grab the input row by row then?


i will do some research so i can hopefully understand your inheritance stuff




Feb 6, 2009 at 1:17pm
If you create a struct containing the data, you will not be able to use operator>> to read the struct unless you implement operator>> for the struct.

Here is an example. Note that operator>> is not the best way to read input though...

1
2
3
4
5
6
7
8
9
10
struct Foo {
   int x;
   int y;
   int z;

   template< typename charT, typename Traits > friend
   std::basic_ostream<charT, Traits>& operator>>(
      std::basic_ostream<charT, Traits>& os, Foo& f )
      { return os >> f.x >> f.y >> f.z; }
};
Topic archived. No new replies allowed.