1

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

4
  • I assume you mean "filled contiguously from the zeroth" index? You could initialize the array elements to a sentry value and then start at zero and count how many are filled. Why is keeping track in a temp variable not good enough? What are you actually trying to accomplish? Technically there are many, many, many ways to do it, though many are not efficient. Commented May 2, 2015 at 4:53
  • 1
    @i_am_jorf That will be a O(n) solution.Correct me if i am wrong Commented May 2, 2015 at 4:55
  • 1
    Yeah, so? You didn't specify how fast you want it. Your question is overly vague. Commented May 2, 2015 at 4:55
  • 1
    I should add that what you are suggesting is only possible with string arrays where they always have a null byte at the end to indicate the end of the string. For other arrays you need to make sure you keep track of the size or atleast keep track of the position to which it is filled, manually. This is your O(1) solution Commented May 2, 2015 at 4:56

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I think for now i will choose the naive way that is first one and after getting good in fundamentals i will try to implement the second way myself.Thank you :) and sorry for my bad english
you might add this to answer : en.cppreference.com/w/cpp/container/array/size
0

will this do?

size_t size_of_array = sizeof(array)/sizeof(array[0]);

something like that , and do correct the syntax :)

8 Comments

That tells you how big the array is, not how many elements have been "filled".
@hg_git I think it will tell the total size of array i.e empty+filled
@chotabheem , oh okay. You mean till which index values are filled and not which are empty?
@chotabheem , then sorry we can't do much. every array is filled with something ( either by you , or some garbage values). To track values you manually filled , you'll have to explicitly introduce another variable and increment it with each insertion.
@Smac89 correct , will fail on pointer types. use templates instead.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.