For example, I have a 10 elements array called 'R'.
I have use 3 of that elements. Can VC++ tell me that which elements are empty and the corresponding index numbers? I googled, but I could not find an answer. (maybe I don't have a good keywords). Please help thanks
you simply could reserve a special value that marks an element as unused. initialize your your array elements with that value and reset the element to that value when it's not used any longer. the way vc++ "tells" you that the elements are unused would then be via the debugger that shows you the reserved value for the unused elements or make a little wrapper class for the array with a function returning the index so there would be not as much oberhead as you have using a template class or stuff like that
Related
i have a class in which it's protected section i need to declare an array with unknown size (the size is given to the constructor as a parameter), so i looked around and found out that the best possible solution is to declare an array of pointers, each element points to an integer:
int* some_array_;
and simply in the constructor i'll use the "new" operator:
some_array_ = new int[size];
and it worked, my question is: can i declare an array in a class without defining the size? and if yes how do i do it, if not then why does it work for pointers and not for a normal array?
EDIT: i know vecotrs will solve the problem but i can't use them on my HW
You have to think about how this works from the compiler's perspective. A pointer uses a specific amount of space (usually 4 bytes) and you request more space with the new operator. But how much space does an empty array use? It can't be 0 bytes and the compiler has no way of knowing what space to allocate for an array without any elements and therefore it is not allowed.
You could always use a vector. To do this, add this line of code: #include <vector> at the top of your code, and then define the vector as follows:
vector<int> vectorName;
Keep in mind that vectors are not arrays and should not be treated as such. For example, in a loop, you would want to retrieve an element of a vector like this: vectorName.at(index) and not like this: vectorName[index]
Lets say that you have an integer array of size 2. So you have Array[0,1]
Arrays are continuous byte of memery, so if you declare one and then you want to add one or more elements to end of that array, the exact next position (in this case :at index 2(or the 3rd integer) ) has a high chance of being already allocated so in that case you just cant do it. A solution is to create a new array (in this case of 3 elements) , copy the initial array into the new and in the last position add the new integer. Obviously this has a high cost so we dont do it.
A solution to this problem in C++ is Vector and in Java are ArrayLists.
So I'm trying to create an empty Array that is the length of a table row. I know how to get the length of a row, but I haven't got a clue in how to make an array with a pre-defined length. The program i'm making is dynamic so the length of the array will vary depending on the table I'm accessing.
Does anyone know how?
You've said you want an empty array, so take a look at Array.zeroCreate<'T>.
From the documentation:
Creates an array where the entries are initially the default value
Unchecked.defaultof<'T>.
Example:
let arrayOfTenZeroes : int array = Array.zeroCreate 10
This page has a lot of useful information on F# arrays - have look through it, it should point you in the right direction.
As Panagiotis Kanavos has pointed out in comments, F# differs from a language like C# for array creation, so I will quote directly from the F# language reference article I've linked to above for clarity:
Several functions create arrays without requiring an existing array.
Array.empty creates a new array that does not contain any elements.
Array.create creates an array of a specified size and sets all the
elements to provided values. Array.init creates an array, given a
dimension and a function to generate the elements. Array.zeroCreate
creates an array in which all the elements are initialized to the zero
value for the array's type.
I have this very large array, called grid. When I declare the array as below, every value in the array should be set to 0 according to the array constructor for integers
int testGrid[226][118];
However when I iterate through the entire array, I seem to get 0s for the majority of the array, however towards the lower part of the array I get arbitrary trash. The solution it is seems is to iterate over the array and manually set each value to 0. Is there a better way to do this?
You could do:
int testGrid[226][118] = {};
which will initialize your entries to 0.
Please see this C answer, which may come in handy for C++ too.
By the way, since this is C++, consider using an std::array, or an std::vector.
I want to know upto what index my array is filled. I know one method in which I will maintain a temporary variable inside the loop and will keep it updating which will at last determine the size.
I want to know that beside this method is their any other way to do this task? Better be O(1)(if possible) or anything better than O(n).
There is no generic way to do that as all elements of an array always contain a value.
Couple common ways to handle that:
keep track of "valid" elements yourself as you suggested in the post.
have sentinel element that marks "missing" value and check each element for it - first element with such value will mark "end of filled array". For reference types you can use null, for other types sometimes there is specific value that rarely used and can be treated as "missing" - i.e. max value of integer types.
The second approach is the way C-style strings are implemented - it is array of characters up to 0 character - so you can always compute length of the string even if it is stored in longer array of chars.
will this do?
size_t size_of_array = sizeof(array)/sizeof(array[0]);
something like that , and do correct the syntax :)
I've searched around the internet, and I can't seem to find the answer to my solution (or I'm blind/dumb and just can't figure out how to do it). Part of one of my assignments is as follows:
Constructor – creates an empty 2xn dynamic array. Your dynamic array should start as 2x5 in size but can grow to any length. The default value for empty `elements is “empty” and 0. The class should also have a nextElement variable that keeps track of the next empty spot in the array and is increment each time a data element is added.
Is there a way to create a 2*5 array that will accept string in one of the dimensions and integers in the other?