Vector of Strings

Banging my head against the wall trying to get a vector of strings to work. Does anyone know how to use a vector of strings on Photon?

    #include "Particle.h"
    #include "neopixel.h"
    #include "neomatrix.h"
    #include "Adafruit_GFX.h"
    #include <string>
    #include <vector>

    std::vector<std::string> messageQueue;
    //std::vector<string> messageQueue;
    //vector<string> messageQueue; 

Results in several errors. Error points to stl_vector.h
The version of my code without vector works fine.

“Macro swap requires two arguments, but only 1 given.”
“Macro swap passed four arguments, but only requires 2.”


I don’t know if Particle supports vectors. Have you tried using a 2D array?

I’m using a cloud event and subscription to get a text message. Since the data is passed to the Photon as string, I’d like to be able to just pop and push the strings as they come in. I don’t really need access to the individual characters.

I may try to push the messageQueue process back onto the node.js web server that receives the texts.

Have you tried to #undef swap?
I’d put that line after all the contributed libraries before #include <vector>

I guess one of the used libraries defines the macro swap() but std::vector does feature a swap() method.

I can verify that the Photon does support a vector of strings; I am using in one of my projects. @ScruffR is probably correct about the method conflicts.

1 Like

Thanks. #undef swap in the right place got rid of a few errors. I’m down to the following:

'no matching function for call to 'std::vector<std::__cxx11:basic_string >::push_back(String&)'
ditto "has no member named pop_front"
conversion '__gnu_cxx::__alloc_traits<std::allocator<std::__cxx11::basic_string >::value_type…

I am using the std::vector<std::string> messageQueue; line.

Does this answer your question about pop_front

And about push_back(): String objects are not the same thing as std:string.

Thanks for your help everyone. String vs string still confuses me. I need to read up on deque but this compiles:

#include "Particle.h"
#include "neopixel.h"
#include "neomatrix.h"
#include "Adafruit_GFX.h"
#undef swap
#include <deque>
std::deque<String> messageQueue;

//#include </string/> wasn’t needed.

From my project, one of my classes.

#include "application.h"
#include "Sequencer.h"
#include "Mode.h"
#include "Repeater.h"
#include "Settings.h"
#include <vector>
#include <map>

uint8_t pi = 0;         //Processing Index. Index of current element being processed.
bool Active = false;    //Used to indicate that the sequence is active and processing should occur.
bool AdvanceNow = false;
bool SequenceComplete = false;

std::vector<String> Elements;      //List of Modes in this sequence. Modes stored as JSON string.
std::map<uint8_t, int> RepeatTracker;    //Tracks how many times the current repeat step has repeated so far.

Sequencer::Sequencer() {

To add to the vector I am using:

//Adds an element to the end of the sequence.
void Sequencer::Add(String JsonString) {

And to read from the vector I am using:

if (pi >= 0 && pi < Elements.size()) {          //Check the index is within bounds of Elements container.
        String full = Elements[pi];

This compiles and functions as I expect. This is the reference I used for researching the functionality:

One word of caution is that many of the std:: functions and certainly String use dynamic memory allocation. On small systems like Particle’s, this can quickly lead to heap fragmentation which can reap havoc on an otherwise seemingly stable system. The issue is not the total amount of heap available but the erosion of the largest piece of heap which can be allocated. :wink: