How to push an element to an array 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 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.

Related

How to implement an array without initialization value 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 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.

Finding indexed data-structure like `std::vector` (not array) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am looking for a data structure which can store values with index and also can be resized like a std::vector (but should be indexed so I can access it easily) is there any C++ standard library implementation of my problem?
What I am looking for is an array-type DS from which I can remove elements.
You can use std::deque which is also indexed sequence container like std::vector. It provids also std::deque::resize member function.
However, your requirement should be much more specific to suggest std::deque at first place than std::vector

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).

can 0-1 knapsack be implemented using 1D array? [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 9 years ago.
Improve this question
I found implementation using 2D array http://www.geeksforgeeks.org/dynamic-programming-set-10-0-1-knapsack-problem/. But how to implement it using 1D array and if it is not possible then why?
Note that each of values in the current row uses only elements of the current and previous rows. Hence you can implement the algorithm with an array K[2][W], which is the same as using an array K[2*W] with some additional trivial index calculations