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










4















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?










share|improve this question



















  • 10





    1,2,3 is not an array, it's an initialization list. int arr is not an array, it's a pointer to int

    – Piotr Skotnicki
    Nov 14 '18 at 13:13







  • 1





    Where is the size of your array?

    – Matthieu Brucher
    Nov 14 '18 at 13:14






  • 1





    How can we initialize a vector using initialization list? The vector needs to be initialized by the initialization list passed as aruments

    – Programmer
    Nov 14 '18 at 13:16







  • 2





    Can you clarify in your question if you need to initialize the vector from an array or from an initialization_list?

    – Galik
    Nov 14 '18 at 13:17







  • 1





    Sorry I was unaware of initialization_list - I was assuming it to be an array

    – Programmer
    Nov 14 '18 at 13:19















4















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?










share|improve this question



















  • 10





    1,2,3 is not an array, it's an initialization list. int arr is not an array, it's a pointer to int

    – Piotr Skotnicki
    Nov 14 '18 at 13:13







  • 1





    Where is the size of your array?

    – Matthieu Brucher
    Nov 14 '18 at 13:14






  • 1





    How can we initialize a vector using initialization list? The vector needs to be initialized by the initialization list passed as aruments

    – Programmer
    Nov 14 '18 at 13:16







  • 2





    Can you clarify in your question if you need to initialize the vector from an array or from an initialization_list?

    – Galik
    Nov 14 '18 at 13:17







  • 1





    Sorry I was unaware of initialization_list - I was assuming it to be an array

    – Programmer
    Nov 14 '18 at 13:19













4












4








4








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?










share|improve this question
















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?







c++ c++11






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 14:36







Programmer

















asked Nov 14 '18 at 13:12









ProgrammerProgrammer

2,9531851103




2,9531851103







  • 10





    1,2,3 is not an array, it's an initialization list. int arr is not an array, it's a pointer to int

    – Piotr Skotnicki
    Nov 14 '18 at 13:13







  • 1





    Where is the size of your array?

    – Matthieu Brucher
    Nov 14 '18 at 13:14






  • 1





    How can we initialize a vector using initialization list? The vector needs to be initialized by the initialization list passed as aruments

    – Programmer
    Nov 14 '18 at 13:16







  • 2





    Can you clarify in your question if you need to initialize the vector from an array or from an initialization_list?

    – Galik
    Nov 14 '18 at 13:17







  • 1





    Sorry I was unaware of initialization_list - I was assuming it to be an array

    – Programmer
    Nov 14 '18 at 13:19












  • 10





    1,2,3 is not an array, it's an initialization list. int arr is not an array, it's a pointer to int

    – Piotr Skotnicki
    Nov 14 '18 at 13:13







  • 1





    Where is the size of your array?

    – Matthieu Brucher
    Nov 14 '18 at 13:14






  • 1





    How can we initialize a vector using initialization list? The vector needs to be initialized by the initialization list passed as aruments

    – Programmer
    Nov 14 '18 at 13:16







  • 2





    Can you clarify in your question if you need to initialize the vector from an array or from an initialization_list?

    – Galik
    Nov 14 '18 at 13:17







  • 1





    Sorry I was unaware of initialization_list - I was assuming it to be an array

    – Programmer
    Nov 14 '18 at 13:19







10




10





1,2,3 is not an array, it's an initialization list. int arr is not an array, it's a pointer to int

– Piotr Skotnicki
Nov 14 '18 at 13:13






1,2,3 is not an array, it's an initialization list. int arr is not an array, it's a pointer to int

– Piotr Skotnicki
Nov 14 '18 at 13:13





1




1





Where is the size of your array?

– Matthieu Brucher
Nov 14 '18 at 13:14





Where is the size of your array?

– Matthieu Brucher
Nov 14 '18 at 13:14




1




1





How can we initialize a vector using initialization list? The vector needs to be initialized by the initialization list passed as aruments

– Programmer
Nov 14 '18 at 13:16






How can we initialize a vector using initialization list? The vector needs to be initialized by the initialization list passed as aruments

– Programmer
Nov 14 '18 at 13:16





2




2





Can you clarify in your question if you need to initialize the vector from an array or from an initialization_list?

– Galik
Nov 14 '18 at 13:17






Can you clarify in your question if you need to initialize the vector from an array or from an initialization_list?

– Galik
Nov 14 '18 at 13:17





1




1





Sorry I was unaware of initialization_list - I was assuming it to be an array

– Programmer
Nov 14 '18 at 13:19





Sorry I was unaware of initialization_list - I was assuming it to be an array

