A vector of Elements
std::vector<Element> elements;
You can add Elements to it. Assuming Element has default constructor, you can use:
elements.push_back(Element());
You can get the first Element from elements using:
Element e = elements[0];
Pointer to a vector of Elements
std::vector<Element>* elementsPointer;
In order to add Elements to elementsPointer, is has to point to something valid.
elementsPointer = new std::vector<Element>; elementsPointer->push_back(Element());
You can get the first Element from elementsPointer using:
Element e = (*elementsPointer)[0];
or
Element e = elementsPointer->at(0);
A vector of pointers to Elements
std::vector<Element*> elementPointers;
You can add Element* to the vector.
elementPointers.push_back(new Element);
When you get the items from this vector, you get Element*s.
Element* ePtr = elementPointers[0];
Pointer to a vector of pointers to Elements
std::vector<Element*>* elementPointersPointer;
In order to add Element pointers to elementPointersPointer, is has to point to something valid.
elementPointersPointer = new std::vector<Element*>; elementPointersPointer->push_back(new Element);
You can get the first Element* from elementPointersPointer using:
Element* ePtr = (*elementPointersPointer)[0];
or
Element* ePtr = elementPointersPointer->at(0);
Using a reference
Using a reference to any of the above forms is just like using references to any other types in C++.
Use can use:
std::vector<Element> elements; std::vector<Element>& elementsRef = elements;
or any of the other variants.
std::vector<Element>will have a pointer internally, but you can safely ignore that.