title: How to create objects inside a loop which name depends on an int
body: <p>I have an structure named "Particle" and I want to create several objects whose names depends on an int.
  As I am inside a for loop the name is going to change as follows: part0, part1, part2.</p>
  
  <pre><code>for (int i = 0; i&lt;num_particles; i++)
  {
      //double sample_x, sample_y, sample_theta;
      string name = "part" + std::to_string(i);
      Particle name;
      name.id = i;
      name.x = dist_x(gen);
      name.y = dist_y(gen);
      name.theta = dist_theta(gen);
  
      cout &lt;&lt; "Sample" &lt;&lt; " " &lt;&lt; name.x &lt;&lt; " " &lt;&lt; name.y &lt;&lt; " " &lt;&lt; name.theta &lt;&lt; endl;
  
  }
  </code></pre>
  
  <p>As you can imagine this approach doesn't work, do you have any solution?</p>
  
  <p>I have updated my question, now this is my new approach:</p>
  
  <p>I have created a vector and an int "number of particles":</p>
  
  <pre><code>std::vector&lt;Particle&gt; particles;
  </code></pre>
  
  <p>And the function code: </p>
  
  <pre><code>void ParticleFilter::init(double x, double y, double theta, double std[]) {
  // TODO: Set the number of particles. Initialize all particles to first position (based on estimates of 
  //   x, y, theta and their uncertainties from GPS) and all weights to 1. 
  // Add random Gaussian noise to each particle.
  // NOTE: Consult particle_filter.h for more information about this method (and others in this file).
  
  
  default_random_engine gen;
  normal_distribution&lt;double&gt; dist_x(x, std[0]);
  normal_distribution&lt;double&gt; dist_y(y, std[1]);
  normal_distribution&lt;double&gt; dist_theta(theta, std[2]);
  
  //for (int i = 0; i&lt;num_particles; i++)
  //{
      //double sample_x, sample_y, sample_theta;
      //string name = "part";
      //+ std::to_string(i);
      //Particle particles;
      particles[num_particles].id =num_particles;
      particles[num_particles].x = dist_x(gen);
      particles[num_particles].y = dist_y(gen);
      particles[num_particles].theta = dist_theta(gen);
      num_particles++;
      cout &lt;&lt; "Sample" &lt;&lt; " " &lt;&lt; particles[num_particles].x  &lt;&lt; " " &lt;&lt; particles[num_particles].y  &lt;&lt; " " &lt;&lt; particles[num_particles].theta  &lt;&lt; endl;
  
  //}
  
  }
  </code></pre>
  
  <p>But it doesn't work yet, it outputs "Segmentation fault".</p>