Vector Dummy Declaration [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 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

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.

What are some pros and cons for making a matrix with arrays vs vectors? [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 have been doing some research and cannot figure out which method/approach would be better and for what reasons. I want to create a simple empty matrix with a given length.
int n = 5;
int m[1...n][1...n];
int s[1...n-1][2...n];
I am not sure if it would be best to do this with vectors or arrays. Can the answers also include a code snippet?
NOTE: The above code snippet works and compiles in gcc -std=c++11, but with actual values in replace of the 1...n stuff
Arrays, like vectors, can store arbitrary objects except references(there are no type of reference in arrays and vectors)
Compared with vector, arrays has the following shortcomings:
The dimension of an array must be a constant expression, that is, it must be given at initialization. And will be not change in the whole process of running the program.
Arrays are not allowed to copy and assign, that is, the contents of arrays cannot be copied to other arrays as their initial values, but vectors can.
The process of using arrays is easy to go into array crossing, but vectors can use more mechanisms to control, such as iterators.

Find max element in each column in a 2-D 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 5 years ago.
Improve this question
For finding max element in i th row in a 2-D matrix[n][m], this is working
int t= *max_element(matrix[i],matrix[i]+m);
but I am not getting how to find max element in i th column in the same way.
The reason why std::max_element works for rows is quite simple: the algorithm accepts forward iterators to specify a range, so one might pass pointers as well: a pointer points to the first element in the row and a pointer points behind the last one. The elements in the row form a continuous block in memory, so the approach with pointers works well.
On the other hand, there is no way to use std::max_element for column elements in the same way as column elements do not form a continuous block in memory.
The most natural way will be just to write a simple cycle and do not use STL algorithms.

Identification possibility of sorting an array by deleting not more than one element [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 6 years ago.
Improve this question
Program gets a number of int arrays with size [2; 2000]. The question is: can that arrays get sorted after deleting not more than one element.
Examples:
2 16 3 3 - after deleting '16' it would be non-increasing array.
4 16 3 15 - there is no way to make it sorted.
Simple way: deliting first incorrect element and checking the fact of sorting. It takes too much time if there is a great number of arrays or that arrays are big-sizes.
Except cases, there incorrect element is first or last, and there array size is less than 4, in what way cheking that possibility can be accelerated
simply iterate through the array, and if you notice N_x > N_x+1 remove N_x, and mark that you removed 1 number...
in case you found another N_y > N_y+1 where y!=x, then it can't be sorted.
I'll leave the implementation for you, as it is your homework after-all.

How do we make our C++ programs fix array index out of range by themselves? [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 7 years ago.
Improve this question
I want to use some C++ code which is not written by me.
However, there are a lot of array index out of range in the code.
Moreover, the lengths of arrays may be not fixed; for example, they may be determined by the size of the input image.
I do not have enough budget to fix them manually, so I ask this question here.
I hope a[i] can be a[0] if i<0 and a[a.Length-1] if i>=a.Length, but I can keep the code a[i].
How do I make it?
You might want to try a wrapper class that is initialized with the original array and then uses an operator[] to behave as you need.
You could write a (templated) class that wrapped an array and overloaded the [] operator to perform bounds checked access to the underlying array. You could then use this class instead of normal C arrays.
How workable this will be will depend heavilly on how the application uses the array. If the array is a gloabl variable or part of a structure/class and is only ever accessed by [] then it will work great but if the array is passed arround by "degrading" to a pointer (and note that array parameters are really pointer parameters) then more work will be needed, changing parameter types and possiblly creating a seperate "checked array reference" class to be used in addition to your "checked array".