tags:

views:

122

answers:

7

I have an array like this:

int a[100]; 

I am filling only the first 4 elements in this array:

a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;

When I do sizeof(a)/sizeof(a[0]) it returns 100.

Is there a way I can get number of elements to which I have assinged a value and thus filtering out the remaining 96 unassigned elements?

thanks

+8  A: 

No. Nothing keeps track of that.

Matthew Flaschen
+7  A: 

No. Either use a data structure, such as Vector, that keeps track of length, or pre-fill the array with a value which cannot ever occur as a real value, and test for that value and end of array while looping from start.

Mitch Wheat
thanks Mitch, yes, I ended up using a vector.
cppb
A: 

Why not use an associative container like std::map in this case? It allows you to test wether it contains an entry with a given key using std::map::find():

std::map<size_t, int> myMap;
myMap[0] = 1;
// ...
bool contains = myMap.find(0) != myMap.end();
Georg Fritzsche
+1, information on std::map was very useful.
cppb
I don't see any +1 ;) Not that its important though. @cppb
Georg Fritzsche
+3  A: 
Matt Curtis
+1: While 'No' is a valid answer, using `std::vector` is far more useful!
Johnsyweb
Just being a pedant, but "All elements must be assigned to something" isn't strictly true. A POD-type will have no value, because it is uninitialized.
GMan
@GMan: you're right of course, I meant "all elements must have some value", I was just trying to state that in terms of the OP's question. But as I read recently elsewhere on Stack Overflow, we need to be pedantic if we're going to use this language without blowing everything up ;-)
Matt Curtis
There is one point of contention between the `vector` and a static array though. With a static array, you may use any index whereas with a vector using an index superior or equal to size is undefined behavior... so I would definitely recommend `at` to users. It's a shame that the operator is unsafe and the verbose version is safe, I would have preferred the latter since programmers are lazy :/
Matthieu M.
+1  A: 

Assuming you're a beginner (and thus not jumping into the STL), here's an example of what @Mitch is talking about:

char *Names[100] = {}; // zero init

Names[0] = "hello";
Names[1] = "world";

for (int n = 0; n < 100 && Names[n] != 0; ++n)
    if (!Names[n])
        break;

printf("# of entries: %d", n);

Nowadays you would only do this if had to keep memory usage to an absolute minimum.

egrunin
You can simplify the initialization and zeroing to `char *Names[100] = {};`
Matthew Flaschen
There's plenty of good reasons for beginners to start with the STL. Stroustrup has an essay about this on his website. Then again, there's plenty more good reasons for beginners to avoid C++ altogether :-)
Matt Curtis
A: 

In the message title you called your array "static". If this is indeed an array with static storage duration, then its elements are zero-initialized by default at program startup. Since the "assigned" values you used in your example are non-zero, you can determine how many elements you have assigned by finding the first zero element in the array. Of course, this will only work when the "assigned" values are guaranteed to be non-zero, i.e. if zero value can be thought of as a reserved value.

AndreyT
A: 

I think using boost::optional as array type can solve the problem:

boost::optional<int> a[100];

So, you can check if element is set like this:

if (! a[i]) {   // element not set
}
else {  // element not set
}

Using boost:array instead of bare static array may be good idea too.

dimba