C++ how to initialize vector member from list of values
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
|
show 2 more comments
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
10
1,2,3
is not an array, it's an initialization list.int arr
is not an array, it's a pointer toint
– 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
|
show 2 more comments
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
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
c++ c++11
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 toint
– 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
|
show 2 more comments
10
1,2,3
is not an array, it's an initialization list.int arr
is not an array, it's a pointer toint
– 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
|
show 2 more comments
2 Answers
2
active
oldest
votes
I would just use a std::initializer_list
since that's what you're already passing
A(std::initializer_list<int> arr) : mvec(arr)
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
add a comment |
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.
1
@FrançoisAndrieux, very good point, thanks!
– Evg
Nov 14 '18 at 13:32
1
If you were initializing astd::array
, may as well take astd::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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
I would just use a std::initializer_list
since that's what you're already passing
A(std::initializer_list<int> arr) : mvec(arr)
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
add a comment |
I would just use a std::initializer_list
since that's what you're already passing
A(std::initializer_list<int> arr) : mvec(arr)
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
add a comment |
I would just use a std::initializer_list
since that's what you're already passing
A(std::initializer_list<int> arr) : mvec(arr)
I would just use a std::initializer_list
since that's what you're already passing
A(std::initializer_list<int> arr) : mvec(arr)
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
add a comment |
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
add a comment |
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.
1
@FrançoisAndrieux, very good point, thanks!
– Evg
Nov 14 '18 at 13:32
1
If you were initializing astd::array
, may as well take astd::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
add a comment |
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.
1
@FrançoisAndrieux, very good point, thanks!
– Evg
Nov 14 '18 at 13:32
1
If you were initializing astd::array
, may as well take astd::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
add a comment |
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.
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.
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 astd::array
, may as well take astd::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
add a comment |
1
@FrançoisAndrieux, very good point, thanks!
– Evg
Nov 14 '18 at 13:32
1
If you were initializing astd::array
, may as well take astd::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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
10
1,2,3
is not an array, it's an initialization list.int arr
is not an array, it's a pointer toint
– 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