How can I access elements from myVector like i would do with arrays ( for(i = 0; i < n; i++) cout << v[i] << " "; )
My code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Month
{
public:
char *name;
int nr_days;
Month(char* c, int nr) : name(c), nr_days(nr){};
~Month() { /* free(name); */}
};
int main()
{
Month January("January", 31);
Month February("February", 28);
Month March("March", 31);
Month April("April", 30);
Month May("May", 31);
Month June("June", 30);
Month July("July", 31);
Month August("August", 31);
Month September("September", 30);
Month Octomber("Octomber", 31);
Month November("November", 30);
Month December("December", 31);
vector<Month> *myVect = new vector<Month>;
myVect->push_back(January);
myVect->push_back(February);
myVect->push_back(March);
myVect->push_back(April);
myVect->push_back(May);
myVect->push_back(June);
myVect->push_back(July);
myVect->push_back(August);
myVect->push_back(September);
myVect->push_back(Octomber);
myVect->push_back(November);
myVect->push_back(December);
for(vector<Month>::const_iterator i = myVect->begin(); i != myVect->end(); i++)
{
/*
Month myMonth = i;
cout << myMonth.name << " " << myMonth.nr_days << endl;
*/
}
free(myVect);
return 0;
}
I would want to be something like a foreach algorithm: foreach(Month in myVect) cout << ...
And another question: why it gives me a run-time error at the destructor if I uncomment my line?
Ok, there are a lot of problems here.
You declare
myVectas a pointer to a vector. This is unnecessary. One of the major benefits of using a vector is so that you don’t have to worry about memory management as thevectordoes it for you. You stack allocate the vector, but internally it heap allocates the memory used to store the items it contains.You never initialize the pointer. You are invoking undefined behavior as that pointer is not valid. To initialize a pointer you use
new. All you have is an invalid stack allocated pointer that does not point to avectoron the heap. EDIT: I just realized that thenewwas edited out, so you can disregard this one. Still, it shouldn’t be a pointer at all.You are using
freeto deallocate a C++ class (that you never allocated to begin with…). Don’t. This isn’t C, you usenewanddeleteto manage memory (when necessary!) in C++.freedoes not call destructors, it simply frees up a chunk of memory.deleteon the other hand does as it knows how to deal with complex C++ types. Never mixnew/deletewithmalloc/free.myVect->begin()returns aconst_iterator, not aT(i.e., in this case, not aMonthobject). Dereferencing the iterator via the*operator will yield the current iteration object, so:As an aside, if you are going to be looping over the vector often you may want to
typedefthe iterator to reduce verbosity, i.e.,Now you can write