Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::string.substr运行时错误_C++_String_C++11_Vector_Std - Fatal编程技术网

C++ std::string.substr运行时错误

C++ std::string.substr运行时错误,c++,string,c++11,vector,std,C++,String,C++11,Vector,Std,我一直在做一个平衡化学方程式的程序。我有它,所以它根据=将方程分成两个边。我正在编写我的程序,我做了一些事情,现在当我试图将std::vector的第一个索引设置为等式的substr时,我遇到了一个运行时错误。我需要你帮我弄清楚 std::vector<std::string> splitEquations(std::string fullEquation) { int pos = findPosition(fullEquation); std::vector<

我一直在做一个平衡化学方程式的程序。我有它,所以它根据
=
将方程分成两个边。我正在编写我的程序,我做了一些事情,现在当我试图将
std::vector
的第一个索引设置为等式的
substr
时,我遇到了一个运行时错误。我需要你帮我弄清楚

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

错误不在
substr
上,而是在向量的
运算符[]
上。当您尝试在索引0和1处赋值时,向量仍然为空。如果需要,它有两个预留的扩展点,但其“活动区域”的大小为零;访问它会导致错误

您可以使用
push_back
修复此问题,如下所示:

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

错误不在
substr
上,而是在向量的
运算符[]
上。当您尝试在索引0和1处赋值时,向量仍然为空。如果需要,它有两个预留的扩展点,但其“活动区域”的大小为零;访问它会导致错误

您可以使用
push_back
修复此问题,如下所示:

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

reserve()
更改为
resize()
,它就可以工作了。在所有其他情况下,
reserve()
调用不会导致重新分配,并且向量容量不会受到影响,但是
resize()
会影响。将
reserve()
更改为
resize()
,它会起作用。在所有其他情况下,
reserve()
调用不会导致重新分配,并且向量容量不会受到影响,但是
resize()
会影响。

成员函数
reserve

leftAndRightEquation.reserve(2);
类的
std::vector
不创建向量的元素。它只是为将来添加到向量的元素保留内存

因为向量没有元素,所以不能使用下标操作符。取而代之的是,您必须使用成员函数
push_back
此外,还可以更简单地指定第二个子字符串

leftAndRightEquation.push_back( fullEquation.substr( 0, pos ) );
leftAndRightEquation.push_back( fullEquation.substr( pos + 1 ) );
class std::basic_string
的成员函数
substr
声明如下

basic_string substr(size_type pos = 0, size_type n = npos) const;
std::vector<std::string> leftAndRightEquation( 2 );
也就是说,它有两个带有默认参数的参数

如果要使用下标运算符,则首先应创建包含两个元素的向量。你可以用下面的方法来做

basic_string substr(size_type pos = 0, size_type n = npos) const;
std::vector<std::string> leftAndRightEquation( 2 );

会员功能
预留

leftAndRightEquation.reserve(2);
类的
std::vector
不创建向量的元素。它只是为将来添加到向量的元素保留内存

因为向量没有元素,所以不能使用下标操作符。取而代之的是,您必须使用成员函数
push_back
此外,还可以更简单地指定第二个子字符串

leftAndRightEquation.push_back( fullEquation.substr( 0, pos ) );
leftAndRightEquation.push_back( fullEquation.substr( pos + 1 ) );
class std::basic_string
的成员函数
substr
声明如下

basic_string substr(size_type pos = 0, size_type n = npos) const;
std::vector<std::string> leftAndRightEquation( 2 );
也就是说,它有两个带有默认参数的参数

如果要使用下标运算符,则首先应创建包含两个元素的向量。你可以用下面的方法来做

basic_string substr(size_type pos = 0, size_type n = npos) const;
std::vector<std::string> leftAndRightEquation( 2 );

reserve
实际上不会创建任何新元素。旁注:为什么要使用向量而不是两个变量?@Cameron因为后来我不得不对它们执行一些操作,我认为最好是编写一个循环,而不是两次相同的代码,但变量除外。那么如果findPosition()会发生什么呢返回它找不到字符串?@PaulMcKenzie,我认为我不应该有这个问题。我的程序进入需要findPosition()的阶段的唯一方法是,我还没有进入异常处理阶段。我会把代码贴到我的原始问题上,你可以告诉我你的想法。老实说我甚至没有想过。
reserve
实际上不会创建任何新元素。旁注:为什么要使用向量而不是两个变量?@Cameron,因为后来我必须对它们执行一些操作,我认为最好编写一个循环,而不是同一个代码两次,但变量除外。所以如果findPosition()返回找不到字符串,会发生什么情况?@PaulMcKenzie,我认为我不应该有这个问题。我的程序进入需要findPosition()的阶段的唯一方法是,我还没有进入异常处理阶段。我会把代码贴到我的原始问题上,你可以告诉我你的想法。老实说,我甚至没有想过。谢谢你简化了我的第二个
substr
。我没有意识到
reserve
resize
之间的区别。感谢您简化了我的第二个
substr
。我没有意识到
reserve
resize
之间的区别。