– Programmer
Nov 14 '18 at 13:19












2 Answers
2






active

oldest

votes


















5














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



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








share|improve this answer




















  • 1





    Thanks it solves the issue - "by value" I assume it meant that remove the reference const arr?

    – Programmer
    Nov 14 '18 at 13:18






  • 1





    yes indeed, remove the const&

    – Matthieu Brucher
    Nov 14 '18 at 13:19


















4














If for some reason you really want to initialize a vector using a C-style array and not std::initializer_list, you can do it using additional level of indirection:



struct A 
template<std::size_t n>
A(const int (&arr)[n]) :
A(arr, std::make_index_sequence<n>)


template<std::size_t... is>
A(const int (&arr)[sizeof...(is)], std::index_sequence<is...>) :
mvecarr[is]...


std::vector<int> mvec;
;

A a(1, 2, 3);


Edit. As François Andrieux pointed in the comment, std::vector can be initialized using a pair of iterators, so the constructor simplifies to:



template<std::size_t n>
A(const int (&arr)[n]) : mvec(arr, arr + n)



But if you were initializing, e.g., std::array instead of std::vector, index_sequence trick seems to be unavoidable.






share|improve this answer




















  • 1





    @FrançoisAndrieux, very good point, thanks!

    – Evg
    Nov 14 '18 at 13:32






  • 1





    If you were initializing a std::array, may as well take a std::array as a parameter, no?

    – StoryTeller
    Nov 14 '18 at 13:34






  • 1





    @StoryTeller, sure. But OP took C-style array, and so did I.

    – Evg
    Nov 14 '18 at 13:36










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301064%2fc-how-to-initialize-vector-member-from-list-of-values%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














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



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








share|improve this answer




















  • 1





    Thanks it solves the issue - "by value" I assume it meant that remove the reference const arr?

    – Programmer
    Nov 14 '18 at 13:18






  • 1





    yes indeed, remove the const&

    – Matthieu Brucher
    Nov 14 '18 at 13:19















5














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



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








share|improve this answer




















  • 1





    Thanks it solves the issue - "by value" I assume it meant that remove the reference const arr?

    – Programmer
    Nov 14 '18 at 13:18






  • 1





    yes indeed, remove the const&

    – Matthieu Brucher
    Nov 14 '18 at 13:19













5












5








5







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



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








share|improve this answer















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



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









share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 13:19

























answered Nov 14 '18 at 13:15









CoryKramerCoryKramer

74.2k1188142




74.2k1188142







  • 1





    Thanks it solves the issue - "by value" I assume it meant that remove the reference const arr?

    – Programmer
    Nov 14 '18 at 13:18






  • 1





    yes indeed, remove the const&

    – Matthieu Brucher
    Nov 14 '18 at 13:19












  • 1





    Thanks it solves the issue - "by value" I assume it meant that remove the reference const arr?

    – Programmer
    Nov 14 '18 at 13:18






  • 1





    yes indeed, remove the const&

    – Matthieu Brucher
    Nov 14 '18 at 13:19







1




1





Thanks it solves the issue - "by value" I assume it meant that remove the reference const arr?

– Programmer
Nov 14 '18 at 13:18





Thanks it solves the issue - "by value" I assume it meant that remove the reference const arr?

– Programmer
Nov 14 '18 at 13:18




1




1





yes indeed, remove the const&

– Matthieu Brucher
Nov 14 '18 at 13:19





yes indeed, remove the const&

– Matthieu Brucher
Nov 14 '18 at 13:19













4














If for some reason you really want to initialize a vector using a C-style array and not std::initializer_list, you can do it using additional level of indirection:



struct A 
template<std::size_t n>
A(const int (&arr)[n]) :
A(arr, std::make_index_sequence<n>)


template<std::size_t... is>
A(const int (&arr)[sizeof...(is)], std::index_sequence<is...>) :
mvecarr[is]...


std::vector<int> mvec;
;

A a(1, 2, 3);


Edit. As François Andrieux pointed in the comment, std::vector can be initialized using a pair of iterators, so the constructor simplifies to:



template<std::size_t n>
A(const int (&arr)[n]) : mvec(arr, arr + n)



But if you were initializing, e.g., std::array instead of std::vector, index_sequence trick seems to be unavoidable.






share|improve this answer




















  • 1





    @FrançoisAndrieux, very good point, thanks!

    – Evg
    Nov 14 '18 at 13:32






  • 1





    If you were initializing a std::array, may as well take a std::array as a parameter, no?

    – StoryTeller
    Nov 14 '18 at 13:34






  • 1





    @StoryTeller, sure. But OP took C-style array, and so did I.

    – Evg
    Nov 14 '18 at 13:36















