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

Posted by dehmann on Stack Overflow See other posts from Stack Overflow or by dehmann
Published on 2010-03-12T16:37:08Z Indexed on 2010/03/12 16:47 UTC
Read the original article Hit count: 129

Filed under:
|
|
|
|

What is the cheapest way to initialize a std::vector from a C-style array?

Example: In the following class, I have a vector, but due to outside restrictions, the data will be passed in as C-style array:

class Foo {
  std::vector<double> w_;
public:
  void set_data(double* w, int len){
   // how to cheaply initialize the std::vector?
}

Obviously, I can call w_.resize() and then loop over the elements, or call std::copy(). Are there any better methods?

© Stack Overflow or respective owner

Related posts about c++

Related posts about c