which one is correct to scan till the ubound of hte array? i know VB has UBound, but i cant seem to figure out how to do it in C++Code:int m[];
for(int i=0;i<=m[i];i++);
int i=0;
while(m[i] != '\n'){
...
i++;
}
thanks
-nabeel
Printable View
which one is correct to scan till the ubound of hte array? i know VB has UBound, but i cant seem to figure out how to do it in C++Code:int m[];
for(int i=0;i<=m[i];i++);
int i=0;
while(m[i] != '\n'){
...
i++;
}
thanks
-nabeel
Why not just keep track of the elements in the array via some counter... then the loop is not neccessary...
but i dont wanna have uneccessary variables.
and after you use 2 or 3 arrays, the counters get kinda hard and messy to keep track of
Neither is correct. To first will loop until it finds a value in the array that is greater then or equal to i, while the other just loops until it finds (int)'\n' in the array. You have to understand that C++ arrays are not VB arrays. If you create an array like:
you will never have more or less then 10 elements in that array.Code:int x[10]
If you need a dynamic array, use an STL vector (#include <vector>, std::vector<int> x;).
Z.
Arrays are C++ weak point (sometimes strong point) as they contain no information about their bounds, which undermines object orientation pretty much. Zaei's suggesting the vector as a implementation of a dynamical array, in VB dynamical arrays are declared without size, while in C++ you need your own implementation for the sake of flexibility.
You can obtain the size of (only) dynamically allocated memory with _msize() and so you could retrieve the size for any dynamical array with:
template<class T>int DynArraySize(T* a){return _msize(a)/sizeof( T);};
thanks :)
any tuts on using vectors?