How to initialize std::vector from C-style array?

Solution 1:

Don't forget that you can treat pointers as iterators:

w_.assign(w, w + len);

Solution 2:

You use the word initialize so it's unclear if this is one-time assignment or can happen multiple times.

If you just need a one time initialization, you can put it in the constructor and use the two iterator vector constructor:

Foo::Foo(double* w, int len) : w_(w, w + len) { }

Otherwise use assign as previously suggested:

void set_data(double* w, int len)
{
    w_.assign(w, w + len);
}