i dont really get the fields of the template std::vector, the first one specifies the type, for example a class, but i dont get the allocator, pointer and reference variables.
i will provide a simple example, i want to use a vector of class person, so i have to say to the template vector: hey, store me some person's, but i also have to tell the vector how to allocate that class. (1)How would a correct syntax be for that example?
some code to start with:
side note, in the person's constructor,c(2)should i use date*?
class date
{
public:
int m_nMonth;
int m_nDay;
int m_nYear;
int getMonth(return m_nMonth;)
int getDay(return m_nDay;)
int getYear(return m_nYear;)
void setDate(int month, int day, int year)
{
m_nMonth=month;
m_nDay=day;
m_nYear=year;
}
date()
{
setDate(0,0,0);
}
date(int month, int day, int year)
{
setDate(month,day,year);
}
void printDate()
{
printf("%d/%d/%d", m_nMonth, m_nDay, m_nYear);
}
}
class person
{
public:
int m_nID;
char m_sName[128];
char m_sSurname[128];
date m_cBirthDay;
void setName(const char* name)
{
strncpy(m_sName, name, 127);
m_sName[127]= '\0';
}
void setSurname(const char* surname)
{
strncpy(m_sSurname, surname, 127);
m_sName[127]= '\0';
}
void setBirthDay(date* bday);
{
m_cBirthDay.date(bday.getMonth(), bday.getDay(), bday.getYear)
}
person(int id, const char* name, const char* surname, date bday)/*should it be date* bday? why?*/
{
m_nID=id;
setName(name);
setSurname(surname);
setBirthDay(bday);
}
}
i suppose you have to declare a function inside class person, called allocator, that is whats passed as second argument when you call, (3)how would that allocator function be defined?:
vector <person*, /*some allocator call*/> ImAVector;
then, (4)how could i push something in the vector, (5)would this work?:
person* someperson;
someperson.person(/*arguments*/);
ImAVector.push_back(someperson);
(6)how could i retrieve the pointer to a particular instance? (7)can i use the iterator to do something like this?:
//(6)
person* = ImAVector[someIterator];
//and then (7)
ImAVector[someIterator].setName(someConstCharPointer);
string* something = person.getName();
also, (8)does the allocator have anything to do with the constructor? (9)its common procedure to fill your class's fields right after allocating the instance of the class? (10)can an allocator call a constructor?
And the last questions: (11)what is the reference field, the pointer field, and their const counterparts?
I know its a lot to write, but ive spent the last two days searching for examples without success, so it would be nice if someone shed some light into this rarely discussed topic, ive numerated all questions for an easy response.
I appreciate any answers :)