In this post we are going to learn how to apply the cpplinq except operator, to obtain only the elements of that array that are not contained in a second array. The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The tests on the ESP8266 were performed on a DFRobot’s ESP8266 FireBeetle board.
Introduction
In this post we are going to learn how to apply the cpplinq except operator, to obtain only the elements of that array that are not contained in a second array.
In other words, we will obtain all the elements of the first array except the ones that are on the intersection with the second array.
The code shown below can be used both on the ESP32 and on the ESP8266 since cpplinq is a generic C++ library. This means that it can even be used outside the scope of microcontroller programming.
In our case, we will be using the Arduino core on both devices.
The tests on the ESP32 were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The tests on the ESP8266 were performed on a DFRobot’s ESP8266 FireBeetle board.
The code
We will start the code by including the cpplinq.h library and then declaring the use of the cpplinq namespace, like we have been doing on the previous tutorials.
#include "cpplinq.hpp"
using namespace cpplinq;
Then we will move on to Arduino setup function, where we will write the rest of the code. As usual, we start by opening a serial connection, so we can output the results of our program.
Serial.begin(115200);
Then we will define two arrays of integers, with some overlapping elements.
int array1[] = {1,2,3,4};
int array2[] = {3,4,5,6};
After this we will apply the except operator to obtain all the elements of array1, except the ones that are also contained on array2.
To be able to use cpplinq operators, we first need to convert array1 to a range object, by calling the from_array operator.
from_array(array1)
After this we can apply the except operator. This operator receives as input the range that we want to use to exclude elements from our original array.
So, we will convert array2 to a range object, also with a call to the from_array function, and then pass it as input of the except operator.
except(from_array(array2))
The result of this call should be a new range that contains only the elements of array1 that do not exist in array2.
Finally we need to convert the range to a type we can iterate. We will convert it to a C++ vector with a call to the to_vector operator.
to_vector()
The full expression tree can be seen below. Recall from previous tutorials that cpplinq operators are chained with the >> operator.
auto result = from_array(array1)
>> except(from_array(array2))
>> to_vector();
Now that we already have a vector with the result of the operation, we will iterate it and print its elements to the serial port, to confirm we get the expected output.
for(int i=0; i<result.size(); ++i){
Serial.print(result[i]);
Serial.print("|");
}
For comparison, we will now apply the except operator to array2, passing as input array1.
auto result2 = from_array(array2)
>> except(from_array(array1))
>> to_vector();
for(int i=0; i<result2.size(); ++i){
Serial.print(result2[i]);
Serial.print("|");
}
The complete code can be seen below.
#include "cpplinq.hpp"
using namespace cpplinq;
void setup() {
Serial.begin(115200);
int array1[] = {1,2,3,4};
int array2[] = {3,4,5,6};
auto result = from_array(array1)
>> except(from_array(array2))
>> to_vector();
Serial.println("Result 1:");
for(int i=0; i<result.size(); ++i){
Serial.print(result[i]);
Serial.print("|");
}
auto result2 = from_array(array2)
>> except(from_array(array1))
>> to_vector();
Serial.println("\nResult 2:");
for(int i=0; i<result2.size(); ++i){
Serial.print(result2[i]);
Serial.print("|");
}
}
void loop() {}
Testing the code
To test the code, simply compile it and upload it to your device. When the procedure finishes, open the Arduino IDE serial monitor.
You should get an output similar to figure 1. As can be seen, for the first case, we have obtained the numbers 1 and 2, since the remaining numbers of array1 (3 and 4) also exist in array2.
For the second case we obtained the numbers 5 and 6 since these are the only numbers from array2 that don’t exist on array1.
Figure 1 – Output of the program, showing the result of both intersection operations. Taken from the tests on the ESP32.