?
Solved

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

Posted on 2005-04-20
4
Medium Priority
?
302 Views
Last Modified: 2013-12-14
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
Comment
Question by:meow00
  • 2
4 Comments
 
LVL 12

Assisted Solution

by:andrewjb
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

by:jkr
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

by:Darrylsh
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

by:
Darrylsh earned 600 total points
ID: 13824650
Expanding on my last answer:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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++.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question