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

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -