Solved

# How do I declare a vector of vector<Animal> ?

Posted on 2005-04-20
Medium Priority
302 Views
Hi experts,

I have a struct

typedef struct{
// blah
}Animal ;

and I need several different Animal vectors ...
i.e.  vector<Animal> Dog, Cat, Pig ;

but I want Dog, Cat, Pig to be stored as element of a new vector ?

How do I declare and use such a vector ???

many thanks !
0
Question by:meow00
• 2

LVL 12

Assisted Solution

andrewjb earned 200 total points
ID: 13822896
Don't think it'll be very effficient - you'll be copying vectors around if you're not careful. But, just:

typedef struct ... Animal
typedef vector<Animal> uVecAnimal
typedef vector<uVecAnimal> uDblVecAnimal

uDblVecAnimal X;
0

LVL 86

Assisted Solution

jkr earned 400 total points
ID: 13824521
Are you thinking about something like

#include <vector>
#include <map>
using namespace std;

struct Animal {

enum Race {

Unknown,
Dog,
Cat,
Pig
};

virtual Race GetRace () const = 0;

//...

};

struct Dog : public Animal {

virtual Race GetRace () const { return Dog};

// ...

};

class Farm {

public:

void AddAnimal ( Animal& a) {

Animal::Race r = a.GetRace();
map<Animal::Race,vector<Animal> >::iterator i = m_mapAnimal2Vector.find(r);

if ( i == m_mapAnimal2Vector.end()) {

vector<Animal> v;

v.push_back(a);

m_mapAnimal2Vector.insert(map<Animal::Race,vector<Animal> >::value_type(a,v));

} else {

i->push_back(a);
}
}

vector<Animal>* GetAnimals ( Animal::Race r) {

map<Animal::Race,vector<Animal> >::iterator i = m_mapAnimal2Vector.find(r);

if ( i != m_mapAnimal2Vector.end()) {

return i;
}

return NULL;
}

protected:

map<Animal::Race,vector<Animal> > m_mapAnimal2Vector;
};

?
0

LVL 3

Expert Comment

ID: 13824585
vector<vector<Animal> >

note there MUST be a space between the last two "> >" or the compiler will interpret it as the shift operator >>
0

LVL 3

Accepted Solution

Darrylsh earned 600 total points
ID: 13824650
struct animal
{
...
};
vector <animal> dogs;
vector <animal> cats;
vector <animal> pigs;

vector <vector<animal> > AllAnimals;

AllAnimals.push_back(dogs);
AllAnimals.push_back(cats);
AllAnimals.push_back(pigs);
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month13 days, 20 hours left to enroll