简体   繁体   English

std :: string.substr运行时错误

[英]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. 我正在编写程序,做了一些事情,现在当我尝试将std::vector<std::string>的第一个索引设置为方程式的substr时,出现了运行时错误。 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 . 这是我的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[] . 错误不在substr ,而是在向量的operator[] At the time when you try assigning at indexes zero and one, the vector is still empty. 当您尝试分配索引0和1时,向量仍然为空。 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: 您可以使用push_back来解决此问题,如下所示:

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

change reserve() to resize() , it'll work. reserve()更改为resize() ,它将起作用。 In all other cases, the reserve() call does not cause a reallocation and and the vector capacity is not affected, but resize() does. 在所有其他情况下, reserve()调用不会导致重新分配,并且向量容量不受影响,但是resize()会影响。

Member function reserve 会员功能reserve

leftAndRightEquation.reserve(2);

of class std::vector does not create elements of the vector. std::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. 取而代之的是,您必须使用成员函数push_back AL,以便可以更简单地指定第二个子字符串。

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 class std::basic_string成员函数substr的声明方式如下

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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