How to change the value of each elmement in vector using iterator in C++ -
i wrote following program initialize vector
of person
. id
of person
objects not change. confuses me. can give me explanation?
#include <iostream> #include <vector> #include <string> #include "person.h" using namespace std; int main() { vector<person>* pv = new vector<person>(5,person(0)); int = 0; for(person person : *pv) { person.id = i++; } for(person person : *pv) { cout<< person.id << endl; } return 0; }
with person.h
this
#include <string> class person { public: int id; std::string name; person(int d); };
in loop need iterate reference or assigning id of copy of person in vector, not actual person in vector.
for(person& person : *pv) { person.id = i++; }
Comments
Post a Comment