简体   繁体   中英

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;
}

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)) ));

change reserve() 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.

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM