Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8057263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:58:40+00:00 2026-06-05T08:58:40+00:00

How can I initialize an std::array from a range (as defined by a pair

  • 0

How can I initialize an std::array from a range (as defined by a pair of iterators)?

Something like this:

vector<T> v;
...
// I know v has exactly N elements (e.g. I just called v.resize(N))
// Now I want a initialized with those elements
array<T, N> a(???);  // what to put here?

I thought array would have a constructor taking a pair of iterators, so that I could do array<T, N> a(v.begin(), v.end()), but it appears to have no constructors at all!

I know I can copy the vector into the array, but I’d rather initialize the array with the vector contents directly, without default-constructing it first. How can I?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:58:43+00:00Added an answer on June 5, 2026 at 8:58 am

    With random access iterators, and assuming a certain size at compile-time, you can use a pack of indices to do so:

    template <std::size_t... Indices>
    struct indices {
        using next = indices<Indices..., sizeof...(Indices)>;
    };
    template <std::size_t N>
    struct build_indices {
        using type = typename build_indices<N-1>::type::next;
    };
    template <>
    struct build_indices<0> {
        using type = indices<>;
    };
    template <std::size_t N>
    using BuildIndices = typename build_indices<N>::type;
    
    template <typename Iterator>
    using ValueType = typename std::iterator_traits<Iterator>::value_type;
    
    // internal overload with indices tag
    template <std::size_t... I, typename RandomAccessIterator,
              typename Array = std::array<ValueType<RandomAccessIterator>, sizeof...(I)>>
    Array make_array(RandomAccessIterator first, indices<I...>) {
        return Array { { first[I]... } };
    }
    
    // externally visible interface
    template <std::size_t N, typename RandomAccessIterator>
    std::array<ValueType<RandomAccessIterator>, N>
    make_array(RandomAccessIterator first, RandomAccessIterator last) {
        // last is not relevant if we're assuming the size is N
        // I'll assert it is correct anyway
        assert(last - first == N); 
        return make_array(first, BuildIndices<N> {});
    }
    
    // usage
    auto a = make_array<N>(v.begin(), v.end());
    

    This assumes a compiler capable of eliding the intermediate copies. I think that assumption is not a big stretch.

    Actually, it can be done with input iterators as well, since the computation of each element in a braced-init-list is sequenced before the computation of the next element (§8.5.4/4).

    // internal overload with indices tag
    template <std::size_t... I, typename InputIterator,
              typename Array = std::array<ValueType<InputIterator>, sizeof...(I)>>
    Array make_array(InputIterator first, indices<I...>) {
        return Array { { (void(I), *first++)... } };
    }    
    

    Since *first++ doesn’t have any I in it, we need a dummy I to provoke the pack expansion. Comma operator to the rescue, with void() to silence warnings about lack of effects, and also preventing overloaded commas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can create an array and initialize it like this: int a[] = {10,
We can initialize a container deque by using standard input like this: deque<int> c((istream_iterator<int>(cin)),(istream_iterator<int>()));
Can we initialize Python objects with statement like this: a = b = c
I would like to know: how can i initialize class object with parameters? I've
How can I use a std::valarray to store/manipulate a 2D array? I'd like to
I would like to initialize boost::random::discrete_distribution with an std::vector<double> . My problem is that
In c# I can initialize a List at creation time like var list =
How do I know when my GUI is ready, so I can initialize my
Can I declare an int array, then initialize it with chars? I'm trying to
I'm trying to do something like: #include <iostream> #include <vector> #include <ctime> class Clickomania

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.