Arduino and the STL library (C++)

So, you are using Arduino and you wonder if you can use the Cpp STL library in your code.

The good news is: yes that’s possible!

Even if some feature will still be lacking, or some others will be limited, you will be able to use things like std::vector and std::string in your Arduino programs.

Let’s start!

How is it possible, since STL is not a part of Arduino?

If you check this post on What is the Arduino language, you’ll see that we clearly mention there is no way to use things like std::vector, std::string, or other features from the STL library.

The Arduino language is a subset of C/C++, with some object oriented programming functionalities. For example you can create classes in Arduino, however, you won’t be able to use exceptions, and the new/delete expressions.

So, how is it possible, if we previously said it was not?

Well, it started with the development of uClibc++. uClibc++ is a Cpp library created to be able to use the standard Cpp library in embedded systems.

And a few developers have also worked on porting this library to Arduino. The library we’ll use here is called StandardCPlusPlus. Basically it enables you to use the (“fake”) STL in Arduino, just by downloading and installing the library in your Arduino environment.

So, even if you can’t natively use the STL, you can get some of its features from an external library.

Important note: Using STL on your computer and on Arduino (which is an embedded system) is clearly not the same thing, so don’t expect everything to work the same! The flash memory and SRAM on Arduino are about a few KiloBytes, whereas on your computers we’re talking in Gigabytes. So you can imagine that some trade offs where made during the development of the library.

Install STL library for Arduino

Let’s install and use the StandardCPlusPlus library in your Arduino environment.

  1. First, go to the StandardCPlusPlus GitHub page, and click on “Clone or Download” (green button on the right) > “Download ZIP”.
  2. On your computer, go into your Arduino folder (I assume you have already installed Arduino). It may be located on Documents/Arduino, or a similar folder – unless you chose a custom path.
  3. Extract the library with 7-zip, winrar, or any other tool for working with archives. Place the new folder (which should be named StandardCPlusPlus-master) inside the libraries/ folder of your Arduino folder.

That’s it! Now, to test it, start the Arduino IDE, or restart it if it was already open when you installed the library. Create a very simple sketch with just one import line.

#include <StandardCplusplus.h>

void setup() {}

void loop() {}

Click on “Verify” to compile your code. If you get some errors at this point, it means your installation was not done correctly. It may be that you didn’t place the StandardCPluslus-master/ folder in the correct place, or that you forgot to extract the archive. Make sure to follow all the steps and try again.

Also, I have made the tutorial with an Arduino Uno board. It should also work out of the box for other boards like Mega or Nano, but if you’re using boards like Due or Zero, you may encounter some issues.

Use the Cpp STL with Arduino

Not all of the Cpp STL features have been ported, and most of them are working a little bit differently under the hood. However, you should be able to use them without any problem or syntax difference, because the interface is really similar.

Let’s see an example for some Arduino STL features.

std::vector on Arduino

Here is a basic example on how to use std::vector in your Arduino sketch, to get a dynamic array of almost any data type.

#include <StandardCplusplus.h>
#include <vector>

std::vector<int> numbers;

void setup() 
{
  Serial.begin(9600);
  
  numbers.push_back(7);
  numbers.push_back(42);
  numbers.push_back(15);

  for (int i = 0; i < numbers.size(); i++)
  {
    Serial.println(numbers.at(i));
  }

  numbers.clear();

  Serial.print("Is the vector empty: ");
  Serial.println(numbers.empty());
}

void loop() {}

If you’re already familiar with std::vector, you can see that the way to use it is the same as if you program on your computer (but keep in mind that the implementation behind the hood is certainly not identical).

In this example, first we declare a vector, then we fill it with values, and print them. After that we clear the vector and check if it’s empty. Try this example on your Arduino and watch the result by opening the Serial Monitor:

7
42
15
Is the vector empty: 1

Warning: as you can guess, due to limited space, you won’t be able to create huge arrays and vectors on Arduino. Keep that in mind before trying to create a 10 000 double vector.

Adding an std::iterator

Let’s rewrite the for loop in the previous example, this time using an std::iterator. Also we’ll initialize the vector directly instead of using the push_back() function.

#include <StandardCplusplus.h>
#include <vector>
#include <iterator>

std::vector<int> numbers {7, 42 ,15};

void setup() 
{
  Serial.begin(9600);
  
  std::vector<int>::iterator it;

  for (it = numbers.begin(); it < numbers.end(); it++)
  {
    Serial.println(*it);
  }
}

void loop() {}

Great, iterators work too!

std::string on Arduino

Let’s start with a simple std::string example.

#include <StandardCplusplus.h>
#include <string>

std::string message = "Hello Arduino";

void setup() 
{
  Serial.begin(9600);

  Serial.println(message.c_str());
  Serial.print("Length: ");
  Serial.println(message.length());  

  std::string subString = message.substr(6,5);
  Serial.println(subString.c_str());
}

void loop() {}

Again, nothing too different. Here’s the output you should see on the Serial Monitor:

Hello Arduino
Length: 13
Ardui

Note: to print the std::string with Serial.println(), you have to use the C representation of the string with the c_str() function. This is similar to when you want to use printf().

Also, do not confuse std::string with the Arduino String object, those two are completely different.

An std::vector of std::string

Let’s continue our previous code on std::vector, but this time using a collection of std::string.

#include <StandardCplusplus.h>
#include <vector>
#include <iterator>
#include <string>

std::vector<std::string> animals {"pig", "dog", "sheep"};

void setup() 
{
  Serial.begin(9600);

  std::vector<std::string>::iterator it;

  for (it = animals.begin(); it < animals.end(); it++)
  {
    Serial.println((*it).c_str());
  }
}

void loop() {}

And the output:

pig
dog
sheep

std::cout on Arduino

Let’s say “Hello” using cout.

#include <StandardCplusplus.h>
#include <serstream>

std::ohserialstream cout(Serial);

void setup() 
{
  Serial.begin(9600);

  cout << "Hello" << std::endl;
}

void loop() {}

First, you have to include “serstream”. On your computer you would have used “iostream”.

Before you use std::cout, you have to tell the Arduino which hardware serial interface it needs to use. Here we use “Serial”, for the USB Serial interface. Depending on your application, this could be “Serial1”, for example, or any other HardwareSerial you want to use.

Let’s get back to our example with vectors, iterators, and strings, and replace Serial.print() by std::cout.

#include <StandardCplusplus.h>
#include <vector>
#include <iterator>
#include <string>
#include <serstream>

std::ohserialstream cout(Serial);

std::vector<std::string> animals {"pig", "dog", "sheep"};

void setup() 
{
  Serial.begin(9600);

  std::vector<std::string>::iterator it;

  for (it = animals.begin(); it < animals.end(); it++)
  {
    cout << *it << std::endl;
  }
}

void loop() {}

More features from the STL

In this tutorial you have seen how to install and use an external library – StandardCPlusPlus, based on uClibc++ – to get some STL features into your Arduino code.

You have experimented with vector, iterator, string, cout. Those are very common in any Cpp code using the STL.

There are many more useful STL features you can use with this Arduino library:

  • bitset
  • queue
  • map
  • set
  • map

It’s simple: if you want to use a certain element of the STL, search on Google to find how to use it, and try it directly on Arduino.

If you want to find out exactly what you can get or not in StandardCPlusPlus, check out the library GitHub page. If you have a doubt about an STL element, or a function inside an element, this might help you.

Leave a Comment