Cannot call an element for a specific index in a vector list

Hello everyone,

I am having a massive headache as to figuring out why this is occuring, but this is probably my ignorance of C++. I am trying to access a specific element in a vector list of strings, but every time I compile the code:

#include <vector>

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

void loop() {
  String change = "S0false, S1true, S2false, S3false, S4true";
  char cArray[change.length() + 1];
  strcpy(cArray, change.c_str());
  char* changes = strtok(cArray, ", ");
  std::vector<String> arr;
  while (changes != NULL) {
    arr.push_back(changes);
    changes = strtok(NULL, ", ");
  }

    if (!trueFalse(arr.at(0))) {
      Serial.print('f');
    }
}

bool trueFalse(String test) {
  return test.charAt(2) == 't';
}

I get the following error:

sketch\sketch_dec29a.ino.cpp.o: In function `std::vector<String, std::allocator<String> >::_M_range_check(unsigned int) const':

c:\users\kyle\appdata\local\arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1\arm-none-eabi\include\c++\4.8.3\bits/stl_vector.h:794: undefined reference to `std::__throw_out_of_range(char const*)'

collect2.exe: error: ld returned 1 exit status

But when I change

    if (!trueFalse(arr.at(0))) {
      Serial.print('f');
    }

to:

  for (int i = 0; i < arr.size(); i++) {
    if (!trueFalse(arr.at(i))) {
      Serial.print(i);
    }
  }

It compiles without a problem. I'm not sure if there's a misunderstanding that I am not seeing, or what is going on. If anyone is able to help out, I'd greatly appreciate it.

Thank you,
-Kyle

Does it run ?

Why are you using this "vector" library? Seems like an overly complicated way to do something rather simple - but I don't know your project

Power_Broker:
Why are you using this "vector" library? Seems like an overly complicated way to do something rather simple - but I don't know your project

I was trying to use the vector library in order to create a dynamic array, similar to Java's ArrayList, so that I could add members to the array without having to declare a fixed size. This was a code snippet. The idea was that I would send the Arduino an array as a string (with each element separated by commas) and that I would split up the elements and put them in the vector array. Different conditions would send arrays of different sizes.

However, after realizing how many elements I knew it was sending, I just wound up sending an integer with the array that indicates the size.

so that I could add members to the array without having to declare a fixed size.

Sorry, but I don't see the point of that. If the array will need to be a certain (large) size at some time then it might just as well be declared at that size in the first place.