I don't understand this vector declaration statement [closed] - c++

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
Why LOG and 0 are used in this statement?
LOG = 35;
vector<int> cnt(LOG, 0); //here cnt is a vector name

The statement is constructing a std::vector, so you should look at help documentation for the vector constructor.
In this case, LOG and 0 are being used specifically in the two-parameter override of the constructor.
LOG is used to specify the initial size of the vector;
0 is used to specify the initial value of the elements.
In other words, the expression declares a vector named cnt being of size 35 with all elements initialized to 0.

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.

Same behaviour for size() vs .size() with string? [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 1 year ago.
Improve this question
Is there any difference in output behaviour between using size(x) instead of x.size(), where x is a string variable? Or it is just another alias?
The behavior of std::size is:
Returns the size of the given container c or array array.
1-2) Returns c.size(), converted to the return type if necessary.
...
So calling std::size(x) and x.size() has the same effect, where x is a std::string.
(This function can also accept an array, but that's not relevant when it comes to a std::string).

How to create an empty std::map object? [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
I tried this:
std::map<int,int> m;
and it works -- m becomes an empty map. But this approach may not work if the compiler choose to not initialize m to an empty map by default. Better solution?
Any better solution?
Taking your question literally, no. There is no better solution.
This will create a default constructed, and therefore empty std::map<int,int>.
std::map<int,int> m;

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.

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)