Emplace an aggregate in std::vector

You have several issues with your code:

  • Emplace takes an iterator as insertion point, and then a list of values which serve as arguments to a constructor.

  • Your struct must have a constructor which takes the values you pass to emplace.

  • You only have 4 argument values in your code, but your Particle struct has 5 fields.

Try this code instead:

struct Particle {
    int id;
    double x;
    double y;
    double theta;
    double weight;

    Particle(int id, double x, double y, double theta, double weight) 
        : id(id), x(x), y(y), theta(theta), weight(weight)
    {
    }
};

Notice the constructor there. And then emplace, for instance in the beginning [just an example which is not inserting at the back (see below)]:

std::vector<Particle> particles;

auto num_particles = 1000;
for (int i = 0; i < num_particles; i++)
{
    particles.emplace(particles.begin(), i, 0.0, 0.0, 1.0, 0.0);
}

As others have noted, if you just want to insert without specifying a specific position in the vector, you can use emplace_back:

std::vector<Particle> particles;

auto num_particles = 1000;
for (int i = 0; i < num_particles; i++)
{
    particles.emplace_back(i, 0.0, 0.0, 1.0, 0.0);
}

This inserts the elements at the end of the vector.


std::vector::emplace expects an iterator as argument too, because it inserts the element before that iterator's position. If you just want to append elements to the vector, use emplace_back. Another problem is that the { i,0.0,0.0,1 } thing is an initializer list, not Particle. If you want to instantiate the Particle struct, then you need to tell the compiler so: Particle{ i, 0.0, 0.0, 1 }. edit: That's because emplace_back expects arguments to later construct the Particle struct, so your attempt won't work as the argument itself will be deduced as an initializer list.

On the other hand, std::vector::push_back's parameter in this case is of type Particle, so here you're able to pass that init-list, since constructing objects like that is called aggregate initialization (i.e. Particle p = {i, 0.0, 0.0, 1} is valid).

Final code:

for (int i = 0; i < num_particles; i++)
{
    particles.push_back({i, 0.0, 0.0, 1});
}

First, std::vector::emplace requires the first argument passed to be an iterator representing the position where the element should be inserted.

Secondly, even if you provide the position. Template types are not deduced for initializer_lists. See initializer_list and template type deduction. So, below will equally fail:

particles.emplace( particles.end(), {i, 0.0, 0.0, 1, 1});

Since there is no constructor that can take the initializer list, below will likewise fail:

particles.emplace( particles.end(), i, 0.0, 0.0, 1, 1);

You either use insert or push_back as in:

particles.insert( particles.end(), {i, 0.0, 0.0, 1, 1});
particles.push_back({i, 0.0, 0.0, 1, 1});

or emplace or push_back:

particles.emplace( particles.end(), Particles{i, 0.0, 0.0, 1, 1});
particles.emplace_back(Particles{i, 0.0, 0.0, 1, 1});

Tags:

C++