hi,
I am searching for some workarounds for array[].length.
How do you solve this problems? I think there are 3 of them:
1.) cycling through an array
2.) calculating with the length (arithmectic mean)
3.) check if some item is the last item
I found this:
int length(int array[])
{
int len = sizeof(array)/sizeof(array[0])
return len;
}
But this will work just for one type of arrays. Is there a way to write a function which works for every array. If multidemensional, if string-aray or anything?
I like using arrays and in my (planned) project I would like to use a lot of them, but without being able to determin the last item of an array I loose its main advantage as a list.
I would like to program some kind of schedule (I know there are some around, but they do not meet my requirements).
The schedule should be read from a SD-Card into an array during setup. In the loop I am referring to the array. I need to cycle through the rows, performing some checks and checking the following row. While in last row the following row should be the first.
So I need a foreach-function and a check for the last row in the array.
I think I might need to find a way saving all array-lengths in a separate array. For the multi-dimensional arrays this will get a bit confusing...
Right. This could become a problem. But as I have to loop (and check) very quick (lets say ~100ms) I avoided some calculations by generating some tables. Maybe I should redo this and check how long a loop will take...
#include "vector.h"
#include <Streaming.h>
using namespace std;
void foo (vector<int> & z)
{
Serial << "items in array are " << z.size () << endl;
}
void setup () {
Serial.begin (115200);
vector<int> x (10);
for (byte a = 0; a < 10; a++)
x [a] = a;
for (vector<int>::const_iterator i = x.begin (); i != x.end(); i++)
Serial.println (*i);
Serial << "backwards!" << endl;
for (vector<int>::reverse_iterator i = x.rbegin (); i != x.rend(); i++)
Serial.println (*i);
foo (x);
x.resize (20);
foo (x);
}
void loop () {}
Output:
0
1
2
3
4
5
6
7
8
9
backwards!
9
8
7
6
5
4
3
2
1
0
items in array are 10
items in array are 20
Arrays can be dynamically sized. You can determine the size of an array in a called function. Arrays can be traversed forwards and backwards. You can find the first item. The last item. And much much more.
The nice thing about the STL is that the arrays (vectors) can be of any type. Adding to the previous example:
Serial << "bytes" << endl;
vector<byte> w (5);
for (byte a = 0; a < 5; a++)
w [a] = a * 2;
for (vector<byte>::const_iterator i = w.begin (); i != w.end(); i++)
Serial.println (*i, DEC);
I had looked at some length for an STL port that ran on the Arduino, without success. So thanks for that link. I was half-way through doing one myself.
However a quick test seems to show that his uses more program memory than what I have ported so far, so I might continue for a bit longer.
As for burning up memory, yes that is a concern. But if the application requires the array anyway, whether or not it is done with STL won't change much. In fact, without dynamic memory you tend to allocate a big array "just in case", and if you need a few, then you are allocating a lot of big arrays "just in case" and using a lot of memory anyway. The actual container takes 6 or 7 bytes of memory (to hold its pointers) so that is the only overhead over the data you need to have anyway.
Your link came out a bit strange (with quotes in it) so I'll re-post it: