std::string.substr Run Time Error

I've been working on a program to balance chemical equations. I had it so it split the equation into two sides based upon the =. I was working on my program and I did something, now I'm getting a run time error when I attempt to set the first index of std::vector<std::string> to the substr of my equation. I need help figuring this out.

std::vector<std::string> splitEquations(std::string fullEquation)
    int pos = findPosition(fullEquation);
    std::vector<std::string> leftAndRightEquation;
    leftAndRightEquation[0] = fullEquation.substr(0, (pos)); //!!!! Error
    leftAndRightEquation[1] = fullEquation.substr( (pos+1), (fullEquation.size() - (pos)) );
    std::cout << leftAndRightEquation[0] << "=" << leftAndRightEquation[1] << std::endl;
    return leftAndRightEquation;

Here's my code for findPosition.

int findPosition(std::string fullEquation)
    int pos = 0;
    pos = fullEquation.find("=");
    return pos;


Member function reserve


of class std::vector does not create elements of the vector. It simply reserves memory for elements that will be added to the vector in future.

So as the vector has no elements you may not use the subscript operator. Instead of it you have to use member function push_back ALso the second substring can be specified simpler.

leftAndRightEquation.push_back( fullEquation.substr( 0, pos ) );
leftAndRightEquation.push_back( fullEquation.substr( pos + 1 ) );

Member function substr of class std::basic_string is declared the following way

basic_string substr(size_type pos = 0, size_type n = npos) const;

that is it has two parameters with default arguments.

If you want to use the subscript operator then you initially should create the vector with two elements. You can do it the following way

std::vector<std::string> leftAndRightEquation( 2 );

And after that you may write

leftAndRightEquation[0] = fullEquation.substr( 0, pos );
leftAndRightEquation[1] = fullEquation.substr( pos + 1 );

The error is not on substr, it's on the vector's operator[]. At the time when you try assigning at indexes zero and one, the vector is still empty. It has two spots reserved for expansion, if needed, but the size of its "active area" is zero; accessing it causes the error.

You can use push_back to fix the problem, like this:

leftAndRightEquation.push_back(fullEquation.substr(0, (pos)));
leftAndRightEquation.push_back(fullEquation.substr( (pos+1), (fullEquation.size() - (pos)) ));

changereserve() to resize(), it'll work. In all other cases, the reserve() call does not cause a reallocation and and the vector capacity is not affected, but resize() does.

Need Your Help

Convert BLE 4.0 data into usable information using Swift (iOS)

ios swift arduino bluetooth-lowenergy

This is my first BLE project using swift and would greatly appreciate any direction you guys have out there. My arduino is using a red bear lab BLE mini to send integers (between 1 and 1200) from a

How to create a LayoutInflater Given XmlPullParser as input?

xmlpullparser layout-inflater

I've a String Input that contains an Layout.xml with String Fromat.