1

I have a language built-in array and I need to copy its elements to the container library array for some processing. I have tried couple of things but it doesn't seem to work. Is there any way to convert one type to another?

A language built-in array is declared as:

int arr[] = {1,12,343,54,99};

While the library container array is declared as:

std::array<int,4> myarray = {4, 26, 80, 14} ;

std::array is declared under header <array>.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Pankaj Mishra
  • 550
  • 6
  • 18
  • 1
    Can you elaborate a bit? Do you want to copy the values from a `int[]` to a `std::array`? In your example are `arr` and `myarray` related in any way? – Cory Kramer Dec 12 '19 at 11:56
  • I think this [post](https://stackoverflow.com/questions/26219721/assign-c-array-to-cs-stdarray-stdarrayt-u-tu-no-suitable-const) answers your question. – Hawky Dec 12 '19 at 11:58
  • @CoryKramer Sorry, I made is more clear. Please check. – Pankaj Mishra Dec 12 '19 at 11:59
  • I’ve never heard the term “container library array” in the context of C++. Just say `std::array`. Same, to some extent, for “built-in array” vs the more common “C-style array”. – Konrad Rudolph Dec 12 '19 at 12:01
  • @KonradRudolph I got this term from here(http://www.cplusplus.com/doc/tutorial/arrays/), scroll down till last. Thanks. – Pankaj Mishra Dec 12 '19 at 12:02
  • @DominikJastrzębski Yes. That exactly what I wanted. I shall write an answer myself after understanding the linked post. Thanks. – Pankaj Mishra Dec 12 '19 at 12:04
  • 1
    @PankajMishra Ah. cplusplus.com is a bit rubbish unfortunately. Googling for the term shows that they basically invented the term, it’s not used anywhere else. – Konrad Rudolph Dec 12 '19 at 12:05
  • @KonradRudolph Then the domain name is pretty delusional. It seems the most genuine one. – Pankaj Mishra Dec 12 '19 at 12:06
  • 1
    @PankajMishra It’s definitely *not* the most “genuine” one. Anybody can register domain names, and cplusplus.com simply happened to be the first to grab it. They are in no way authoritative, and most experts tend to avoid the site. The “official” website is https://isocpp.org/, and a pretty good reference is https://cppreference.com/. – Konrad Rudolph Dec 12 '19 at 12:07
  • 1
    cppreference.com is broadly considered the "official unofficial reference" for C++. – Sneftel Dec 12 '19 at 12:09
  • @KonradRudolph Thanks. This is a new info for me. – Pankaj Mishra Dec 12 '19 at 12:09

1 Answers1

9

For C++20, this is what std::to_array is for.

int arr[] = {1,12,343,54};
std::array<int,4> myarray = std::to_array(arr); // could use "auto myarray" instead

Prior to C++20, you'd use std::copy, or std::move if your element type is noncopyable or costly to copy:

int arr[] = {1,12,343,54};
std::array<int, 4> myarray;
std::copy(arr, arr+4, myarray.begin());
// or
std::copy(std::begin(arr), std::end(arr), myarray.begin());
Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • This does answer my question. Thanks. – Pankaj Mishra Dec 12 '19 at 12:16
  • 2
    I don't think the implementation of `to_array` uses anything C++20 specific, so @PankajMishra can just copy the implementation from [cppref](https://en.cppreference.com/w/cpp/container/array/to_array). Also note, that while `std::copy` is perfectly fine for `int` there are types for which you probably would want to use [`std::move`](https://en.cppreference.com/w/cpp/algorithm/move) (not the cast, but the algorithm). – n314159 Dec 12 '19 at 12:24
  • Yay for c++ 20! – Gaspa79 Jan 08 '20 at 13:06