How to implement an array without initialization value in C++? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
The uninitialized means that the array should firstly be created, but without any value initialized. It will gots values if and only if we do operations on the elements.
For example, given an array A[100]. I want to create another array B[100] with each element in it larger than A by 1, i.e. B[i] = A[i] + 1. But I don't want to initialize all the elements at the beginning. How can I implement such a structure/object?
To be more specific, the operation I want to avoid is the initialization of the elements in B because I need to change the values later. Instead, I want to assign the values directly by some functions/operations.
I think a struct is needed here but I am not familiar with the concepts of the details about how to implement the value assignment once each element got some operations on it.

Related

Vector Dummy Declaration [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
This post was edited and submitted for review 8 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
A Vectors in C++ are sequence containers representing arrays that can change in size. They use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays.
Can anyone explain to me, in easy layman language, what the meaning of vector<int> dummy1(rows,-1); is in C++? Specifically, what does dummy1(rows,-1) mean?
The code is declaring and constructing an object named dummy1 whose type is vector<int>, using constructor #3 on this reference page to initialize the object with rows number of elements that are all set to the value -1.
The first argument is the number of elements, the second argument is the value used to fill all of those elements. So it creates a vector with rows number of elements, all with the value -1

How to push an element to an array in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Let's say I have an array like:
{1,2,3}
So, for example, some function like:
arr.push(4);
Will make this array:
{4,1,2,3}
How do I do that?
It is not possible to push an element to an array. The size of an array remains the same through the lifetime of the array.
What can be done instead is to create a new, larger array and copy the elements from the old array. Such dynamic growable "array" data structure is provided for you in the standard library: std::vector.

Appending bits to increase the size of a char [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Is there any way I can append the size of a particular variable? For instance, if I have:
static_cast<char>(0x0147)
an 8 bit char and I want to increase it to say 16 bits without changing the data type, is that possible?
No.
The size of the object is not only related to its type; the size is defined by the type.
You cannot change one and not the other.
Just initialize a new int16_t from this char if that's what you want.
Or, you could have a vector<char> and add new elements to this collection as needed.
(Shifting has nothing to do with it; that's about transforming data.)

What is Default value of array on heap in c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
When I print the default value of array on heap memory I'm getting random big numbers in code block. I know that the default value of array is 0 but I am getting random number .
The doc of the std::array constructor says it:
initializes the array following the rules of aggregate initialization (note that default initialization may result in indeterminate values for non-class T)

What is Predecessor of an element in an array? How do I find it? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I tried to Google but in vain.
In C++ arrays are stored in contiguous memory.
This means that if you have an element x and you know that it's inside an array and that it's not the first element of the array, then then previous one is
*(&x - 1)
i.e. the element pointed by the address of x after we subtract 1 (note that this works because pointer arithmetic in C++ considers element size, so &x - 1 is not point to the byte before, but to the element before).