0

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.

for (int i = 0; i<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 << "Sample" << " " << name.x << " " << name.y << " " << name.theta << endl;

}

As you can imagine this approach doesn't work, do you have any solution?

I have updated my question, now this is my new approach:

I have created a vector and an int "number of particles":

std::vector<Particle> particles;

And the function 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<double> dist_x(x, std[0]);
normal_distribution<double> dist_y(y, std[1]);
normal_distribution<double> dist_theta(theta, std[2]);

//for (int i = 0; i<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 << "Sample" << " " << particles[num_particles].x  << " " << particles[num_particles].y  << " " << particles[num_particles].theta  << endl;

//}

}

But it doesn't work yet, it outputs "Segmentation fault".

rachuism
  • 193
  • 2
  • 3
  • 9
  • 2
    *Why* do you need different names of the variable inside the loop? The variables defined within the loop are *local* inside that loop, and objects constructed in the loop will be destructed at the end of the current iteration. Perhaps you need to [get a couple of good books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and read more about ***scoping***? – Some programmer dude Sep 24 '17 at 14:52
  • Apart from the fact that you are reusing the variable name "name", for objects of type "string" and "Particle", there's no reason something like this shouldn't work. The fact that you intend to give "Particle" a name implies it has a "string" field, which you should set. – jwimberley Sep 24 '17 at 14:52
  • 3
    Despite of what already been said: if you ever need to make variables of form "_part0, part1, part2_" - it's a strong indication that you need an array, or a similar construct. – Algirdas Preidžius Sep 24 '17 at 14:55
  • Why is the "particle number" not simply a constructor argument (if it's important to a particle) or just a container index if it is not relevant to the particle, just to the managing code.? Why would you ever need variable names to contain the "`n`" of the particle? – Jesper Juhl Sep 24 '17 at 14:55
  • Use vector `std::vector items(num_particles)` (before loop) and inside loop access items[i] – Artemy Vysotsky Sep 24 '17 at 14:55
  • Oh, after reading @Someprogrammerdude is it that you want the actual C++ variable name to depend on this variable? In addition to being unnecessary as pointed out, it is something that is also in general impossible in C++, and most programming languages. This would require some advanced form of something called "reflection", which C++ does not have. – jwimberley Sep 24 '17 at 14:55

3 Answers3

1

you can use itoa() function of cstdlib simply in your code.

for (int i = 0; i<10; i++)
{ 
 char a[max];
 string pa="part_";
string name = pa + itoa(i,a,i+1) ;
cout << "Sample" << " " << name << endl;

}
}

Sample Output:

 Sample part_0
 Sample part_1
 Sample part_2
 Sample part_3
 Sample part_4
 Sample part_5
 Sample part_6
 Sample part_7
 Sample part_8
 Sample part_9
krpra
  • 466
  • 1
  • 4
  • 19
0

This construct exists in C++, it is called std::vector.

 // we want to have a bunch of variables of type Particle
 // all named particles[i] for i == 0,1,2....
 std::vector<Particle> particles;

 // create a new particle variable
 particles.emplace_back(x, y, theta);

 // print the variable number 42
 std::cout << particles[42];
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
-3

Why do you want to down the messy road of variable naming such as var0, var1, var2 and so on? I'd recommend creating an array or vector.

It's not clear from your code snippet that why you need to create variables with different names. Moreover, your code/usecase doesn't sit right with the concept of variable scoping.

  • 1
    This answer looks is half-comment, half-trying to answer. If you are posting an answer - post an answer, not ask for clarification, which is the job for the comments. Relevant read for you: [Why do I need 50 reputation to comment? What can I do instead?](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – Algirdas Preidžius Sep 24 '17 at 14:58