4














If for some reason you really want to initialize a vector using a C-style array and not std::initializer_list, you can do it using additional level of indirection:



struct A 
template<std::size_t n>
A(const int (&arr)[n]) :
A(arr, std::make_index_sequence<n>)


template<std::size_t... is>
A(const int (&arr)[sizeof...(is)], std::index_sequence<is...>) :
mvecarr[is]...


std::vector<int> mvec;
;

A a(1, 2, 3);


Edit. As François Andrieux pointed in the comment, std::vector can be initialized using a pair of iterators, so the constructor simplifies to:



template<std::size_t n>
A(const int (&arr)[n]) : mvec(arr, arr + n)



But if you were initializing, e.g., std::array instead of std::vector, index_sequence trick seems to be unavoidable.






share|improve this answer




















  • 1





    @FrançoisAndrieux, very good point, thanks!

    – Evg
    Nov 14 '18 at 13:32






  • 1





    If you were initializing a std::array, may as well take a std::array as a parameter, no?

    – StoryTeller
    Nov 14 '18 at 13:34






  • 1





    @StoryTeller, sure. But OP took C-style array, and so did I.

    – Evg
    Nov 14 '18 at 13:36













4












4








4







If for some reason you really want to initialize a vector using a C-style array and not std::initializer_list, you can do it using additional level of indirection:



struct A 
template<std::size_t n>
A(const int (&arr)[n]) :
A(arr, std::make_index_sequence<n>)


template<std::size_t... is>
A(const int (&arr)[sizeof...(is)], std::index_sequence<is...>) :
mvecarr[is]...


std::vector<int> mvec;
;

A a(1, 2, 3);


Edit. As François Andrieux pointed in the comment, std::vector can be initialized using a pair of iterators, so the constructor simplifies to:



template<std::size_t n>
A(const int (&arr)[n]) : mvec(arr, arr + n)



But if you were initializing, e.g., std::array instead of std::vector, index_sequence trick seems to be unavoidable.






share|improve this answer















If for some reason you really want to initialize a vector using a C-style array and not std::initializer_list, you can do it using additional level of indirection:



struct A 
template<std::size_t n>
A(const int (&arr)[n]) :
A(arr, std::make_index_sequence<n>)


template<std::size_t... is>
A(const int (&arr)[sizeof...(is)], std::index_sequence<is...>) :
mvecarr[is]...


std::vector<int> mvec;
;

A a(1, 2, 3);


Edit. As François Andrieux pointed in the comment, std::vector can be initialized using a pair of iterators, so the constructor simplifies to:



template<std::size_t n>
A(const int (&arr)[n]) : mvec(arr, arr + n)



But if you were initializing, e.g., std::array instead of std::vector, index_sequence trick seems to be unavoidable.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 13:32

























answered Nov 14 '18 at 13:25









EvgEvg

4,00721434




4,00721434







  • 1





    @FrançoisAndrieux, very good point, thanks!

    – Evg
    Nov 14 '18 at 13:32






  • 1





    If you were initializing a std::array, may as well take a std::array as a parameter, no?

    – StoryTeller
    Nov 14 '18 at 13:34






  • 1





    @StoryTeller, sure. But OP took C-style array, and so did I.

    – Evg
    Nov 14 '18 at 13:36












  • 1





    @FrançoisAndrieux, very good point, thanks!

    – Evg
    Nov 14 '18 at 13:32






  • 1





    If you were initializing a std::array, may as well take a std::array as a parameter, no?

    – StoryTeller
    Nov 14 '18 at 13:34






  • 1





    @StoryTeller, sure. But OP took C-style array, and so did I.

    – Evg
    Nov 14 '18 at 13:36







1




1





@FrançoisAndrieux, very good point, thanks!

– Evg
Nov 14 '18 at 13:32





@FrançoisAndrieux, very good point, thanks!

– Evg
Nov 14 '18 at 13:32




1




1





If you were initializing a std::array, may as well take a std::array as a parameter, no?

– StoryTeller
Nov 14 '18 at 13:34





If you were initializing a std::array, may as well take a std::array as a parameter, no?

– StoryTeller
Nov 14 '18 at 13:34




1




1





@StoryTeller, sure. But OP took C-style array, and so did I.

– Evg
Nov 14 '18 at 13:36





@StoryTeller, sure. But OP took C-style array, and so did I.

– Evg
Nov 14 '18 at 13:36

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53301064%2fc-how-to-initialize-vector-member-from-list-of-values%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto