使用结构向量 c++ 的断言错误
Assertion Error using a struct vector c++
我有一个程序,我想从一个字符串更新一个变量。该函数会读入一个字符串,查找是否是加法、减法等,然后将其添加到变量中。函数是这样的:
using namespace std;
struct variable{
string name;
int value;
};
void update_varabile(string line, vector<variable> & v)
{
char c = line[0]; //variable to be updated
string b;
char d[0];
int flag = 0; //counter
int a = 0;
int temp_value = 0;
int perm_value = 0;
for (int i = 0; i < v.size(); i++) {
if (c == v[i].name[0]) {
flag = 1;
temp_value = v[i].value;
break;
}
}
if (flag == 1) { //variable is present
for (int i = 0; i< line.size(); i++) {
if (line[i] == '+'|| line[i] =='-'|| line[i] == '*'|| line[i] =='/') {
b[0] = line[i+1]; //assuming the integer is between 0 and 9
d[0] = b[0];
a = atoi (d);
if (line [i] == '+') {
perm_value = temp_value + a;
} else if (line [i] == '-') {
perm_value = temp_value - a;
} else if (line [i] == '*') {
perm_value = temp_value * a;
} else if (line [i] == '/') {
perm_value = temp_value / a;
}
}
}
for (int i = 0; i < v.size(); i++) {
if (v[i].name[0] == 'c') {
v[i].value = perm_value;
break;
}
}
}
}
main 中的调用如下所示:
int main()
{
variable a;
int val = 0;
string up = "c=c+2";
string f = "c";
vector<variable> q;
a.name = f;
a.value = val;
q.push_back(a);
update_varabile(up, q);
return 0;
}
但是,当我 运行 代码时,我收到此错误消息:
Assertion failed: ((m_->valid == LIFE_MUTEX) && (m_->busy > 0)), file C:/crossdev/src/winpthreads-git20141130/src/mutex.c, line 57
Process returned 1 (0x1) execution time : 0.014 s
Press any key to continue.
我逐行 运行 调试器,它显示函数正确执行。我也试图在我的电脑上寻找那个 C:/ 文件,但它不存在。不确定为什么这不起作用。
首先,摆脱所有的休息时间。在 C++ 中,只应在每个 case 语句的末尾使用换行符。几乎不可能阅读带有一堆中断的代码,因为我必须深入了解每个中断是什么以及为什么。如果您需要尽早退出 for 循环,请使用 while 循环。您不需要在 if 和 else 语句末尾进行中断,因为它们会导致程序提前离开函数,如果您使用 if、else if 和 else 条件格式,您的 if 和 else 语句自然会跳过。
话虽如此,您需要更好地分解您正在尝试做的事情。
例如你得到一个像这样的字符串值。
2+3+4-5+6
您的程序将从左到右读取。我假设您希望它采用第一个值,即 2,然后将 3 添加到它,然后是 4,依此类推,以此类推。
方法是先解析字符串中的int值,再解析加减值。换句话说,从字符串中读取 int 值,直到您找到一个不在 0 到 9 之间的值。然后查看该非数值是否是您要查找的运算符。这样你的程序就不会被 2555 和 2 这样的值绊倒。
IE
//intValueHolder is a string.
while(i < line.size() && line[i] >= '0' && line[i] <= '9' ) {
intValueHolder.push_back(string[i]);
}
然后当您点击“+”或类似的东西时,将 char 值放入 case 语句中。并且不要忘记在末尾添加一个默认值来解释像 'a' 这样的垃圾输入。您可能希望保留该值,以防万一您需要先获取左侧值,然后才能获取右侧值。但这听起来像是您从左侧值开始,所以您实际上只需要找到右侧值以及它需要哪个运算符。我不会重写你的程序,因为这看起来像是学校的作业。但我会为你指出正确的方向。让我知道,如果我不理解你的问题。
如果您不仅仅限于字符串和向量,您可能还想研究为此使用队列。
我有一个程序,我想从一个字符串更新一个变量。该函数会读入一个字符串,查找是否是加法、减法等,然后将其添加到变量中。函数是这样的:
using namespace std;
struct variable{
string name;
int value;
};
void update_varabile(string line, vector<variable> & v)
{
char c = line[0]; //variable to be updated
string b;
char d[0];
int flag = 0; //counter
int a = 0;
int temp_value = 0;
int perm_value = 0;
for (int i = 0; i < v.size(); i++) {
if (c == v[i].name[0]) {
flag = 1;
temp_value = v[i].value;
break;
}
}
if (flag == 1) { //variable is present
for (int i = 0; i< line.size(); i++) {
if (line[i] == '+'|| line[i] =='-'|| line[i] == '*'|| line[i] =='/') {
b[0] = line[i+1]; //assuming the integer is between 0 and 9
d[0] = b[0];
a = atoi (d);
if (line [i] == '+') {
perm_value = temp_value + a;
} else if (line [i] == '-') {
perm_value = temp_value - a;
} else if (line [i] == '*') {
perm_value = temp_value * a;
} else if (line [i] == '/') {
perm_value = temp_value / a;
}
}
}
for (int i = 0; i < v.size(); i++) {
if (v[i].name[0] == 'c') {
v[i].value = perm_value;
break;
}
}
}
}
main 中的调用如下所示:
int main()
{
variable a;
int val = 0;
string up = "c=c+2";
string f = "c";
vector<variable> q;
a.name = f;
a.value = val;
q.push_back(a);
update_varabile(up, q);
return 0;
}
但是,当我 运行 代码时,我收到此错误消息:
Assertion failed: ((m_->valid == LIFE_MUTEX) && (m_->busy > 0)), file C:/crossdev/src/winpthreads-git20141130/src/mutex.c, line 57
Process returned 1 (0x1) execution time : 0.014 s
Press any key to continue.
我逐行 运行 调试器,它显示函数正确执行。我也试图在我的电脑上寻找那个 C:/ 文件,但它不存在。不确定为什么这不起作用。
首先,摆脱所有的休息时间。在 C++ 中,只应在每个 case 语句的末尾使用换行符。几乎不可能阅读带有一堆中断的代码,因为我必须深入了解每个中断是什么以及为什么。如果您需要尽早退出 for 循环,请使用 while 循环。您不需要在 if 和 else 语句末尾进行中断,因为它们会导致程序提前离开函数,如果您使用 if、else if 和 else 条件格式,您的 if 和 else 语句自然会跳过。
话虽如此,您需要更好地分解您正在尝试做的事情。 例如你得到一个像这样的字符串值。 2+3+4-5+6 您的程序将从左到右读取。我假设您希望它采用第一个值,即 2,然后将 3 添加到它,然后是 4,依此类推,以此类推。
方法是先解析字符串中的int值,再解析加减值。换句话说,从字符串中读取 int 值,直到您找到一个不在 0 到 9 之间的值。然后查看该非数值是否是您要查找的运算符。这样你的程序就不会被 2555 和 2 这样的值绊倒。
IE
//intValueHolder is a string.
while(i < line.size() && line[i] >= '0' && line[i] <= '9' ) {
intValueHolder.push_back(string[i]);
}
然后当您点击“+”或类似的东西时,将 char 值放入 case 语句中。并且不要忘记在末尾添加一个默认值来解释像 'a' 这样的垃圾输入。您可能希望保留该值,以防万一您需要先获取左侧值,然后才能获取右侧值。但这听起来像是您从左侧值开始,所以您实际上只需要找到右侧值以及它需要哪个运算符。我不会重写你的程序,因为这看起来像是学校的作业。但我会为你指出正确的方向。让我知道,如果我不理解你的问题。
如果您不仅仅限于字符串和向量,您可能还想研究为此使用队列。