container for Objects like c++ vector?

Hey All,

In the avr version of c++, is there a way to have a list container to store Objects? Say I have and RGBLed library for controlling RGB leds declared like this:

RGBLed led1(redPin,greenPin,bluePin);

I want to have a list of these. In regular c++ I would use a vector declared like this:

vector leds;

…then I could iterate though them and select certain elements in the list, etc.

Can I do something like this with avr c++ or Arduino library?

You'll need to use an array. Remember the memory constraint when creating arrays of object.

//change class for your class in change the initialization list
class arr[] = {
  class(1,2),
  class(1,3)
};

I realise this may be too late to help the original poster, but since searching for ‘vector’ (as I did) brings you here, I’ll post this for others:

It’s relatively simple to write your own Vector class if you need basic functionality (no iterators, removing items once added etc.). The following class allows you to define your own Vector, push_back() objects and recall them with the notation.

// Minimal class to replace std::vector
template<typename Data>
class Vector {
   size_t d_size; // Stores no. of actually stored objects
   size_t d_capacity; // Stores allocated capacity
   Data *d_data; // Stores data
   public:
     Vector() : d_size(0), d_capacity(0), d_data(0) {}; // Default constructor
     Vector(Vector const &other) : d_size(other.d_size), d_capacity(other.d_capacity), d_data(0) { d_data = (Data *)malloc(d_capacity*sizeof(Data)); memcpy(d_data, other.d_data, d_size*sizeof(Data)); }; // Copy constuctor
     ~Vector() { free(d_data); }; // Destructor
     Vector &operator=(Vector const &other) { free(d_data); d_size = other.d_size; d_capacity = other.d_capacity; d_data = (Data *)malloc(d_capacity*sizeof(Data)); memcpy(d_data, other.d_data, d_size*sizeof(Data)); return *this; }; // Needed for memory management
     void push_back(Data const &x) { if (d_capacity == d_size) resize(); d_data[d_size++] = x; }; // Adds new value. If needed, allocates more space
     size_t size() const { return d_size; }; // Size getter
     Data const &operator[](size_t idx) const { return d_data[idx]; }; // Const getter
     Data &operator[](size_t idx) { return d_data[idx]; }; // Changeable getter
   private:
     void resize() { d_capacity = d_capacity ? d_capacity*2 : 1; Data *newdata = (Data *)malloc(d_capacity*sizeof(Data)); memcpy(newdata, d_data, d_size * sizeof(Data)); free(d_data); d_data = newdata; };// Allocates double the old space
};

Note that due to memory constraints it would be unwise to store large objects. Also note that due to differences between free and delete, the destructor of objects stored in a Vector might not be called. Be careful when storing objects which contain allocated memory.

An example of usage is given below. This loops through the numbers 2 to 100, storing all prime numbers in the Vector “prime” and meanwhile having the led-light (on the Uno) blink them all out (so first it blinks twice, then 3 times, etc).

// Example of Vector usage: store prime numbers and blink LED
void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
}

void blink(size_t n, size_t d = 1000) {
  for (size_t idx = 0; idx < n; ++idx) {
    digitalWrite(13, HIGH);
    delay(d/2);
    digitalWrite(13, LOW);
    delay(d/2);
  }
}

void loop() {
  Vector<size_t> prime;
  for (size_t num = 2; num < 100; ++num) {
    boolean isPrime = true;
    for (size_t idx = 0; idx < prime.size(); ++idx)
      if (!(num % prime[idx])) {
        isPrime = false;
        break;
      }
    if (!isPrime)
      continue;
    prime.push_back(num);
    blink(num);
    delay(1000);
  }
}

Edit: Added memory management for copy constructor and assignment-operator. We wouldn’t want that to fail…