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.reserve(2);
    leftAndRightEquation[0] = fullEquation.substr(0, (pos)); //!!!! Error
    leftAndRightEquation[1] = fullEquation.substr( (pos+1), (fullEquation.size() - (pos)) );
    removeWhiteSpace(leftAndRightEquation);
    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;
}

Answers


Member function reserve

leftAndRightEquation.reserve(2);

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

how to edit a div and meta tags in a html page using php

javascript php jquery html file

I have a situation here. I need to edit only meta tags and the contents of a &lt;div id="container"&gt;&lt;/div&gt; in a remote html page, using php.

LocationClient not working with Google Play services imported properly

java android gps

I need some help with a little error I'm getting when using Google Play Services.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.