c++ library restrictions

Hi everyone, I am pretty new to arduino, and somewhat new to C++.

I am trying to create some libraries, and I am wondering if anyone can help me understand what c++ libraries are accessible.

For example, in a sketch, I tried to make a vector, a dynamic array, but the sketch would not allow me to do this.

Would it be possible to do this in a library that I use in a sketch?

I don't quite understand how this would work, any help would be appreciated.

Read this before posting a programming question

For example, in a sketch, I tried to make a vector ...

Post this sketch.

NJavrGuy:
For example, in a sketch, I tried to make a vector, a dynamic array, but the sketch would not allow me to do this.

NJavrGuy, Are you trying to use vectors from the standard template library? Just fyi they are not there.

However someone did go through the trouble of porting them The Standard Template Library (STL) for AVR with C++ streams | Andys Workshop

OK, sure, this is not meant to do anything more than test out vectors in arduino ide.

I am getting an error message: 'vector' was not declared in this scope, and the highligted line is:

vector DynArrNums(1);

Thanks for your help!

#include <vector>

void setup()
{
  
        using namespace std;

        Serial.begin(9600);
  
        vector<int> DynArrNums(1);

	DynArrNums[0]=365;

	int aNewNumber=335;

	DynArrNums.push_back(aNewNumber);


        for(int i=0;i<DynArrNums.size();++i)
          {
            Serial.println(DynArrNums[i];
          }
}


void loop()
{
  
  
}

Denbo:

NJavrGuy:
For example, in a sketch, I tried to make a vector, a dynamic array, but the sketch would not allow me to do this.

NJavrGuy, Are you trying to use vectors from the standard template library? Just fyi they are not there.

However someone did go through the trouble of porting them The Standard Template Library (STL) for AVR with C++ streams | Andys Workshop

Yup, that is definitely the problem.

This is kind of what I am trying to understand, what libraries are in arduino, and which ones are not.

Interesting, I will check out the post, thanks.

NJavrGuy:
Yup, that is definitely the problem.

This is kind of what I am trying to understand, what libraries are in arduino, and which ones are not.

Interesting, I will check out the post, thanks.

Here is the list of libraries that are installed with the Arduino ide: Libraries - Arduino Reference

If you decide to download a library and install it yourself, follow these instructions: http://arduino.cc/en/Guide/Libraries

Also just FYI... when you include a file it normally has the .h extension. The code you posted just said #include but should have been #include <vector.h> had you indeed installed the library.

Denbo:

NJavrGuy:
Yup, that is definitely the problem.

This is kind of what I am trying to understand, what libraries are in arduino, and which ones are not.

Interesting, I will check out the post, thanks.

Here is the list of libraries that are installed with the Arduino ide: Libraries - Arduino Reference

If you decide to download a library and install it yourself, follow these instructions: http://arduino.cc/en/Guide/Libraries

Also just FYI... when you include a file it normally has the .h extension. The code you posted just said #include but should have been #include <vector.h> had you indeed installed the library.

Ok, thanks!

When I use vector in eclipse, I am able to just include vector, and not the h file, good to know.

Can any c++ library be brought into arduino? Or does something have to be done to do so?

NJavrGuy:
Can any c++ library be brought into arduino? Or does something have to be done to do so?

It really depends on the library. If you read the link I posted on the STL port you will see he had to do a bit of work to get things to work. Something simple might be a straight port while others might be quite impossible with the limited memory one must work with.

Oh and let me correct myself, normally libraries end with .h. I thought his port for the vector library had .h on the end but according to the sample code on his website I was mistaken.

Denbo:

NJavrGuy:
Can any c++ library be brought into arduino? Or does something have to be done to do so?

It really depends on the library. If you read the link I posted on the STL port you will see he had to do a bit of work to get things to work. Something simple might be a straight port while others might be quite impossible with the limited memory one must work with.

Oh and let me correct myself, normally libraries end with .h. I thought his port for the vector library had .h on the end but according to the sample code on his website I was mistaken.

Thanks Denbo, very helpful.

If, by "vector", you mean an array with dynamic length, then it is unlikely to be useful on the Arduino.
Dynamically allocated memory does not work very well on the arduino.

You'd be better advised to learn to do with it, and implement your project another way.

If you follow the link to Andy Brown's port of the STL, you can have that going fairly easily. You need to copy quite a few files as described on his page.

However as others are pointing out, the STL uses dynamic memory allocation, and you don't have a heap of free memory on the Arduino. Plus currently the free() function has a bug, which you can correct by following this link:

http://arduino.cc/forum/index.php/topic,145765

Personally I like the Standard Template Library (STL), but as I say, I would be using it with caution on this platform.

michinyon:
If, by "vector", you mean an array with dynamic length, then it is unlikely to be useful on the Arduino.
Dynamically allocated memory does not work very well on the arduino.

You'd be better advised to learn to do with it, and implement your project another way.

Thanks to both of you for the heads up.

I will definitely keep this in mind.