C++ Array Assignment of Multiple Values

Assign multiple values to array in C

If you really to assign values (as opposed to initialize), you can do it like this:

 GLfloat coordinates[8]; 
static const GLfloat coordinates_defaults[8] = {1.0f, 0.0f, 1.0f ....};
...
memcpy(coordinates, coordinates_defaults, sizeof(coordinates_defaults));

return coordinates;

c++ array assignment of multiple values

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::copy(newarr, newarr + 5, array);

However, in C++0x, you can 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.

How to assign multiple constants to an array at once, not at the time of declaration?

I know that I can assign multiple constants at once to the array at the time of declaration as follows:

double[] MyArray = new double[3] {1d, 2d, 3d};

This code doesn't really assign all the values at once... it actually does the same thing as this:

double[] MyArray = new double[3];
MyArray[0] = 1d;
MyArray[1] = 2d;
MyArray[2] = 3d;

There's no specific syntax to assign multiple values to an existing array.

If you need to do it in a loop, you can do this:

double[] MyArray = new double[3];
for (int i = 0; i < MyArray.Length; i++)
{
MyArray[i] = i + 1;
}

C array declaration and assignment?

Simply put, arrays are not assignable. They are a "non-modifiable lvalue". This of course begs the question: why? Please refer to this question for more information:

Why does C++ support memberwise assignment of arrays within structs, but not generally?

Arrays are not pointers. x here does refer to an array, though in many circumstances this "decays" (is implicitly converted) to a pointer to its first element. Likewise, y too is the name of an array, not a pointer.

You can do array assignment within structs:

struct data {
int arr[10];
};

struct data x = {/* blah */};
struct data y;
y = x;

But you can't do it directly with arrays. Use memcpy.

How to assign multiple values to an array?

With C-style array? No, they are not reassignable.

With C++ std::array? Sure

#include <array>
int main()
{
std::array<int, 4> dest_linesize = {4 , 0 , 0 , 0 };
dest_linesize[0] = 5;
for(int n:dest_linesize) {
std::cout << n << " ";
}
std::cout << "\n";

dest_linesize = { 4 , 3 , 1 , 0 };
for(int n:dest_linesize) {
std::cout << n << " ";
}
}

Assign a multiple numbers to an array

If you want to initialize array with several values, you can use this:

int a[4] = {1, 2, 3, 4};

But it should not be used, if you have a lot of numbers. If you need to assign list of numbers, that you stated in question, you should describe them with mathmetical expression in cycle:

for(int i = 0; i < 203; i++)
{
a[i] = -1.5 + 0.5 * i;
}

If you want to assign array with list of values, that can't be described with mathematical expression, you should read them from file.

Assigning multiple value in an array after declaration

You can use standard class std::array instead. For example

#include <array>

//...

std::array<int, 9> values = { 1,2,3,4,5,6,7,8,9 };
values = { 10,20,30,40,50,60,700,800,900 };

Here is a demonstrative program

#include <iostream>
#include <array>

int main()
{
std::array<int, 9> values = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

for ( int x : values ) std::cout << x << ' ';
std::cout << std::endl;

values = { 10, 20, 30, 40, 50, 60, 700, 800, 900 };

for ( int x : values ) std::cout << x << ' ';
std::cout << std::endl;

return 0;
}

The output is

1 2 3 4 5 6 7 8 9 
10 20 30 40 50 60 700 800 900

Though I did no test the program in the CLR environment.

Simpler way to set multiple array slots to one value

This function will help make it less painful.

void initialize(int * arr, std::initializer_list<std::size_t> list, int value) {
for (auto i : list) {
arr[i] = value;
}
}

Call it like this.

initialize(array,{9,5,14},2);


Related Topics



Leave a reply



Submit