How do you assign all elements of an array at once?
Asked Answered
W

5

93

When you initialize an array, you can assign multiple values to it in one spot:

int array [] = {1,3,34,5,6};

... but what if the array is already initialized and I want to completely replace the values of the elements in that array in one line:

int array [] = {1,3,34,5,6};
array [] = {34,2,4,5,6};

This doesn't seem to work. Is there a way to do so?

Woodford answered 20/4, 2011 at 15:25 Comment(0)
M
85

There is a difference between initialization and assignment. What you want to do is not initialization, but assignment. But such assignment to array is not possible in C++.

Here is what you can do:

#include <algorithm>

int array [] = {1,3,34,5,6};
int newarr [] = {34,2,4,5,6};

std::ranges::copy(newarr, array); // C++20
// or
std::copy(std::begin(newarr), std::end(newarr), std::begin(array)); // C++11
// or
std::copy(newarr, newarr + 5, array); // C++03

In C++11, you can also do this:

std::vector<int> array = {1,3,34,5,6};
array = {34,2,4,5,6};

Of course, if you choose to use std::vector instead of raw array.

Meshwork answered 20/4, 2011 at 15:29 Comment(4)
std::copy(std::begin(newarr), std::end(newarr), std::begin(array)); would be better, wouldn't it?Fleck
@MattCruikshank: Yes. But that wasn't possible in C++03.Meshwork
Better to use std::array instead of std::vector when it's fixed size.Rhinal
I am looking for a pointer to the Standard about this rule. Do you know where it is?Lymanlymann
S
10

You have to replace the values one by one such as in a for-loop or copying another array over another such as using memcpy(..) or std::copy

e.g.

for (int i = 0; i < arrayLength; i++) {
    array[i] = newValue[i];
}

Take care to ensure proper bounds-checking and any other checking that needs to occur to prevent an out of bounds problem.

Scrutiny answered 20/4, 2011 at 15:29 Comment(0)
S
5

I made a little template function to conveniently assign values to a raw pointer.

template <typename T, typename U>
void set_array(T* array, U x) {
  *array = x;
}

template <typename T, typename U, typename... V>
void set_array(T* array, U x, V... y) {
  *array = x;
  set_array(array + 1, y...);
}

An example is:

int main() {
  int64_t array[10] = {};
  set_array(array, 11, 12, 13, 14, 15, 16);
  for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i) {
    std::cout << array[i] << ", ";
  }
}

...and it should print:

11, 12, 13, 14, 15, 16, 0, 0, 0, 0,
Stumer answered 8/9, 2021 at 7:44 Comment(1)
Seems it could be made more safe if accepting not T* but array type.Daudet
S
2
const static int newvals[] = {34,2,4,5,6};

std::copy(newvals, newvals+sizeof(newvals)/sizeof(newvals[0]), array);
Sangsanger answered 20/4, 2011 at 15:30 Comment(0)
C
0

You could use a range based for loop like this:

int array[] = { 1, 3, 34, 5, 6 };

int count = 0;
for (int num : { 34, 2, 4, 5, 6 })
{
    array[count++] = num;
}
Cigarillo answered 18/4 at 12:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.