0

This is related to my other post. One of the suggestions here was to use vector for class Region.. as illustrated in the following code. I have a few more beginner questions --

a) How to allocate this vector a size = numOfRegions? Or do I really need to allocate a size to a vector?

b) How do I insert objects of class Region to this vector<Region>? All these Region objects need to be managed by class A

c) I am assuming I don't need to delete this vector in class A's distructor .. correct?

struct elemt {
  int* vertex; 
  int foo1;
  double foo2;
};


class Region{
   public:
      std::vector <elemt*> elements;   
      int info;
}



class A{
public:
    std::vector<Region> reg;
    const int numOfRegions = 100;
}
A::A(){
    // how to create and append Region object to vector std::vector<Region> reg ??
    // Do I need to allocate size for this region vector? 
}

A::~A(){
  //do nothing
 // Do I need to delete vector<Region> reg here ??
}

A::doSomething(){

// here I want to append the elements to the vector
// Let i be region 10. 
// Let e1 be an element of "struct elemt" that needs to be added

  reg[i].elements.push_back(e1);

}
Community
  • 1
  • 1
memC
  • 1,005
  • 2
  • 14
  • 23
  • 1
    You seem to be writing this program without a compiler. Why limit yourself like that? – Manuel Feb 09 '10 at 11:08
  • Also, you seem rather lost with respect to the STL and C++ in general. Maybe you should study some basic material before actually coding anything. See this, for example: http://www.cplusplus.com/reference/stl/vector/. C++ has a steep learning curve, you will not get too far if you don't grok the basics. – Manuel Feb 09 '10 at 11:23
  • I agree.. I am parallely reading material. there is a lot to learn. btw, could you pls explain what user f4 meant in the comment below? (see my reply) – memC Feb 09 '10 at 11:48

2 Answers2

2
  1. Call vector.reserve if you know the size of your vector up front. This isn't required because push_back will resize the vector when needed

  2. vector.push_back(Region());

  3. You don't have to delete member vector

Nikola Smiljanić
  • 26,745
  • 6
  • 48
  • 60
1

a) the vector does all the memory management for you, you don't need to specify a size. However you can, if you want, call vector::reserve to allocate enough memory (this is only a matter of optimisation)

b) reg.push_back(Region());

c) you don't have to delete the vector as it is in the stack. But you probably want to delete the contents of the vector elements in the class Region.

f4.
  • 3,814
  • 1
  • 23
  • 30
  • @f4: Thanks... could you please illustrate point c? Do you mean I should do it in destructor of class Region ... an example would be really helpfu! ... also, the vector elements just has "pointers to "elem" instances created elsewhere. Do I still need to delete contents of vector elements in the class Region? – memC Feb 09 '10 at 11:36
  • 2
    He means that somebody must take care of deleting the `Element` objects, but you said in your other question that this was responsibility of some other class, so I think you'll be fine without doing anything in the destructor of `Region`. – Manuel Feb 09 '10 at 11:56
  • Ok I didn't read the other post entirely. If you have an other class taking care of deleting the Elements it's ok. Just keep in mind the vector won't do it for you :) – f4. Feb 09 '10 at 13:22