C++ how to initialize vector member from list of values

  • A+

I am trying to initialize a vector member variable with an array of integers:

#include <vector> #include <iostream>  struct A {     A(int arr[]) : mvec(arr)     { }     std::vector<int> mvec; };  int main() {     A s({1,2,3}); } 

Compilation gives me error :

$ c++ -std=c++11 try59.cpp  try59.cpp:15:12: note: candidates are: try59.cpp:6:1: note: A::A(int*)  A(int arr[]) : mvec(arr) 

How can I initialize my vector using an array of integers?


I would just use a std::initializer_list since that's what you're already passing

A(std::initializer_list<int> arr) : mvec(arr) {  } 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: